Iterative Processing: The for Statement

The simplest for statement looks like this:

for variable in sequence : suite

The suite is an indented block of statements. Any statement is allowed in the block, including indented for statements.

The variable is a variable name. The suite will be executed iteratively with the variable set to each of the values in the given sequence. Typically, the suite will use the variable, expecting it to have a distinct value on each pass.

There are a number of ways of creating the necessary sequence of values. The most common is to use the range function to generate a suitable list. We can also create the list manually, using a sequence display; we'll show some examples here. We'll return to the details of sequences in Chapter 11, Sequences: Strings, Tuples and Lists.

The range function has 3 forms:

A sequence display looks like this: [ expression, ... ]. It's a list of expressions, usually simply numbers, separated by commas. The square brackets are essential for marking a sequence.

This first example creates a sequence of 6 values from 0 to just before 6. The for statement iterates through the sequence, assigning each value to the local variable i. The print statement has an expression that adds one to i and prints the resulting value. Note that the suite of statements in the body of the for statement is simply a print statement, so we can combine it all on one line.

for i in range(6): print i+1

The second example creates a sequence of 6 values from 1 to just before 7. The for statement iterates through the sequence, assigning each value to the local variable j. The print statement prints the value.

for j in range(1,7):
    print j

This example creates a sequence of 36/2=18 values from 1 to just before 36 stepping by 2. This will be a list of odd values from 1 to 35. The for statement iterates through the sequence, assigning each value to the local variable o. The print statement prints all 18 values.

for o in range(1,36,2): 
    print o

This example uses an explicit sequence of values. These are all of the red numbers on a standard roulette wheel. It then iterates through the sequence, assigning each value to the local variable r. The print statement prints all 18 values followed by the word "red".

for r in [1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36]: 
    print r, "red"

Here's a more complex example, showing nested for statements. This enumerates all the 36 outcomes of rolling two dice. The outer for statement creates a sequence of 6 values, and iterates through the sequence, assigning each value to the local variable d1. For each value of d1, the inner loop creates a sequence of 6 values, and iterates through that sequence, assigning each value to d2. The print statement will be executed 36 times.

for d1 in range(6):
    for d2 in range(6):
        print d1+1,d2+1,'=',d1+d2+2

Here's the example alluded to earlier, which does 100 simulations of rolling two dice. The for statement creates the sequence of 100 values, assigns each value to the local variable i; note that the suite of statements never actually uses the value of i. The value of i marks the state changes until the loop is complete, but isn't used for anything else.

import random
for i in range(100):
    d1= random.randrange(6)+1
    d2= random.randrange(6)+1
    print d1+d2

There are a number of more advanced forms of the for statement, which we'll cover in the section on sequences in Chapter 11, Sequences: Strings, Tuples and Lists.