How does Python iterate a for loop?

Viewed 269

I tried the following code on Python, and this is what I got: It seems like for many changes I try to make to the iterables by changing elem, it doesn't work.

lis = [1,2,3,4,5]
for elem in lis:
    elem = 3

print lis
[1, 2, 3, 4, 5]

However if the iterables are objects with its own methods (like a list), they can be modified in a for loop.

lis = [[1],[2]]
for elem in lis:
    elem.append(8)

print lis
    [[1, 8], [2, 8]]

In the for loop what exactly is the 'elem' term? Thanks in advance!

6 Answers
Related