2

I am trying to figure what a iteration step precisely is.

wikipedia gives this explanation about iteration

Iteration in computing is the technique marking out of a block of statements within a computer program for a defined number of repetitions. That block of statements is said to be iterated; a computer scientist might also refer to that block of statements as an "iteration".

The python doc says

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) ...

Consider this example

#include <stdio.h>
int main()
{
   int i;
   for (i=1; i<=3; i++)
   {
       printf("%d\n", i);
   }
   return 0;
}

It is obvious that the total number of steps is equal to 3.

accordingly, the total number of steps is equal to 3 in the following code.

>>> mylist = [1, 2, 3]
>>> for i in mylist:
...    print(i**2)

this code print an i^2 at one iteration step, one iteration step is also called a iteration pass.

Is my understanding right?

  • 1
    *Python* *for* loop is a https://en.wikipedia.org/wiki/Foreach_loop. – CristiFati Sep 14 '19 at 15:45
  • 1
    _It is obvious that the iteration step is equal to 3_ no, the step is 1 in your C code: your are doing `i++`, which is the same as `i += 1` in this context, not as `i += 3`. – sanyassh Sep 14 '19 at 17:02

2 Answers2

1

In C, the iteration step is the third part of the for loop. In your example, it's i++. That code increments the value by one each time you go through the loop. Python doesn't have an equivalent concept baked into its for loops, because they iterate over iterable objects which are responsible for generating their own values however they want. Not all iterable objects have a meaningful concept of an iteration step in the C style.

That said, the most direct analogy to a classic C-style for loop in Python is a for loop on a range object. The range object does have a step, though it defaults to 1 and so you may not always see it specified explicitly. A needlessly verbose translation of your C loop to Python would be:

for i in range(1, 4, 1): # the 1 last 1 could be omitted, since it's the default
    ...

The iteration gives values to i starting at 1 on the first iteration (from the first 1 passed to range). It stops when the value would become (or passes) 4 (equivalent to the <= 3 condition in the C loop). The iteration step is 1, equivalent to i++.

Specifying the step is more useful when you're doing other odd kinds of looping, like counting down the even values from ten to zero:

for i in range(10, -1, -2): # 10, 8, 6, ..., 0
    ...

In C that would be:

for (i = 10; i > -1; i -= 2) {}
Blckknght
  • 100,903
  • 11
  • 120
  • 169
1

The step isn't an iteration concept in contrast of pass, it is related to a counter.

In your C example, the i is a counter. The starting value of the counter is 1. The step equals to 1 too. The halting condition says "if the counter is less or equal 3 - end loop". In this case the code inside for loop will be passed 3 times. Or by another words, it will have 3 iteration (repetition).

Take another case: for (i = 0; i <= 15; i = i + 3). Here, the step equals to 3, but the number of passes equals to 5. If the step will be 5, the number of passes will decrease and will be equal to 3.

So, the step is how fast the counter grows or decreases. The number of passes are resulting value, dependent from: counter, step, halting condition. Bear in mind, that each pass can be last, due to break, return statements or due to a run-time error.

The same logic can be applied to Python.

start = 0
end = 16
step = 3
for i in range(start, end, step):
    pass

Note the pass statement? It fits especially well to your question. Information about the pass: How to use the pass statement?

But in spite the same logic can be applied to Python, the for loop works in totally different way under the hood. If you interested, I advise you to watch CPython internals - Iterators - Lecture 7.

Now, consider your Python's example:

>>> mylist = [1, 2, 3]
>>> for i in mylist:
...    print(i**2)

The mylist is a sequence object and it has iterator object, which knows as this sequence should be processed. If you watch the video I gave above, you will understand what I am talking about.

Some iterator objects hides and maintains start, stop, step variables itself. There are others, that uses another data structures, linked list for example. They are determining the amount of iterations by another way. The passes haven’t gone anywhere though, the loop is repeated 3 times here, because mylist iterator returns 3 items.

Conclusion: No, the pass and step are different things in Python as well as in C. The step size determines the number of passes.

MiniMax
  • 983
  • 2
  • 8
  • 24