In this post, the following code snippets could work.
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1])
Refer to this answer
While doing for a[-1] in a
, you actually iterate through the list and temporary store the value of the current element into a[-1]
.
Likewise, I think doing for a in a
, it should iterate through the list and temporary store the value of current element to a
, so the value of a
could be 0
, and is not iterable, then TypeError
exception will be thrown in the next iteration. However, the result is as below.
>>> a = [0, 1, 2, 3]
>>> for a in a:
... print a
...
0
1
2
3
>>> a
3
How to understand it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…