两个整数变量 a = 5, b = 100,交换数值
1.使用其他变量
c = a
a = b
b = a
print(a, b)
2.不使用临时变量
a = a + b
b = a - b
a = a - b
print(a, b)
3.利用元组
# a, b = (b, a)
a, b = b, a
print(a, b)
或者
def exchange():
b = 100
a = 6
return b, a
a, b = exchange()
print(a, b)
4.异或运算
a = a ^ b
b = a ^ b
a = a ^ b
print(a, b)