-5

I am new to Python but I am still stuck on how Python call and iterate for loop, showing these with different output and I read the Docs of how it works but got me confused, if I can get help here with out ban I'll appreciate it.strong text

i = 2
j = 1
x = 5
for j in range(x):
    print (i)
    i+=1

Output:
2, 3 , 4 , 5 , 6

i = 2
j = 1
x = 5
for i in range(x):
    print (i)
    i+=1

Output:
0, 1, 2, 3 , 4

i = 2
j = 1
x = 5
for i in range(x):
    print (i)
    x+=1

Output:
0, 1, 2, 3 , 4

i = 2
j = 1
x = 5
for j in range(x):
    print (i)
    i+=1

Output:
2, 3 , 4 , 5 , 6

Unfortunately, I checked these question 1, 2 and did not help me my question is why every loop give different output

Ahmad
  • 1,618
  • 5
  • 24
  • 46
Mike
  • 1
  • 5

2 Answers2

1
i = 2
j = 1
x = 5
for j in range(x):
    print (i)
    i+=1

j has been set to 2, but is overwritten as the iterator element in the loop. j will then iterate over the values [0, 1, 2, 3, 4] as this is what range returns. i starts at 1 and is incremented on each loop.


i = 2
j = 1
x = 5
for i in range(x):
    print (i)
    i+=1

Now i is being overwritten by the loop, being set to the same range. This then prints out what you get from range


i = 2
j = 1
x = 5
for i in range(x):
    print (i)
    x+=1

When range is called, the list [0, 1, 2, 3, 4] to iterate over is already created, incrementing x will do nothing to i so the output is the same as the last case.


The last case is the same as the first. I hope this helps you see what's going on, but in the future you will get more help on SO if you talk more about what you think is happening and what specific things you are stuck on.

ufoxDan
  • 609
  • 4
  • 13
0

What happens actually is this:

  • Using range() method and i as the index does not combine in your first loop, the range() is 5 elements so it starts printing i which was 2 and increases one and repeats this till finish the range.
  • In the second loop, you used i as an index so now we have two variable with the same name; i
    • the first one declared with value 2
    • the other created after for and there is only one i in this scope and the loop iterator overwrites the first one. print(i) after the loop is done you can see it is still 4 after the loop finishes. so it takes the created inside
  • in loop 3 what is happening is the same as above but changing the value (increase or decrease) that used in range() will not change it after irritating because the method was called and set with the variable, and everything is happening inside it, so you are increasing x but will not effect until you print x like this
  j = 1
  x = 5
  for i in range(x):
      print (i)
      x+=1
  print (x)
  • loop 4 used i you declared earlier so it working normally, now note that if you printed the i from the loop 2 you will get 5 and that because Python is printing what used lately and here you know that you should do this not like languages like Java and C#
Ahmad
  • 1,618
  • 5
  • 24
  • 46
  • 2
    When you say "you have two i" that is incorrect. There is only one `i` in this scope and the loop iterator overwrites the first one. `print(i)` after the loop is done and you can see it is still 4 after the loop finishes. – ufoxDan Nov 14 '19 at 20:38
  • That's what I meant but typed in the wrong way; please remove the down-vote @ufoxDan – Ahmad Nov 15 '19 at 11:59