LESSON3 - 2 - For Loops
LESSON3 - 2 - For Loops
for loops
Contents
Sources
Basics of for loops
Attention
Finnish university students are encouraged to use the CSC Notebooks platform.
launch CSC notebook
Others can follow the lesson and fill in their student notebooks using Binder.
launch binder
In this lesson we introduce loops as a way of repeating parts of a Python program, such as
iterating over all of the items in a list and performing a calculation on each item.
Sources
This lesson is inspired by the Software Carpentry group’s lessons on Programming with Python.
A (bad) example
Let’s consider an example using the list below:
Suppose we want to check the name of each city in our list. We could use the index value for
each city and do the following:
european_cities[0]
'Helsinki'
european_cities[1]
'Paris'
european_cities[2]
'Barcelona'
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 1/9
8/3/23, 16:45 for loops
european_cities[3]
'Uppsala'
But this is a bad idea. Why? Well there are two reasons:
1. It does not scale nicely for long lists, and will take forever to type in.
2. It won’t work if the length of the list has fewer than 4 cities.
european_cities[0]
'Riga'
european_cities[1]
'Rome'
european_cities[2]
'Athens'
european_cities[3]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/tmp/ipykernel_364/1729903104.py in <module>
----> 1 european_cities[3]
Here we encounter an IndexError because we have tried to access a value outside the range of
values in the updated european_cities list.
Amsterdam
Brussels
Lisbon
Reykjavik
Not only is this shorter, but it is also more flexible. Let’s try printing out a different list of cities
such as ['Detroit', 'Chicago', 'Denver', 'Boston', 'Portland', 'San Francisco',
'Houston', 'Orlando']. Still works, right?
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 2/9
8/3/23, 16:45 for loops
us_cities = [
"Detroit",
"Chicago",
"Denver",
"Boston",
"Portland",
"San Francisco",
"Houston",
"Orlando",
]
Detroit
Chicago
Denver
Boston
Portland
San Francisco
Houston
Orlando
Let’s break down the code above to see some essential aspects of for loops:
1. The variable can be any name you like other than a reserved keyword
2. The statement of the for loop must end with a :
3. The code that should be executed as part of the loop must be indented beneath the for
loop statement
The typical indentation is 4 spaces
4. There is no additional special word needed to end the loop, you simply change the
indentation back to normal.
Hint
for loops are useful to repeat some part of the code a finite number of times.
Source: https://www.bugmartini.com/comic/cats-eye/
Like many other programming concepts, the idea of looping through actions is something that
is already perhaps more familiar to you than you think. Consider your actions during a given
day. Many people have certain routines they follow each day, such as waking up, taking a
shower, eating breakfast and brushing their teeth. In Python code, we might represent such
actions as follows:
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 3/9
8/3/23, 16:45 for loops
Note that my_life would be a list of the days of your life, and the actions you take are
represented as functions, such as wake_up(). Furthermore, by following this kind of list of
repeating actions we’re able to start the day effectively even before the first cup of coffee :).
weather_conditions = [
"rain",
"sleet",
"snow",
"freezing fog",
"sunny",
"cloudy",
"ice pellets",
]
rain
sleet
snow
freezing fog
sunny
cloudy
ice pellets
0
1
2
3
4
In this case, we used a special function called range() to give us a list of 5 numbers [0, 1, 2, 3,
4] and then we printed each number in the list to the screen. When given an integer (whole
number) as an argument, range() will produce a list of numbers with a length equal to the
specified number. The list starts at 0 and ends with number - 1. You can learn a bit more about
range by typing help(range).
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 4/9
8/3/23, 16:45 for loops
help(range)
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 5/9
8/3/23, 16:45 for loops
class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return an object that produces a sequence of integers from start (inclusive)
| to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
| start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
| These are exactly the valid indices for a list of 4 elements.
| When step is given, it specifies the increment (or decrement).
|
| Methods defined here:
|
| __bool__(self, /)
| self != 0
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __reduce__(...)
| Helper for pickle.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(...)
| Return a reverse iterator.
|
| count(...)
| rangeobject.count(value) -> integer -- return number of occurrences of value
|
| index(...)
| rangeobject.index(value) -> integer -- return index of value.
| Raise ValueError if the value is not present.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| start
|
| step
|
| stop
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 6/9
8/3/23, 16:45 for loops
for i in range(...):
print(i)
Using the documentation that is produced when you run help(range), what values would you
replace the ... in the parentheses of the range() function with to have the following output
printed to the screen?
2
5
8
You can test your solution in the cell below and select your answer from the poll options at
https://geo-python.github.io/poll/.
2
5
8
Tip
We will print numbers[i] before and after the addition in the for loop to see how the
values change.
numbers = [5, 6, 7, 8]
for i in range(len(numbers)):
print(f"Value of i: {i}")
print(f"Value of numbers[i] before addition: {numbers[i]}")
numbers[i] = numbers[i] + i
print(f"Value of numbers[i] after addition: {numbers[i]}")
print("")
Value of i: 0
Value of numbers[i] before addition: 5
Value of numbers[i] after addition: 5
Value of i: 1
Value of numbers[i] before addition: 6
Value of numbers[i] after addition: 7
Value of i: 2
Value of numbers[i] before addition: 7
Value of numbers[i] after addition: 9
Value of i: 3
Value of numbers[i] before addition: 8
Value of numbers[i] after addition: 11
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 7/9
8/3/23, 16:45 for loops
print(numbers)
[5, 7, 9, 11]
1. You can see that because we are using the range() function, the value assigned to the loop
variable i starts with 0 and increases by 1 each time through the loop.
2. You can see the value in the list numbers at index i each time through the loop, and how
that value changes when it is increased by adding i.
3. The value that changes in the list numbers in each iteration through this for loop is the
value at index i, while the other values are not updated. This occurs because we’re
assigning a new value at numbers[i].
4. Note that the values for numbers[i] on the right side of the equation is the “old” value.
That “old” value is increased by i first, and then stored as the updated value numbers[i].
Note
The variable i is commonly used to denote the index variable in loops. Loops can
sometimes occur with another loop (referred to as nested loops), in which case other
index variables such as j or k may be used.
As you can see we have 5 cities and 5 corresponding counties. Can you print out each pair using
a single for loop?
for i in range(len(cities)):
print(f"{cities[i]} is the capital of {countries[i]}")
Cool. So as you can see, the index i is used in this case to access each item in the two lists of
cities and countries and allow us to print out the city/country pairs. We’ll get more practice with
this kind of thing in the exercises for this week.
Note
In the example above, we used the length of the list cities in the range() function. We
could just as easily used the list countries to define the values of i since both lists are
the same length.
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 8/9
8/3/23, 16:45 for loops
odd_numbers = [1, 3, 5, 7, 9]
even_numbers = [10, 4, 6, 8, 2]
for i in range(len(odd_numbers)):
print(odd_numbers[i] + even_numbers[i])
Try to think about the loop without running the code and then select your answer from the poll
options at https://geo-python.github.io/poll/.
11
7
11
15
11
© Copyright 2016-2022, D. Whipp, H. Tenkanen, V. Heikinheimo, H. Aagesen, and C. Fink, Department of Geosciences and Geography, University of Helsinki.
Last updated on Oct 20, 2022.
https://geo-python-site.readthedocs.io/en/latest/notebooks/L3/for-loops.html 9/9