60
3 Loops and Branching
When executed, the 10 results are printed quite nicely as
1*5 = 5
2*5 = 10
...
...
With a for loop, however, the very same printout may be produced by just two (!)
lines of code:
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
# Note... for, in and colon
print(’{:d}*5 = {:d}’.format(i, i*5))
# Note indent
With this construction, the loop variable i takes on each of the values 1 to 10, and
for each value, the print function is called.
Since the numbers 1 to 10 appear in square brackets, they constitute a special
structure called a list. The loop here would work equally well if the brackets had
been dropped, but then the numbers would be a tuple:
for i in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10:
# no brackets...
print(’{:d}*5 = {:d}’.format(i, i*5))
Both lists and tuples have certain properties, which we will come back to in Sect. 5.1.
3.1.2 Characteristics of a Typical for Loop
Loop Structure There are different ways to write for loops, but herein, they are
typically structured as
for loop_variable in some_numbers:
# Loop header
3 Loops and Branching
When executed, the 10 results are printed quite nicely as
1*5 = 5
2*5 = 10
...
...
With a for loop, however, the very same printout may be produced by just two (!)
lines of code:
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
# Note... for, in and colon
print(’{:d}*5 = {:d}’.format(i, i*5))
# Note indent
With this construction, the loop variable i takes on each of the values 1 to 10, and
for each value, the print function is called.
Since the numbers 1 to 10 appear in square brackets, they constitute a special
structure called a list. The loop here would work equally well if the brackets had
been dropped, but then the numbers would be a tuple:
for i in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10:
# no brackets...
print(’{:d}*5 = {:d}’.format(i, i*5))
Both lists and tuples have certain properties, which we will come back to in Sect. 5.1.
3.1.2 Characteristics of a Typical for Loop
Loop Structure There are different ways to write for loops, but herein, they are
typically structured as
for loop_variable in some_numbers:
# Loop header
# 1st line in loop body
# 2nd line in loop body
...
...
# last line in loop body
# First line after the loop
where loop_variable runs through the numbers 1 given by some_numbers. In the
very first line, called the for loop header, there are two reserved words, for and
in. They are compulsory, as is the colon at the end. Also, the block of code lines
inside a loop must be indented. These indented lines are referred to as the loop body.
Once the indent is reversed, we are outside (and after) the loop (as commented with
# First line after the loop, see code). One run-through of the loop body is
called an iteration, i.e., in our example above with the 5 times table, the loop will
do 10 iterations.
Loop Variable The name picked for the loop_variable is up to the programmer.
In our times table example, the loop variable i appeared explicitly in the print
command within the loop. Generally, however, the loop variable is not required to
enter in any of the code lines within the loop, it is just available if you need it.
1 In Python, the loop variable does not have to be a number. For example, a header like for name
in [‘John’, ‘Paul’, ‘George’, ‘Ringo’]: is fine, causing the loop variable to be a name.
If you place print(name) inside the loop and run it, you get each of the names printed. In this
book, however, our focus will be loops with numbers. 