[Tutor] Swapping values

yehudak . katye2007 at gmail.com
Mon Jan 4 19:37:27 EST 2016


In Python we can swap values between two variable a and b this way:

a = 3; b = 7
print(a, b)   # =====> 3  7

a, b = b, a  # swapping!
print(a, b)   # =====> 7  3

How does this work?

If I split the 'magic' line into:
a = b; b = a
without a temp variable I get:
print(a, b)   # =====> 7  7

Thank you,
Yehuda (Israel)


More information about the Tutor mailing list