0

I have a question regarding for loop working

m=10
for i in range(0,m):
    m=m-1
    print(i)

The ouput is :0,1,2,3,4,5,6,7,8,9

Why it is not 0,1,2,3,4 because we are updating m value by m-1

khelwood
  • 55,782
  • 14
  • 81
  • 108

4 Answers4

0

That's because python ints are immutable, meaning out cannot change the value of one.

Hence, you did not change the value of the previous variable, you simply created another variable that is also named m, also overwriting the previous one in the process.

Red
  • 26,798
  • 7
  • 36
  • 58
0

The number of times a for loop will run for, is already evaluated at the for loop itself, and the code after the for loop can't affect how many times it runs. It is fixed, until it is changed before the for loop runs, an error occurs, or a break is found within it.

0

The range type represents an immutable sequence of numbers. means once a range object is created like range(a,b) -> (a, a+1, a+2,...b) then if you do change the value of limits(start or end) it won't affect the for statement.

so if you want to change the for loop execution, you need iterator which produces a mutable instance.

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

I will give you a bit more detailed answer.

So first of all such problems starts when you start implementing Java, C# etc concepts in Python. It should be noted that Python is similar to other languages in some aspects and different in others.

For-loop of python is not same as java's. In java for loop is evaluated for the testing condition at each iteration, so when you increment or decrement a variable that is being used in loop evaluation condition, it affects the loop. For example:

int m=10;
for (int i = 0; i <= m; i = i + 1) { // on each iteration it is checked that `i` is less than or equal to `m`
   m = m-1;
   System.out.println(i);
}

So here your loop wont' run from i=0 to i=11, as you are decrementing m. You can visualize it some like this:

╔════════════╦═══╦════╦══════════════╗
║ Iteration# ║ i ║ m  ║  Loop Test   ║
╠════════════╬═══╬════╬══════════════╣
║          1 ║ 0 ║ 10 ║ 0<=10 : True ║
║          2 ║ 1 ║  9 ║ 1<=9 : True  ║
║          3 ║ 2 ║  8 ║ 2<=8 : True  ║
║          4 ║ 3 ║  7 ║ 3<=7 : True  ║
║          5 ║ 4 ║  6 ║ 4<=6 : True  ║
║          6 ║ 5 ║  5 ║ 5<=5 : True  ║
║          7 ║ 6 ║  4 ║ 6<=4 : False ║
╚════════════╩═══╩════╩══════════════╝

On each iteration i gets printed and on 7th loop test the condition gets false thus loop breaks.

Now come to Python(the great), you check into range(). You would think that if you do something like this: print(range(4))

You should will get this: [0, 1, 2, 3]

But actually you will get this: range(0, 4)

This is where you have to be pythonic. Once you tell it to start at 0 and stop at m it will return a reference to a generator object. So instead of creating a list object and populate the whole list with values (which is is very memory inefficient when it comes to large ranges), python will do lazy evaluation and will calculate the number on each iteration. So it doesn't matter if you change the value of m, the generator has already being created once the for-loop started.

However, the behavior of Java's for-loop can be implemented using while-loop in python.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26