Reputation: 4753
C:
# include <stdio.h>
main()
{
int i;
for (i=0; i<10; i++)
{
if (i>5)
{
i=i-1;
printf("%d",i);
}
}
}
Python:
for i in range(10):
if i>5: i=i-1
print(i)
When we compile C code, it goes into a infinite loop printing 5 forever, whereas in Python it doesn't, why not?
The Python output is:
0 1 2 3 4 5 5 6 7 8
Upvotes: 6
Views: 7565
Reputation: 76908
Because your two examples are completely different things.
range(10)
in python produces a list of values 0 - 9, then for i in
returns each value as i
. This is generally referred to as a "for-each" loop. You are operating on a value, not the iterator, when you say i=i-1
in your python example.
Upvotes: 4
Reputation: 8437
In Python, the loop does not increment i
, instead it assigns it values from the iterable object (in this case, list). Therefore, changing i
inside the for loop does not "confuse" the loop, since in the next iteration i
will simply be assigned the next value.
In the code you provided, when i
is 6, it is then decremented in the loop so that it is changed to 5 and then printed. In the next iteration, Python simply sets it to the next value in the list [0,1,2,3,4,5,6,7,8,9]
, which is 7, and so on. The loop terminates when there are no more values to take.
Of course, the effect you get in the C loop you provided could still be achieved in Python. Since every for loop is a glorified while loop, in the sense that it could be converted like this:
for (init; condition; term) ...
Is equivalent to:
init
while(condition) {
...
term
}
Then your for infinite loop could be written in Python as:
i = 0
while i < 10:
if i > 5:
i -= 1
print i
i += 1
Upvotes: 29
Reputation: 45059
The two constructs are both called for loops but they really aren't the same thing.
Python's version is really a foreach loop. It runs once for each element in a collection.
range(10)
produces a list like [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
So the for loop runs once for each member of the collection. It doesn't use the value of i
in deciding what the next element is, it always takes the next element in the list.
The c for loop gets translated into the equivalent of
int i = 0
while i < 10:
...
i++;
Which is why you can manipulate the i
.
Upvotes: 6
Reputation: 11935
http://docs.python.org/tutorial/controlflow.html
The for
statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy.
Of course the sequence generated by range is not mutable.
Upvotes: 1