+ 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]
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
+ 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
0
Thanks. I got it.