Skip to main content

Section 10.2 The for loop

The while statement is a general-purpose tool for iteration, and is necessary for any instance of iteration where we don’t know how many repetitions will be needed. However, if we do know how many are needed, there is a more efficient approach: the for statement.
As a simple example, let’s say we have some friends, and we’d like to send them each an email inviting them to our party. We don’t quite know how to send email yet, so for the moment we’ll just print a message for each friend.
Take a look at the output produced when you press the run button. There is one line printed for each friend. Here’s how it works:
  • name in this for statement is called the loop variable or, alternatively, the iterator variable.
  • The list of names in the square brackets is the sequence over which we will iterate.
  • Line 2 is the loop body. The loop body is always indented. The indentation determines exactly what statements are β€œin the loop”. The loop body is performed one time for each name in the list.
  • On each iteration or pass of the loop, first a check is done to see if there are still more items to be processed. If there are none left (this is called the terminating condition of the loop), the loop has finished. Program execution continues at the next statement after the loop body.
  • If there are items still to be processed, the loop variable is updated to refer to the next item in the list. This means, in this case, that the loop body is executed here 7 times, and each time name will refer to a different friend.
  • At the end of each execution of the body of the loop, Python returns to the for statement, to see if there are more items to be handled.
The overall syntax is for <loop_var_name> in <sequence>:
  • Between the words for and in, there must be a variable name for the loop variable. You can’t put a whole expression there.
  • A colon is required at the end of the line.
  • After the word in and before the colon is an expression that must evaluate to a sequence (e.g, a string or a list or a tuple). It could be a literal, or a variable name, or a more complex expression.

Note 10.2.1.

Introduction of the for statement causes us to think about the types of iteration we have seen. The for statement will always iterate through a sequence of values like the list of names for the party.
Since we know that it will iterate once for each value in the collection, it is often said that a for loop creates a definite iteration because we definitely know how many times we are going to iterate. On the other hand, the while statement is dependent on a condition that needs to evaluate to False in order for the loop to terminate. Since we do not necessarily know when this will happen, it creates what we call indefinite iteration. Indefinite iteration simply means that we don’t know how many times we will repeat but eventually the condition controlling the iteration will fail and the iteration will stop (unless we have an infinite loop).

Note 10.2.2. Choosing between for and while.

Use a for loop if you know the maximum number of times that you’ll need to execute the body. For example, if you’re traversing a list of elements, or know the number of times you’ll iterate, then choose the for loop.
So any problem like "iterate this weather model run for 1000 cycles", "search this list of words", or "check all integers up to 10000 to see which are prime" suggest that a for loop is best.
By contrast, if you are required to repeat some computation until some condition is met, as we did in the 3n + 1 problem, you’ll need a while loop.
What you will notice here is that the while loop is more work for you β€” the programmer β€” than the equivalent for loop. When using a while loop you have to control the loop variable yourself. You give it an initial value, test for completion, and then make sure you change something in the body so that the loop terminates.
Check your understanding

Checkpoint 10.2.3.

True or False: You can rewrite any for-loop as a while-loop.
  • True
  • The syntax for a for-loop can make it easier and more appealing, but a while loop is just as powerful as a for-loop and often more flexible.
  • False
  • Often a for-loop is more natural and convenient for a task, but that same task can always be expressed using a while loop.
You have attempted of activities on this page.