# 赋值运算符,运算顺序从右到左
i =3+4print(i)print("---------------------支持链式赋值------------------------")
a = b = c =20# 链式赋值print(a,id(a))print(b,id(b))print(c,id(c))print("---------------------支持参数赋值------------------------")
a =20
a +=30# 相当于 a = a + 30print(a,type(a),id(a))
a -=10# 相当于 a = a - 10print(a,type(a),id(a))
a *=2# 相当于 a = a * 2print(a,type(a),id(a))
a /=3# 相当于 a = a / 3print(a,type(a),id(a))
a //=2# 相当于 a = a // 2print(a,type(a),id(a))
a %=3# 相当于 a = a % 3print(a,type(a),id(a))print("---------------------支持系列解包赋值------------------------")
a, b, c =20,30,40print(a, b, c)# a, b = 20, 30, 40 # 报错。因为左边变量的个数和右边值的个数不一致print("---------------------交换两个变量的值------------------------")
a, b =10,20print("交换之前:", a, b)
a, b = b, a # 交换print("交换之后:", a, b)
运行结果:
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnpython\马士兵Python\第3章_算你赢\P23_22.运算符_赋值运算符.py
7---------------------支持链式赋值------------------------201407333180348242014073331803482420140733318034824---------------------支持参数赋值------------------------50<class'int'>14073331803578440<class'int'>14073331803546480<class'int'>14073331803674426.666666666666668<class'float'>207223066820813.0<class'float'>20722306741281.0<class'float'>2072230668208---------------------支持系列解包赋值------------------------203040---------------------交换两个变量的值------------------------
交换之前: 1020
交换之后: 2010
Process finished with exit code 0