4

I have tried this code :

for i in range(10)
    print(line, i)

print(line, i)

and the program executed without error, so why is i declared even after the for statement, it must no longer exist.

2

2 Answers 2

2

Yes, your iteration variable isn’t deleted when the loop is finished. As the documentation puts it: „Names in the target list are not deleted when the loop is finished”.

This has to do with variable scopes. As has been pointed out, the variable i exists within the scope of the function you’re in. A loop does not create an extra scope in python.

Sign up to request clarification or add additional context in comments.

Comments

0

The scope is within a function, not a loop. A little different than other programming languages.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.