temp
變量。該temp變量用來存儲(chǔ)拳頭變量的值(temp = a)
,允許你交換兩個(gè)變量的值(a = b)
,然后分配的值temp
到所述第二變量。a = 11
b = 7
temp = a
a = b
b = temp
print(a) # 7
print(b) # 11
a = 11
b = 7
a, b = b, a
print(a) # 7
print(b) # 11
( +, -)
或乘法和除法 ( *, /)
。這種交換方法是基于計(jì)算兩個(gè)數(shù)字的總和,然后使用總和和與總和的差來交換它們。a = 11
b = 7
a = a + b # a = 18, b = 7
b = a - b # a = 18, b = 11
a = a - b # a = 7, b = 11
print(a) # 7
print(b) # 11
聯(lián)系客服