-1

Lets take a standard Java for-loop

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

I can see exactly what's happening in this for loop. i is initialized to 0. The condition is checked and the increment takes place, every iteration.

Here's the Python version

for i in range(0, 10):
    print(i)

What's happening in detail during this loop?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • So you're asking how Python is implemented. – Maroun Apr 19 '15 at 08:21
  • Yeah, I'm asking how python is implemented. – Ogen Apr 19 '15 at 08:21
  • possible duplicate of [Understanding Python's iterator, iterable, and iteration protocols -- what exactly are they?](http://stackoverflow.com/questions/9884132/understanding-pythons-iterator-iterable-and-iteration-protocols-what-exact) – Preet Kukreti Apr 19 '15 at 08:38

3 Answers3

3

The misconception is that these two loops are not similar.

Java's form is starting from some initial value, printing it out, and so long as the value is less than some terminal value, it will continue the loop.

Python is actually taking each element contained in whatever iterable you give it, and printing out the contents of it without any incrementation whatsoever.

Since in this case, the iterable is the result of range, the behavior of how you get the elements changes between versions.

  • If you're using Python < 3, range is a function that eagerly generates a list of elements for you to use. The lazy, generated variant of this is xrange, in which the values are generated as needed. In this scenario, since you're looping to completion over the entire collection, and the memory constraints aren't that high, you won't notice any difference between the two.

  • In Python >= 3, range behaves similar to xrange in that it's another sequence type, which also generates the values that it requires on the fly.

The key difference here is that the variable in a Python loop represents the actual value contained in the iterable, whereas Java is generating the value with a standard for loop.

If you were to use an enhanced-for loop instead, then you'd get closer to how Python's loops work:

int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9}
for(int i : list) {
    System.out.println(i);
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Taking a look at the documentation for the range() built-in function, it is possible to see that this function "create lists containing arithmetic progressions". This list is the iterated over as if it was passed directly to the loop:

for i in range(0, 10):
    print(i)

This code is essentially the same as

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(i)

The in keyword is basically saying for each element in the supplied iteratable object - in this case, a list. "For each item in the list". The loop knows when to stop because the range function generates a finite list - this list has it's length and that is the stop condition. Internally, when the loop attempts to read an index that is out of range, a StopIteration exception is generated and will trigger the termination condition.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • What exactly is the `in` keyword doing. How does this for loop know when to stop? – Ogen Apr 19 '15 at 08:23
  • 1
    @Ogen - the loop knows when to stop because the range function generates a finite list - this list has it's length and that is the stop condition. Internally, when the loop attempts to read an index that is out of range, a `StopIteration` exception is generated and will trigger the termination condition. – Lix Apr 19 '15 at 08:24
  • 1
    @Ogen The `in` keyword is basically saying for each element **in** the supplied iteratable object - in this case, a list. "For each item **in** the list". – Lix Apr 19 '15 at 08:25
  • I see, so rather than checking a condition like in java, python checks an exception? – Ogen Apr 19 '15 at 08:26
  • You *could* look at it like that, but you rarely have to deal with the actual exception - as I said, this is (how I assume) it is handled internally. We as users just have to utilize the syntax of the loop and it will stop accordingly. – Lix Apr 19 '15 at 08:27
  • I would go as far as to say there is no *real* difference other than the syntax - you don't explicitly have to give the stop condition if it can be implied by the fact that you are iterating over a finite list. – Lix Apr 19 '15 at 08:31
1

range(0,10) returns a list of the numbers from 0 to 9. The loop loops over this list, each iteration assigning the next number in the list to i

Morad
  • 2,761
  • 21
  • 29