0

Python challenge question

In a py challenge, x = 5 x, x = x+1, x+2 print (x) //Outputs 7. Why not 8?? I think I should clarify it a bit. The result of above written code is 7 and not 8. I want to know why it outputs 7 and not 8 ???

23rd Feb 2021, 4:54 AM
CHANDAN ROY
CHANDAN ROY - avatar
6 Answers
+ 2
Because right side (tuple) calculates first x, x = 6, 7 and then tuple unpacking
23rd Feb 2021, 4:58 AM
Nikolai Ivanov
Nikolai Ivanov - avatar
+ 1
I'm very sure Google is hunting out the quiz maker right now for interviews ... The first question of the interview will be ... "What did you have for breakfast ...eggs or spam?" Sorry Roy, just couldn't help wonder where such riddle educate us in coding đŸ€—
23rd Feb 2021, 5:08 AM
Ipang
+ 1
Oh it was my mistake. So the correct answer is that ... In python, tuples assign the values in the right to the left. In simple words, when u wrote the code: x,x = x+1, x+2 Python at first evaluated x+1 and x+2. So the code now is: x,x = 6,7 And now since python is an interpreted language it first took the value of x as 6 and then changed it to 7 .. Hope I'm correct😅
23rd Feb 2021, 3:30 PM
Amrit Mishra
Amrit Mishra - avatar
0
∆BH∆Y I think you misunderstood my question. I have edited it a bit for clarity. Kindly go through it
23rd Feb 2021, 8:01 AM
CHANDAN ROY
CHANDAN ROY - avatar
0
Because the newest value of variable iterates the old value in Python.
23rd Feb 2021, 2:41 PM
Amrit Mishra
Amrit Mishra - avatar
0
Amrit Mishra Can you please elaborate on what you just said. Please.
23rd Feb 2021, 2:44 PM
CHANDAN ROY
CHANDAN ROY - avatar