1


Shouldn't both blocks of code print similar results? Why is the range function inside of the inner loop reevaluated each time the inner for statement is reached while the range function in the outerloop is only evaluated once?

x = 4
for j in range(x)
   for i in range(x)
       print i
       x = 2

Results
0
1
2
3
0
1
0
1
0
1

I know the first 4 integers printed ( 0 - 3) are a result of the code for j in range(x): code but why are the the following also printed?
0
1
0
1
0
1
The code

x = 4
for j in range(x):
    print i
    x = 5

Prints
0
1
2
3

Additional Info Python 2.7 in IDLE

3
  • Is this really a practical, answerable question based on actual problems that you face? (faq) Commented Mar 28, 2013 at 16:38
  • 2
    @codesparkle Why not? Commented Mar 28, 2013 at 16:39
  • The code to the right of in (the range(x) call in this case) is evaluated each time the loop is entered from above. Therefore, the range in the outer loop only gets evaluated once, but in the inner loop it gets evaluated for each iteration of the outer loop. Think of range as returning a list, and what that list would hold, each time it is called. (In python2 it does return a list. In python3 it's more efficient, and returns a range type that's like an iterator.) Commented Nov 13, 2019 at 22:40

5 Answers 5

4

I can only explain by walking through the iterations of the loops, so here goes:

x = 4
for j in range(x)
   for i in range(x)
       print i
       x = 2

First time through.

x = 4
for j in [0, 1, 2, 3]
    for i in range [0, 1, 2, 3]
        print i
        x = 2

prints

0
1
2
3

Now x is set as 2, but the outer loops range has already been executed, so it is not reevaluated.

Code now becomes:

for j in [0, 1, 2, 3]:
    for i in [0, 1]:
        print i
        x = 2

prints

0
1

And this continues two more times.

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

Comments

4

Function range(x) produces a list of [0,1,2,3,4]. In for loop you iterate over this list.

Your code is equivalent to:

for j in [0,1,2,3]:
    for i in [0,1,2,3]:
        print i
    for i in [0,1]:
        print i
    for i in [0,1]:
        print i
    for i in [0,1]:
        print i

5 Comments

I'm just trying to get a better understanding of how for loop functions work. Can't seem to wrap my head around why the range function in the inner loop is reevaluated while the range function in the outer loops is only evaluated once
"range function in the inner loop" - as you noticed, it is in the inner loop, therefore it will be run in each iteration of the outer loop. What other behavior did you expect?
I expected the same results if I were to have ran the code <br> x = 4 for j in range(x): print j x = 5 How come this code only prints 0 1 2 3 instead of 0 1 2 3 0 1 2 3 4
The range function in the outer loop in your example is evaluated every time the outer for statement is reached. But that only happens once. Similarly in your example from the comment with a single loop: print j is reached multiple times, but for j in range(x) is only reached once.
The range is evaluated whenever entering the loop. The outer loop is entered once. The inner loop is entered once per outer loop iteration.
0

range(x) is evaluated only once i.e. when the loop begins, that is why modifying x inside the loop has no effect.

However, in the first code clock, you change x to 2 in the inner, so the next time the inner loop is executed, range only gives (0,1).

Edit: your first block of code is equivalent to

x = 5
for i in range(x)
    print i
    x = 2

Comments

0

Here is how I view it:

Inner loop is executed for each iteration of outer loop

x = 4
j = 0
for i in range (x):
    print(i)
    x = 2
j = 1
for i in range(x) # this x is no longer 4 but it is 2
    print(i)
    x = 2
j = 2
for i in range (x): # this x is, of course, 2
    print(i)
    x = 2
j = 3
for i in range (x): # this x is, of course, 2
    print(i)
    x = 2

Comments

-2

j is not printed, insert " print('xx', j, 'oo') " before " for i in range(x): "

then change to "print('xx', x, 'oo')

or change 'for j in range(x):' to 'for j in range(225,200,-5):' follow with 'print(j)' before 'for i in range(x):'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.