What’s happening here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What’s happening here?

a=[1,1,2,3,5,8] for a[1] in a: pass print(a) ————————- Result: [1,8,2,3,5,8]

20th Dec 2019, 4:09 PM
Eleri Lamp
Eleri Lamp - avatar
4 Answers
+ 1
Well technically nothing is happening since syntax and capitalization is incorrect a=[1,1,2,3,5,8] for a[1] in a: Pass print(a) And whats happening is its taking the second element of the list and replacing it with the entire list and printing that same list again but with the second element changed to one of the other elements in order from left to right.
20th Dec 2019, 4:32 PM
Creeper FRM
Creeper FRM - avatar
+ 1
The 2nd element in the list which is 1 is going to become an 8 thats whats going to happen because thats how for loops work its iterating through the a list to change the second element to the last element of the list
20th Dec 2019, 4:43 PM
Creeper FRM
Creeper FRM - avatar
0
only The capitalisation is incorrect as I typed it on my phone. Ive fixed it now. You changed the indentation which is not in my initial code. Sorry I don’t understand your explanation.
20th Dec 2019, 4:39 PM
Eleri Lamp
Eleri Lamp - avatar
0
ok thanks, i think i got it
20th Dec 2019, 7:39 PM
Eleri Lamp
Eleri Lamp - avatar