运算符
运算符
算数运算符
1+1
2
1-1
0
1*1
1
2%1
0
5//2
2
比较运算符
1>1
False
1<2
True
1>=1
True
1==1
True
1!=2
True
赋值运算符
x=1
print(x)
1
逻辑运算符
1.and
2.or
3.not
1==1 and 2>1#两个都对
True
1!=1 or 2>1#一个满足就行
True
not not"我是神"
True
身份运算符
is 比较的是id
x=10
y=10
print(x==y)
print(x is y)
True
True
x=257
y=257
print(x==y)
print(x is y)#小整数池不生效
True
False
python运算符的优先级
不背了,想先算的直接加().
链式赋值
x=10
y=10
z=10
x,y,z=10,10,10
print(x,y,z)
10 10 10
交叉赋值
x=10
y=20
z=x
x=y
y=z
print(x,y)
20 10
x=10
y=20
x,y=y,x
print(x,y)
20 10
解压缩
hobby_list=['wan','566',232,777]
print(hobby_list[1])
566
hobby_list=['wan','566',232,777]
print(hobby_list[1])
print(hobby_list[-1])
print(hobby_list[2])
566
777
232
hobby_list=['wan','566',232,777]
_,hobby1,_,hobby2=hobby_list
print(hobby1)
print(hobby2)
566
777
hobby_list=['wan','566',232,777,'wan','566',232,777]
_,hobby1,_,hobby2,*_=hobby_list
print(hobby1)
print(hobby2)
566
777
hobby_list=['wan','566',232,777,'wan','566',232,777]
hobby1,*a,hobby2=hobby_list
print(hobby1)
print(hobby2)
print(a)
wan
777
['566', 232, 777, 'wan', '566', 232]