PythonStudy——赋值运算符 Assignment operator

eg:

num = 10
num += 1      # 等价于  num = num + 1 => 11
print(num)

特殊操作:

1.链式赋值

a = b = num
print(a, b, num, id(a), id(b), id(num))

2.交叉赋值

# 传统交换赋值
x = 10 y = 20 temp = xx = yy = tempprint(x, y)

Output:
20 10
x, y = y, x
print(x, y)

3.解压赋值

ls = [3, 1, 2]

a, b, c = ls
print(a, b, c)

res = ls
print(res)

Output:

  3 1 2
  [3, 1, 2]

# _是合法的变量名,会接受值,但我们认为_代表该解压位不用接收,用_来接收表示

_, _, g = ls
print(g)

Output:
2
原文地址:https://www.cnblogs.com/tingguoguoyo/p/10713174.html