3

Beginner here, trying to understand for loops using Python3. Can someone please explain in the following code why outside of the for loop returns a value of 5? Does Python just return the max number if a variable is called outside of the for loop?

for x in range(0,6):
    print(x) #here is the expected output of (0,1,2,3,4,5)

print(x) #this just returns 5, why?
5
  • Python retains the last value of a variable used in a loop. Try to play around with the values inside range. Commented Aug 2, 2015 at 22:17
  • Nice question! I thought this would have given you an error, because x should be a local variable to the loop, but apparently it is not garbage collected immediately. Commented Aug 2, 2015 at 22:18
  • 1
    @nbro for loops don’t create a new scope. This has nothing to do with garbage collection, the variable is defined for the whole namespace (functions and modules). See also this answer. Commented Aug 2, 2015 at 22:34
  • Does this answer your question? Short description of the scoping rules? Commented Sep 12, 2022 at 11:16
  • "Does Python just return the max number if a variable is called outside of the for loop?" It retains the last value, because nothing happened that could have changed the value. The purpose of names is to remember and refer to stuff. If the name is expected to be usable at all, then there is no other candidate for what it could be naming. So the only remaining question is "why is the name still in use after the for loop?" and answer is that for loops in Python do not create a scope. Commented Sep 12, 2022 at 11:18

1 Answer 1

2

As per the language reference (emphasis mine):

The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop:

[…]

Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop.

So while you are iterating over the range of numbers from 0 to 6, those values are assigned to x in each iteration. So in the first iteration, you get x = 0, in the second it’s x = 1, and so on until x = 5 (which is the last number that’s in the range).

Then when the for loop ends, as specified, the variable x is not deleted, so x is still around and still has that value 5 that it was assigned to in the final iteration.


Finally, since you seemed surprised that x is just 5 in the end (and not 0, 1, 2, 3, 4, 5), note that in each iteration, x has only a single value. The print(x) inside the for loop is just executed 6 times with x having a different value each time. That does not result in the output you stated though; it’s still one number per line. Your code is essentially the same as the following without loops:

# the for loop does this:
x = 0
print(x)
x = 1
print(x)
x = 2
print(x)
x = 3
print(x)
x = 4
print(x)
x = 5
print(x)

# and this follows the loop:
print(x)
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent explanation poke. Very appreciated, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.