+ 1

Python variable swapping

Consider this code snippets: a = 5 b = 4 a, b = b, a In this code, why isn't b equal to the new, modified value of a (i.e., b itself), and instead holds the old value of a? What exactly happens when doing this? Does Python create a copy of the variables in the right-hand side, and use the new copy holding the old values to assign the variables in the left-hand side?

1st Jul 2021, 2:20 PM
Calvin Thomas
Calvin Thomas - avatar
2 Answers
+ 1
On the right side in the 3rd line a tuple is created which has its own memory address.
1st Jul 2021, 2:56 PM
Lisa
Lisa - avatar
0
Runtime Terror and Lisa Thank you very much. Is this also applicable to objects?
1st Jul 2021, 4:32 PM
Calvin Thomas
Calvin Thomas - avatar