0

a, b = b, a?

a =5 b = 6 a, b = b, a print(a) Solution: 6 5, 6 = 6, 5 # i think this is what python is thinking print (a) #a=5. Print 5. So, why did it print six? At whatpoint did a and b switch values? Also, in prior posts people have pointed out the difference between having the same value (5 and 6 here) but be different objects. So super confused here. Thanks for any help:)

3rd Apr 2019, 2:09 AM
tristach605
tristach605 - avatar
4 Answers
+ 7
a, b = b, a Basically, this is a way of swapping values of variables. a will be assigned value of b and b will be assigned the value of a. Basically, this doesn't work with integers as they have fixed value. They can't change their value.
3rd Apr 2019, 2:48 AM
Arushi Singhania
Arushi Singhania - avatar
+ 3
This thing means swapping the values of the variables.this is not comparing the values of the variables.remember that when we compare values we use double == and when we assign the values to the variable we use single =. a=5 b=6 a,b=b,a Now a become b and b become a.
3rd Apr 2019, 6:08 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
a = 5 b = 6 a,b = b,a means (a,b) =(6,5)
4th Apr 2019, 1:39 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 1
Thanks everyone:)!
3rd Apr 2019, 10:32 PM
tristach605
tristach605 - avatar