假设a = 1, b = 2,试交换a, b的值。
方法1:Python特有
a, b = b, a # Python中支持此写法
方法2:临时变量
temp = a
a = b
b = temp
方法3:异或
异或有个特性就是相同则抵消为零
a = a ^ b
b = a ^ b # (a ^ b) ^ b = a
a = a ^ b # (a ^ b) ^ a = b
方法4:加减法
a = a + b
b = a - b # b = (a + b) - b = a
a = a - b # a = (a + b) - a = b