python 赋值运算符

时间:2022-08-25 01:35:28

赋值运算符包括:= 、+= 、-= 、*= 、/= 、%=

In [1]: x = 2     # 赋值操作符
In [2]: x += 2    # 加等,相当于 x = x + 2
In [3]: x -= 2    # 减等,相当于 x = x - 2
In [4]: x *= 2    # 乘等,相当于 x = x * 2
In [5]: x /= 2    # 除等,相当于 x = x / 2
In [6]: x %= 2    # 取余,相当于 x = x % 2
In [1]: x = y = z = 1    # 多重赋值
In [2]: x, y, z = 1, 2, 'string'    # 多元赋值

In [3]: x
Out[3]: 1

In [4]: y
Out[4]: 2

In [5]: z
Out[5]: 'string'