what is the difference | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the difference

a, b = b, a Is it equal to the : a = b b = a ?

5th Jun 2020, 4:56 PM
shiryaeva
shiryaeva - avatar
3 Answers
+ 2
Well, Lets understand it with the help of an example: Case 1- # a,b = b,a # means essentially the following: a=2 b=5 temp=a a=b b= temp print('a:', a) print('b:',b) >>> a: 5 >>> b: 2 Case 2- # What you wrote second a=2 b=5 a=b b=a print('a:', a) print('b:',b) >>> a: 5 >>> b: 5
5th Jun 2020, 5:13 PM
Manish Kumar Mohanty
Manish Kumar Mohanty - avatar
+ 1
a, b = b, a means you are switching the values of a and b. a = 2 b = 5 a, b = b, a print(a) # 5 print(b) # 2 a = b or b = a means you will set them to be the same.
5th Jun 2020, 4:59 PM
Russ
Russ - avatar
+ 1
It is, if understood as done *simultaneously*. If in iterative way, it is rather equivalent to: a = a + b b = a - b a = a - b
5th Jun 2020, 5:00 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar