For loops
Contents
For loops#
Without us telling it otherwise, Python will execute code within a cell from top to bottom, with each line being run once. However, most of the important tasks that computers are used for involve repeating steps a large number of times. A computer’s ability to repeat steps quickly is one of the main things that make them so useful. We can have Python repeat lines by simply copy and pasting them:
from mobilechelonian import Turtle
tina = Turtle()
# make tina go a bit faster for convenience
tina.speed(5)
# draw a square by repeatedly moving forward then turning left 90 degrees
tina.forward(60)
tina.left(90)
tina.forward(60)
tina.left(90)
tina.forward(60)
tina.left(90)
tina.forward(60)
tina.left(90)
Doing this has many problems; for example, it is prone to mistakes, difficult to read, and difficult to modify (imagine we realised we wanted a hexagon instead). A for
loop makes the code much tidier:
raph = Turtle()
# make tina go a bit faster for convenience
raph.speed(5)
# To repeat something 4 times, we use the following syntax
for i in range(4):
# these indented lines will be repeated 4 times
raph.forward(60)
raph.left(90)
# To create any other regular polygon, you would only need to change two numbers
Let’s break down the code above. When we want Python to repeat a process a certain number of times, or once for each object in some sequence, we typically use a for
loop. The alternative is to loop until some condition is satisfied, which we will see in a later notebook on while
loops. The for
loop has the basic syntax
for x in some_sequence:
indented body statements to be repeated, possibly making use of x
next thing to do, not repeated
In the example above, the some_sequence
is range(4)
, which represents the sequence 0, 1, 2, 3
.
In that example, we didn’t really care about what the sequence was, only that it had four elements in it so that the code would be repeated four times (note that the variable i
was never used).
Here’s an example where we actually use the sequence in the for
loop:
for i in range(4):
print(f"i is {i}")
print(f"i squared is {i**2}")
# any following unindented lines will not be repeated
print(f"now the loop is done, and the final value of i is {i}")
i is 0
i squared is 0
i is 1
i squared is 1
i is 2
i squared is 4
i is 3
i squared is 9
now the loop is done, and the final value of i is 3
You can use any variables in your for
loop; the only requirement is that some_sequence
makes sense to “loop over”. The technical name for this is that some_sequence
is iterable, and it includes things like lists
, sets
, tuples
, and ranges
, and most objects which “contain” other objects.
The body statements are repeated once for each value in the sequence. Like regular code, they are executed from top to bottom within a for
loop. It is important to remember that the body statements are all executed in sequence for each value of x
, rather than executing the first statement for all x
, and then the second statement for all x
, and so on. You can see this behaviour in the examples above.
Computing sums and pseudocode#
A common example of where you might use a for
-loop is to sum up some values. For example, let’s try to compute the sum
\[ S(n) = \sum_{i = 1}^{n} i^{10}, \]
where \(n\) is fixed. When we are trying to write code to solve a problem, it is often the best strategy to first understand how we would systematically do the computation by hand. To compute \(S(n)\), the simplest method is to keep a running total, starting with zero and adding \(i^{10}\) to the total for \(i = 1, \ldots, n\). We might write this down in the following way:
We have some fixed \(n \in \mathbb{N}\)
Start with the total being 0
For \(i = 1, 2, \ldots, n\):
Add \(i^{10}\) to the total
Report the total as the answer
We call this sort of description of how to systematically solve a problem pseudocode. Pseudocode is not code and there is no specific standard which you must follow when writing it, but it should be possible to turn pseudocode directly into code. For example, the pseudocode above translates directly into Python like so:
# fix a value of n
n = 10
# start with the total being 0
total = 0
# loop through i = 0, 1, ..., n
for i in range(n + 1):
# add i**10 to the total
total = total + i**10
# report the total as the answer
total
14914341925
Ranges#
In the example above, we used the command range(4)
to create the sequence 0, 1, 2, 3
. Ranges are a very common way of making sequences of integers. You can see the numbers in a range
by using the list
command:
list(range(4))
[0, 1, 2, 3]
By default, a range
starts at 0
and stops before reaching the stop value. You can change where the range starts by providing a start
value as well:
list(range(4, 10))
[4, 5, 6, 7, 8, 9]
Sometimes we don’t want the integers to increase by 1
each time. We can change this by giving a step
argument. For example, to produce the odd numbers between 1 and 21 (inclusive), we could start at 1 and step by 2 each time:
list(range(1, 22, 2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
The stop
value doesn’t have to be precisely 1 more than the final value; it just has to be less than or equal to than the next value that the step would produce. In the previous example, using 23
as the stop value would produce the same result.
list(range(1, 23, 2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
We can also go backwards by giving a negative step value. Note that in this case, the final element of the range
will be greater than the stop
value.
list(range(23, 11, -2))
[23, 21, 19, 17, 15, 13]
Looping over lists#
It is very common to loop over a range
, but you can also loop over other things. We’ll see more on lists
later, but for now here is an example where we iterate over a list
of strings:
fruits = ["apple", "durian", "mangosteen", "pear"]
for fruit in fruits:
print(f"'{fruit}' has {len(fruit)} letters")
'apple' has 5 letters
'durian' has 6 letters
'mangosteen' has 10 letters
'pear' has 4 letters