Repetition structures
This page is also available as Jupyter interactive notebook. Download it from here and run it in your cloudstor.
Sometimes, we want to programmatically reapeat the execution of some statement until some condition doesn’t change or a specific number of times. A typical situation is adjusting the music volume. In that case, we keep increasing the volume by pressing a button or turning a knob until the volume is just right. The following figure presents a flow chart with a condition and a statement representing that situation. The condition is revaluated every time we adjust the volume (our statement) and only if the condition is found to be False
we stop executing the statement.
A loop: we keep increasing the volume as long as we find the volume to be low |
The while loop
The while
command is used to execute a series of statements defined in the following indented lines until the while condition is determined to be False
.
while condition:
statement 1
statement 2
etc.
For example, in the following cell we write a while
loop to increase the volume
until we reach the right level, which we define being exactly 10
. As you notice, within the while
loop we have an if
statement that test the condition volume == 10
; if
the condition if True
, then the value of the variable volume_is_too_low
is set to no
- which is different from yes
, the while
condition - breaking the loop.
volume = 0
volume_is_too_low = 'yes'
while volume_is_too_low == 'yes':
print("Increasing the volume...")
volume = volume + 1
print("Volume is now " + str(volume))
if volume == 10:
volume_is_too_low = "no"
The for loop
The for
loop is useful when we know exactly the number of times we would like to execute a block of statements or, better, we know exactly the values we would like to process in a block of statements.
for variable in [value1, value2, etc.]:
statement 1
statement 2
etc.
For example, let’s say I want to print the first ten roman numerals. First, I need to create a list of string values corresponding to the roman numerals. A list is a common Python object defined by square brackets ([
and ]
) where to store multiple items.
roman_numerals = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X']
Then I write a for
statement to pass each item of my roman_numerals
- one at a time - to the statement in the for
loop. (Remember to run the previous cells before running the next cell or the intepreter will not find your roman_numerals
!)
for this_roman_numeral in roman_numerals:
print(this_roman_numeral)
What’s going on here? In the block we see two variables. The first variable is the list roman_numerals
, which we initialised before. But what’s the value of this_roman_numeral
? Well, the value of this_roman_numeral
is different in every iteration of the for
loop. It assumes the value I
during the first iteration, II
during the second iteration and so on until it assumes the value X
in the last iteration. In general terms, every for
statement can also be read like this:
Dear interpreter,
For every individual item you find in this object,
please do something.
Sincerely,
The Coder.
As a matter of fact, you don’t need to use the individual items at all in your for
loop. Indeed, it is very common to use the object in the for
statement as a counter. So if I want to print cat
ten times I can simply do
for i in [1,2,3,4,5,6,7,8,9,10]:
print("cat")
or for the sake of brevity replace [1,2,3,4,5,6,7,8,9,10]
with range(10)
, a Python function that returns all the numbers in the range 0 to 10 (this excludes the first one, which is 0).
for i in range(10):
print("cat")
Exercise
Complete the following code so to print dog
exactly five times.
____:
print('dog')
Additional resources
- Loops on Lynda.com.