+ 2

Python

a = 5 b = 7 c=b=a print(b) Can anyone explain whats happening there and why b value is 5 but not 7 ?

31st Aug 2020, 6:19 AM
Ramana Sai
Ramana Sai - avatar
3 Answers
+ 8
Ramana Sai See the thing is The value of a is 5 The value of b is 7 The value of c is first assigned to the value of b that is 7 ok? After that again the value of b is assigned to a that is 5 that's the only reason the answer is 5 Lemme explain you in more comfortable language. A=5 B=7 C=b (which is 7)=a(now the value of b is changed to the value of a which is 5) So the answer is 5 Thanks Hope this helps đŸ€Ÿ
31st Aug 2020, 6:32 AM
Piyush
Piyush - avatar
+ 2
It is so simple.Firstly assign c to b so c = 7 b = 7 then assign b to a will gives b = 5. In summary: c = b = a => b = a (5) and c = b (5)
31st Aug 2020, 6:27 AM
HBhZ_C
HBhZ_C - avatar
+ 1
REASON Assignment operator (=) has right to left Associativity. DHANANJAY PATEL
1st Sep 2020, 3:45 PM
THANK YOU
THANK YOU - avatar