Python 核心编程

1. 整型 int

  • 没有长度限制
score = 95
type(score)

int

num = 888888888888888888888888888888888888888888888888888888888888888888888888888888
type(num)

int

num

888888888888888888888888888888888888888888888888888888888888888888888888888888

a = 4
b = 3
a + b

7

a - b

1

a * b

12

a / b

1.3333333333333333

a ** b

64

a ** (1 / b)

1.5874010519681994

import math
math.exp(3)

20.085536923187668

math.log(a)

1.3862943611198906

str(a)

‘4’

c = int('5')
c

5

a > b

True

b > a

False

 a == b

False

a >= b

True

a <= b

False

a != b

True

2. 浮点数 float

  • 浮点数是不精准存储
score = 82.11
type(score)

float

price = 5.55555555555555555555555555555555555555555555555555555555555
price

5.555555555555555

type(price)

float

distance = 1.5e-4
distance

0.00015

a = 1.5
b = 2.7
a + b

4.2

a - b

-1.2000000000000002

a * b

4.050000000000001

a / b

0.5555555555555555

import math
a = 3.5
a ** 2

12.25

a ** (1/2)

1.8708286933869707

a = 5.6
math.exp(a)

270.42640742615254

math.log(a)

1.7227665977411035

math.log2(a)

2.4854268271702415

str(a)

‘5.6’

c = float('2.3132')
c

2.3132

a = 3.23452
math.ceil(a)

4

math.floor(a)

3

int(a)

3

round(a,2)

3.23

a = 1-0.55
a

0.44999999999999996

a = 3.5
b = 6.43
a < b

True

a = 1 - 0.55
a == 0.45

False

# 浮点数的相等不能直接比较
math.fabs(a - 0.45) < 1e-6

True

3. 布尔类型 bool

  • True 就是1 , False 就是0
True

True

False

False

result = True
3>2

True

1==2

False

True+1

2

2 ** True

2

1 / False

ZeroDivisionError Traceback (most recent call last)

Cell In[85], line 1
----> 1 1 / False

ZeroDivisionError: division by zero

True and True

True

True and False

False

True or False

True

not False

True

not True

False

4. 字符串 str

  • 单引号/双引号/三单引号/三双引号
s1 = '人工智能'
s2 = "人工智能"
type(s1),type(s2)

(str, str)

s3='''这是一个多行字符串
我可以自由换行
'''
s3

‘这是一个多行字符串\n我可以自由换行\n’

s4="""三双引号
一样可以灵活换行"""
s4

‘三双引号\n一样可以灵活换行’

len(s4)

13

s4.__len__()

13

s="qwertyuiopezxcfvgbnhmpdsfkpisjfoid"
s[0]

‘q’

s[10]

‘e’

s[-1]

‘d’

s[-2]

‘i’

s[:3]

‘qwe’

s[3:5]

‘rt’

s[1:-1]

‘wertyuiopezxcfvgbnhmpdsfkpisjfoi’

len(s)

34

#[start:stop:step]
s[1:-4:3]

‘wtiecghdks’

for ele in s[5:-6:4]:
    print(ele)

y
p
c
b
p
k

s = "            erwreqw"
s.strip()

‘erwreqw’

s = "   \t\n AI \t \n"
s

’ \t\n AI \t \n’

s.strip()

‘AI’

s = "人工  \t智能"
s.strip()

‘人工 \t智能’

s.replace("\t","").replace(" ","")

‘人工智能’

s = "1,2,3,4,5,6  "
ls = s.strip().split(",")
ls

[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’]

s = ",".join(ls)
s

‘1,2,3,4,5,6’

s = "Hello World"
s.lower()

‘hello world’

s.upper()

‘HELLO WORLD’

s1 = "AI "
s2 = "人工智能 "
s1+s2

'AI 人工智能 ’

s = 3*s1+ 5* s2
s

'AI AI AI 人工智能 人工智能 人工智能 人工智能 人工智能 ’

s.count("AI")

3

'人工' in s

True

'智能' not in s

False

5. 列表 list

  • 元素有顺序,元素可以是任意类型,元素可以重复
ls = [1,2,3,4]
type(ls)

list

ls = list("abc")
ls

[‘a’, ‘b’, ‘c’]

[1,2] == [2,1]

False

ls = [1,True,"asd",[1,2,3,4]]
ls

[1, True, ‘asd’, [1, 2, 3, 4]]

len
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值