[转]在不声明新变量的情况下,让变量a和变量b的值互换?
2017-03-22 本文已影响18人
everfight
>>> a = 1
>>> b = 9
>>> a = a + b
>>> b = a - b
>>> a = a - b
>>> a
9
>>> b
1
上面是转的,当时感觉思路挺不错。现在想想,在python中好像没必要这么麻烦:
>>>a =1
>>>b = 9
>>>a,b = b,a
>>>a
9
>>>b
1
>>> a = 1
>>> b = 9
>>> a = a + b
>>> b = a - b
>>> a = a - b
>>> a
9
>>> b
1
上面是转的,当时感觉思路挺不错。现在想想,在python中好像没必要这么麻烦:
>>>a =1
>>>b = 9
>>>a,b = b,a
>>>a
9
>>>b
1