+ 2

How does this code work?

I saw it in Quiz Factory. a = [1,1,2,3,5,8] for a[1] in a: pass print(a) Output: [1,8,2,3,5,8]

6th Jan 2019, 5:11 PM
Nikolai Ivanov
Nikolai Ivanov - avatar
3 Answers
+ 8
"for var in iterable" assigns iterable's items to var in each iteration so "for a[1] in a" assigns a's items to a[1] If you add "print(a[1])" to loop body you will get this: 1 1 2 3 5 8
6th Jan 2019, 6:41 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
for loop is running over the items in list pass means do nothing in each iteration a[1] is manupilated because instead of usually for i in range, the quiz uses for a[1] in a, So after for loop, a[1] is as the ending state of a[5] which is 8
6th Jan 2019, 5:23 PM
Gordon
Gordon - avatar
0
Thanks. I got it.
8th Jan 2019, 6:07 AM
Nikolai Ivanov
Nikolai Ivanov - avatar