One of the for loops features in Python is that the iterated variable (counter) does not belong to the local scope of the loop.
For example, after executing the following code:
1 2 3 4 5 6 7 |
a = 0 print(a) b = [1,3,7] print(b) for a in b: pass print(a) |
The “a” variable declared before the for loop will change its value if the variable with the same name “a” declared as the loop counter.
1 2 3 4 |
# output > 0 # print(a) > [1, 3, 7] # print(b) > 7 # print(a) after loop execution |
It is necessary to remember about this feature, in order to avoid writing to the previously declared variable the values of the iterator of the for loop.