Skip to main content

for loop in Python Tutorial

Learn how to loop over a list in Python.
Jun 2020  · 4 min read

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.

Check out our For Loops in Python Tutorial.

Related

The 23 Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.
Abid Ali Awan's photo

Abid Ali Awan

22 min

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
DataCamp Team's photo

DataCamp Team

Plotly Express Cheat Sheet

Plotly is one of the most widely used data visualization packages in Python. Learn more about it in this cheat sheet.
DataCamp Team's photo

DataCamp Team

0 min

Getting started with Python cheat sheet

Python is the most popular programming language in data science. Use this cheat sheet to jumpstart your Python learning journey.
DataCamp Team's photo

DataCamp Team

8 min

Python pandas tutorial: The ultimate guide for beginners

Are you ready to begin your pandas journey? Here’s a step-by-step guide on how to get started. [Updated November 2022]
Vidhi Chugh's photo

Vidhi Chugh

15 min

Python Iterators and Generators Tutorial

Explore the difference between Python Iterators and Generators and learn which are the best to use in various situations.
Kurtis Pykes 's photo

Kurtis Pykes

10 min

See MoreSee More