1.使用临时变量
a = 10
b = 20
temp = a
a = b
b = temp
print("交换后的a:", a)
print("交换后的b:", b)
2.使用元组解包
a = 10
b = 20
a, b = b, a
print("交换后的a:", a)
print("交换后的b:", b)
3.使用加法和减法
a = 10
b = 20
a = a + b
b = a - b
a = a - b
print("交换后的a:", a)
print("交换后的b:", b)
4.使用异或操作
a = 10
b = 20
a = a ^ b
b = a ^ b
a = a ^ b
print("交换后的a:", a)
print("交换后的b:", b)
5.使用多重赋值
a = 10
b = 20
a, b = b, a
print("交换后的a:", a)
print("交换后的b:", b)