The for loop counter in Python does not have a local scope

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:

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.

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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Комментарий
Inline Feedbacks
View all comments