The for Statement

Sometimes, we may prefer to loop a specific number of times, rather than checking whether a condition is true or false.  Fortunately, we have the for statement which does exactly that. 

The for statement is another looping statement which iterates over a sequence of objects i.e. go through each item in a sequence. We will see more about sequences in detail in later lessons. What you need to know right now is that a sequence is just an ordered collection of items.

The for statement is considered to be a count-controlled statement.

The simplest form of the for statement is this:

for [variable] in [item1, item2, item3, etc]:    [do something]

Example: Countdown, Part 2

Here is how we can re-write the countdown program using a for statement:

for n in [5, 4, 3, 2, 1]:
    print(n)
    
print('Blastoff!')

Output

5
4
3
2
1
Blastoff!

How it Works

Code Output Explanation
for n in [5, 4, 3, 2, 1]: None

We begin the for statement by selecting a variable – and we can call it anything.  n is a traditional variable to use to represent a number, but we could really call it almost anything.

Then we create our list.  We separate each item using a comma and we enclose the entire list in bracket symbols [ ].

We then move on to execute the body.

    print(n)

5

The first time we go through the loop, the variable n represents the first element in the list.

We display the value of n.  And that’s it for the body of the loop.

for n in [5, 4, 3, 2, 1]:
    print(n)
4

The second time we go through the loop, the variable n represents the second element in the list.

We display the value of n.

for n in [5, 4, 3, 2, 1]:
    print(n)
3

The third time we go through the loop, the variable n represents the third element in the list.

We display the value of n.

for n in [5, 4, 3, 2, 1]:
    print(n)
2

The fourth time we go through the loop, the variable n represents the fourth element in the list.

We display the value of n.

for n in [5, 4, 3, 2, 1]:
    print(n)
1

The fifth time we go through the loop, the variable n represents the fifth element in the list.

We display the value of n.

We’re out of elements in the list now, so the for statement is finished and the program moves on.

print('Blastoff!') Blastoff!

We display the word Blastoff! The program is finished.

A list doesn’t have to contain numbers.  It can contain strings and other objects as well:

Code Output
for day in ['Mon', 'Tues', 'Weds']:
    print(day)
Mon
Tues
Weds
for stuff in [0, 'hi', 2.75, 'bye']:
    print(stuff)
0
hi
2.75
bye

We'll learn more about lists in a later lesson.

Launch Exercise

Launch Exercise

else: Alternative Execution

A for statement can also have an optional else clause.  This code will execute once the list is exhausted.  We can re-write the Countdown program using the else clause like this:

for n in [5, 4, 3, 2, 1]:
    print(n)
else:
    print('Blastoff!')

Again, we will get the exact same output.

Launch Exercise

Loop Patterns

Often we use a for or while loop to go through a list of items or the contents of a file and we are looking for something such as the largest or smallest value of the data we scan through.

These loops are generally constructed by:

  • Initializing one or more variables before the loop starts
  • Performing some computation on each item in the loop body, possibly changing the variables in the body of the loop
  • Looking at the resulting variables when the loop completes

We will use a list of numbers to demonstrate the concepts and construction of these loop patterns.