3.1 The for Loop
61
This means that if we had, e.g., switched the print command inside our loop with
print(’Hello!’) (i.e., so that i does not appear explicitly within the loop), i
would still run through the numbers 1 to 10 as before, but Hello! would be printed
10 times instead.
The loop variable i takes on the values 1 to 10 in the order listed, and any order
would be acceptable to Python. Thus, if we (for some reason) would like to reverse
the order of the printouts, we could simply reverse the list of numbers, writing [10,
9, 8, 7, 6, 5, 4, 3, 2, 1] instead.
It should be noted that the loop variable is not restricted to run over integers. Our
next example includes looping also over floating point values.
Indentation and Nested Loops In our simple times table example above, the print
command inside the loop was indented 4 spaces, which is in accordance with the
official style guide of Python. 2
Strictly speaking, the style guide recommends an indent of 4 spaces per
indentation level. What this means, should become clear if we demonstrate how
a for loop may appear within another for loop, i.e., if we show an arrangement
with nested loops.
for i in [1, 2, 3]:
# First indentation level (4 spaces)
print(’i = {:d}’.format(i))
for j in [4.0, 5.0, 6.0]:
# Second indentation level (4+4 spaces)
print(’
j = {:.1f}’.format(j))
# First line AFTER loop over j
# First line AFTER loop over i
The meaning of indentation levels should be clear from the comments (see code),
and it is straight forward to use more nested loops than shown here (see, e.g.,
Exercise 5.7). Note that, together with the colon, indenting is part of the syntax
also for other basic programming constructions in Python (e.g., in if-elif-else
constructions and functions).
When executing the nested loop construction, we get this printout:
i = 1
j = 4.0
j = 5.0
j = 6.0
i = 2
j = 4.0
j = 5.0
j = 6.0
i = 3
j = 4.0
j = 5.0
j = 6.0
From the printout, we may infer how the execution proceeds. For each value of
i, the loop over j runs through all its values before i is updated (or the loop is
terminated). To test your understanding of nested loops, you are recommended to
do Exercise 5.1.
2 https://www.python.org/dev/peps/pep-0008/.
61
This means that if we had, e.g., switched the print command inside our loop with
print(’Hello!’) (i.e., so that i does not appear explicitly within the loop), i
would still run through the numbers 1 to 10 as before, but Hello! would be printed
10 times instead.
The loop variable i takes on the values 1 to 10 in the order listed, and any order
would be acceptable to Python. Thus, if we (for some reason) would like to reverse
the order of the printouts, we could simply reverse the list of numbers, writing [10,
9, 8, 7, 6, 5, 4, 3, 2, 1] instead.
It should be noted that the loop variable is not restricted to run over integers. Our
next example includes looping also over floating point values.
Indentation and Nested Loops In our simple times table example above, the print
command inside the loop was indented 4 spaces, which is in accordance with the
official style guide of Python. 2
Strictly speaking, the style guide recommends an indent of 4 spaces per
indentation level. What this means, should become clear if we demonstrate how
a for loop may appear within another for loop, i.e., if we show an arrangement
with nested loops.
for i in [1, 2, 3]:
# First indentation level (4 spaces)
print(’i = {:d}’.format(i))
for j in [4.0, 5.0, 6.0]:
# Second indentation level (4+4 spaces)
print(’
j = {:.1f}’.format(j))
# First line AFTER loop over j
# First line AFTER loop over i
The meaning of indentation levels should be clear from the comments (see code),
and it is straight forward to use more nested loops than shown here (see, e.g.,
Exercise 5.7). Note that, together with the colon, indenting is part of the syntax
also for other basic programming constructions in Python (e.g., in if-elif-else
constructions and functions).
When executing the nested loop construction, we get this printout:
i = 1
j = 4.0
j = 5.0
j = 6.0
i = 2
j = 4.0
j = 5.0
j = 6.0
i = 3
j = 4.0
j = 5.0
j = 6.0
From the printout, we may infer how the execution proceeds. For each value of
i, the loop over j runs through all its values before i is updated (or the loop is
terminated). To test your understanding of nested loops, you are recommended to
do Exercise 5.1.
2 https://www.python.org/dev/peps/pep-0008/.
