Changing the value of range during iteration in Python
>>> k = 8 >>> for i in range(k): print i k -= 3 print k Above the is the code which prints numbers from 0-7 if I use just print i in the for loop. I want to understand the above code how it is working, and is there any way we can update the value of variable used in range(variable) so it iterates differently. Also why it always iterates up to the initial k value, why the value doesn't updated. I know it's a silly question, but all ideas and comments are welcome. You can't change the range after it's been generated. In Python 2, range(k) will make a list of integers from 0 to k, like this: [0,