+ 3

Don’t understand the logic

UPD: Solved, great thanks for responses. Hello. Here is the code: x=[4,3,1,2] x[0],x[x[0]-1]=x[x[0]-1],x[0] print(x) Output is [2, 4, 1, 2] I don’t understand why it is not [2, 3, 1, 4]. Because it seems to me that second line means x[0] (first member in a list, i.e 4) and x[x[0]-1] ( i think it is x[4-1] i.e. x[3] i.e 2) should switch places. Please help

19th Apr 2022, 10:05 PM
Anna Mitrofanova
2 odpowiedzi
+ 4
This is called parallel reassignment ☺️ For ease of understanding, you should be aware that the right side of the expression remains unchanged, while the left side of the expression is interactive (that is, it changes instantly): x[0] = x[4-1] = 2 and x[2-1] = 4 😉
19th Apr 2022, 11:10 PM
Solo
Solo - avatar