算数运算符
比较运算符
赋值运算符
逻辑运算符
身份运算符
算数运算符
1+1
2
1*6
6
1/9
0.1111111111111111
1//1
1
比较运算符
1>1
False
1<2
True
1>=1
True
赋值运算符
m=1
print(m)
1
逻辑运算符
- and
- or
- not
1<=1 and 1>=1#and逻辑条件都要成立才可以
True
#or要的是一个成立就可以了
身份运算符
is比较的是id
x=10
y=10
print(x is y)
print(x == y)
True
True
x=295
y=295
print(x is y)
print(x == y)
False
True
x=295
y=29
print(x is y)
print(x == y)
False
False
id相同值一定相同,值相同的id不一定相同,除小整数池
Python的优先级
想单独运算那个要加括号
1*3 > 2*1
True
2*(96+3)/8
24.75
链式赋值(考试必考)
y,j,k =10, 20 ,30
print(y,j,k)
10 20 30
交叉赋值(考试必考)
x=10
y=20 #现在要求y=10 x=20
x,y=y,x #这个是交叉赋值
print(x,y)
20 10
解压缩
hobby_list = ['piao','666',2333,999,'handsone','resd','misic','fooball']
_,hobby1,_,_,_,_,hobby2,_=hobby_list#下划线表示值你字典里面有多少个就要有多少个
print(hobby1,hobby2,_)#为什么最后下划线的值是fooball,因为每一次赋值前一个下划线的值就会被后一个的覆盖
666 misic fooball
hobby_list = ['piao','666',2333,999,'handsome','read','piao','666',2333,999,'handsome','read']
hobby1,*a,hobby2 = hobby_list
print(hobby1)
print(hobby2)
print(a)
piao
read
['666', 2333, 999, 'handsome', 'read', 'piao', '666', 2333, 999, 'handsome']
hobby_list = ['piao','666',2333,999,'handsome','read','piao','666',2333,999,'handsome','read']
hobby1,*_,hobby2 = hobby_list
print(_)
['666', 2333, 999, 'handsome', 'read', 'piao', '666', 2333, 999, 'handsome']
hobby_list = ['piao','666']
hobby1,hobby2 = hobby_list
print(hobby1)
print(hobby2)
piao
666