Python入门7 —— 赋值运算符补充

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12349132.html

增量赋值

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)
posted @ 2020-02-23 12:14  轻描丨淡写  阅读(225)  评论(0编辑  收藏  举报