for i in range(10):
i += 1
print(i)
is equivalent to
iterator = iter(range(10))
try:
while True:
i = next(iterator)
i += 1
print(i)
except StopIteration:
pass
The iterator
that iter(range(10))
produces will yield values 0
, 1
, 2
... 8
and 9
each time next
is called with it, then raise StopIteration
on the 11th call.
Thus, you can see that i
gets overwritten in each iteration with a new value from the range(10)
, and not incremented as one would see in e.g. C-style for
loop.