Tutorials
python

for loop in Python

Have a look at the recipe for the “for loop.”

for var in seq :
    expression

This can be read as for each var, variable, in seq, a sequence, execute the expressions.

Example of a for loop

Let’s start a for loop that prints the height of family members.

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam :
    print(height)

For height in fam followed by a colon means that we want to execute some code for each height in the fam list.

Height is an arbitrary name here; you could just as well call it “h” or something else. Inside the for loop, on every iteration, you print out the value of height. When you run this script, Python encounters the for loop and evaluates the seq element, fam in this case. It sees that it’s a list containing four elements.

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam :
    print(height)
    # first iteration
    # height = 1.73
1.73

In the first run, Python stores the first element, so the float 1 point 73, in the variable height. Next, the expression print(height), is executed, printing out 1 point 73.

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam :
    print(height)
    # second iteration
    # height = 1.68
1.73
1.68

In the second interaction, Python stores the second value of fam in height, being 1 point 68 now, and prints out the height again.

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam :
    print(height)
1.73
1.68
1.71
1.89

This process continues, until heights in fam have been iterated over, and we end up with four separate printouts.

Enumerate

Say that together with printing out the height, you also want to display the index in the list, so that the printouts are converted to this. How should the for loop be built in this case? To achieve this, you can use enumerate(). Let’s update the for loop definition like this.

fam = [1.73, 1.68, 1.71, 1.89]
for index, height in enumerate(fam) :
    print("index " + str(index) + ": " + str(height))
index 0: 1.73
index 1: 1.68
index 2: 1.71
index 3: 1.89

Now enumerate(fam) produces two values on each iteration: the index of the value, and the value itself. Instead of a single variable height, you now write index, height. Now, on each iteration, index will contain the index, and height will contain the float.

This means that we can now also update the statement inside the for loop with a more complicated print call. Notice that we had to convert the floats to strings with STR so that you can add everything together. The printouts are exactly what we wanted.

Loop Over String

The for loop doesn’t only work with lists; you can also create a for loop that iterates over every character in a string family and stores it in C one after the other.

for c in "family" :
    print(c.capitalize())
F
A
M
I
L
Y

Inside the loop, the string C is capitalized and printed out. This time six different print outs occur.

Interactive example of a for loop

Here is a for loop that iterates over all elements of the areas list and prints out every element separately.

# areas list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Code the for loop
for area in areas :
    print(area)
11.25
18.0
20.0
10.75
9.5

Try it for yourself.

To learn more about for loops in Python, please see this video from our course Intermediate Python.

This content is taken from DataCamp’s Intermediate Python course by Hugo Bowne-Anderson.