Here is a snippet of code which gives the output: 0 1 2 2
. I had expected the output 3 3 3 3
since a[-1]
accesses the number 3 in the list. The explanation given online says "The value of a[-1]
changes in each iteration" but I don't quite understand how or why. Any explanations would be great!
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1])
a[-1]
even allowed as the loop variable... - timgeb