Why doesn't Python for a loop work like a C loop?

FROM

# 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 the C code, it goes into an infinite loop, whereas in Python this is not so, why not?

Python output:

0 1 2 3 4 5 5 6 7 8

+6
source share
4 answers

In Python, a loop does not increment i ; instead, it assigns values ​​from an iterable object (in this case, a list) to it. Therefore, changing i inside the for loop does not "confuse" the loop, because in the next iteration i next value will simply be assigned.

In the code you indicated when i is 6, it then decreases in the loop, so that it changes to 5 and then prints. 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 ends when there are no more values.

Of course, the effect you get in the C loop that you provided can still be achieved in Python. Since each for loop is an illustrious while loop, in the sense that it can be transformed as follows:

 for (init; condition; term) ... 

It is equivalent to:

 init while(condition) { ... term } 

Then your infinite loop can be written in Python as:

 i = 0 while i < 10: if i > 5: i -= 1 print i i += 1 
+25
source

Two constructs are called for loops, but in reality it is not the same thing.

The Python version is indeed a foreach loop. It runs once for each item in the collection. range(10) creates a list like [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] So, the for loop starts once for each member of the collection. It does not use the value of i to determine that the next item, it always takes the next item in the list.

The c for loop translates to the equivalent

 int i = 0 while i < 10: ... i++; 

That is why you can manipulate i .

+6
source

Because your two examples are completely different things.

range(10) in python returns a list of values ​​0 - 9, then for i in returns each value as i . This is commonly called the "for everyone" cycle. You use a value, not an iterator, when you say i=i-1 in your python example.

+4
source

http://docs.python.org/tutorial/controlflow.html

The for statement in Python is slightly different from what you can use in C or Pascal. Instead of always repeating the arithmetic progression of numbers (for example, in Pascal) or giving the user the ability to determine both the iteration step and the stop condition (like C), the Pythons operator to iterate over elements of any sequence (list or line), in the order they are occurrences in sequence. For example (no pun intended):

 a = ['cat', 'window', 'defenestrate'] for x in a: print x, len(x) 

It is incorrect to change a sequence repeated in a loop (this can happen only for mutable types of sequences, such as lists). If you need to change the list that you are repeating (for example, to duplicate selected items), you must iterate over the copy.

Of course, the sequence generated by the range is not changed.

+1
source

Source: https://habr.com/ru/post/906552/


All Articles