Operatorfor item in list
assigns to each element from list
to item
every iteration. When you enter A[-1]
instead item
, it assigns the item A[-1]
and A
changes the list.
, for loop
, , print
.
A = [1,2,3,40,50]
for x in A:
A[-1] = x
print(A[-1])
print(A)
:
1
[1, 2, 3, 40, 1]
2
[1, 2, 3, 40, 2]
3
[1, 2, 3, 40, 3]
40
[1, 2, 3, 40, 40]
40
[1, 2, 3, 40, 40]
, A . . (40
) .