增量赋值
x = 10
x += 1 #就是:x = x+1
交叉赋值
a = 10
b = 20
print(a,b)
temp=b # temp=20
b=a # b = 10
a=temp
# python一行代码搞定:
a, b = b, a
print(a,b)
链式赋值
a=7
b=a
c=b
d=c
a = b = c = d = 7
print(a,b,c,d)
解压赋值: 取开头和结尾的几个值
salaries=[33,44,55,66,77]
x=salaries[0]
y=salaries[1]
z=salaries[2]
a=salaries[3]
b=salaries[4]
左边变量名的个数与右面包含值的个数相同,多一个不行,少一个也不行
x,y,z,a,b=salaries
print(x,y,z,a,b)
salaries=[33,44,55,66,77,88,99]
x,y,z,*abc=salaries
x,y,z会对应列表salaries的前三个值
然后*会把剩余的值存放一个列表,然后赋值给abc
print(x,y,z)
print(abc)
_当变量名,代表该变量值是无用的
x,y,z,*_=salaries #取前三个值
print(x,y,z)
print(_)
salaries=[33,44,55,66,77,88,99]
*_,m,n=salaries # 取后两个值
print(_)
print(m,n)
# salaries=[33,44,55,66,77,88,99]
x,y,z,*_,m=salaries #取后两个值
print(x,y,z)
print(m)
d={'a':1,'b':2,'c':3}
x,y,z=d
print(x,y,z)