6  Intro to Loops in Python

6.1 Introduction

At the heart of programming is the concept of repeating a task multiple times. A for loop is one fundamental way to do that. Loops enable efficient repetition, saving time and effort.

Mastering this concept is essential for writing intelligent Python code.

Let’s dive in and enhance your coding skills!/Applications/Python 3.12/Install Certificates.command

6.2 Learning Objectives

By the end of this lesson, you will be able to:

  • Use basic for loops in Python
  • Use index variables to iterate through lists in a loop
  • Format output using f-strings within loops
  • Apply loops to generate multiple plots for data visualization

6.3 Packages

In this lesson, we will use the following Python libraries:

import pandas as pd
import plotly.express as px
from vega_datasets import data

6.4 Intro to for Loops

Let’s start with a simple example. Suppose we have a list of children’s ages in years, and we want to convert these to months:

ages = [7, 8, 9]  # List of ages in years

We could try to directly multiply the list by 12:

ages * 12
[7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9,
 7,
 8,
 9]

But this does not do what we want. It repeats the list 12 times.

Rather, we need to loop through each element in the list and multiply it by 12:

for age in ages:
    print(age * 12)
84
96
108

for and in are required keywords in the loop. The colon and the indentation on the second line are also required.

In this loop, age is a temporary variable that takes the value of each element in ages during each iteration. First, age is 7, then 8, then 9.

You can choose any name for this variable:

for random_name in ages:
    print(random_name * 12)
84
96
108

Note that we need the print statement since the loop does not automatically print the result:

for age in ages:
    age * 12
Practice

6.4.1 Hours to Minutes Basic Loop

Try converting hours to minutes using a for loop. Start with this list of hours:

hours = [3, 4, 5]  # List of hours
# Your code here

6.5 Printing with f-strings

We might want to print both the result and the original age. We could do this by concatenating strings with the + operator. But we need to convert the age to a string with str().

for age in ages:
    print(str(age) + " years is " + str(age * 12) + " months" )
7 years is 84 months
8 years is 96 months
9 years is 108 months

Alternatively, we can use something called an f-string. This is a string that allows us to embed variables directly.

for age in ages:
    print(f"{age} years is {age * 12} months")
7 years is 84 months
8 years is 96 months
9 years is 108 months

Within the f-string, we use curly braces {} to embed the variables.

Practice

6.5.1 Practice: F-String

Again convert the list of hours below to minutes. Use f-strings to print both the original hours and the converted minutes.

hours = [3, 4, 5]  # List of hours
# Your code here
# Example output "3 hours is 180 minutes"

6.6 Are for Loops Useful in Python?

While for loops are useful, in many cases there are more efficient ways to perform operations over collections of data.

For example, our initial age conversion could be achieved using pandas Series:

import pandas as pd

ages = pd.Series([7, 8, 9])
months = ages * 12
print(months)
0     84
1     96
2    108
dtype: int64

But while libraries like pandas offer powerful ways to work with data, for loops are essential for tasks that can’t be easily vectorized or when you need fine-grained control over the iteration process.

6.7 Looping with an Index and Value

Sometimes, we want to access both the position (index) and the value of items in a list. The enumerate() function helps us do this easily.

Let’s look at our ages list again:

ages = [7, 8, 9]  # List of ages in years

First, let’s see what enumerate() actually does:

for item in enumerate(ages):
    print(item)
(0, 7)
(1, 8)
(2, 9)

As you can see, enumerate() gives us pairs of (index, value).

We can unpack these pairs directly in the for loop:

for i, age in enumerate(ages):
    print(f"The person at index {i} is aged {age}")
The person at index 0 is aged 7
The person at index 1 is aged 8
The person at index 2 is aged 9

Here, i is the index, and age is the value at that index.

Now, let’s create a more detailed output using both the index and value:

for i, age in enumerate(ages):
    print(f"The person at index {i} is aged {age} years which is {age * 12} months")
The person at index 0 is aged 7 years which is 84 months
The person at index 1 is aged 8 years which is 96 months
The person at index 2 is aged 9 years which is 108 months

This is particularly useful when you need both the position and the value in your loop.

Practice

6.7.1 Practice: Enumerate with F-strings

Use enumerate() and f-strings to print a sentence for each hour in the list:

hours = [3, 4, 5]  # List of hours

# Your code here
# Example output: "Hour 3 at index 0 is equal to 180 minutes"

7 Real Loops Application: Generating Multiple Plots

Now that you have a solid understanding of for loops, let’s apply our knowledge to a more realistic looping task: generating multiple plots.

We’ll use the gapminder dataset from Vega datasets to demonstrate this. Our aim is to create line plots for a few selected countries in the gapminder dataset.

First, let’s load the data:

# Load gapminder dataset
gapminder = data.gapminder()
gapminder.head()
year country cluster pop life_expect fertility
0 1955 Afghanistan 0 8891209 30.332 7.7
1 1960 Afghanistan 0 9829450 31.997 7.7
2 1965 Afghanistan 0 10997885 34.020 7.7
3 1970 Afghanistan 0 12430623 36.088 7.7
4 1975 Afghanistan 0 14132019 38.438 7.7

Now let’s create a line chart for a single country. We’ll use the query method to filter the data for the country “Canada”. We have not learned this method yet; it is a pandas function that allows us to filter the data based on a condition. We will learn more about it later in the course.

# Filter data for China
china_data = gapminder.query("country == 'China'")

# Create line chart
fig = px.line(china_data, x="year", y="life_expect", title="Life Expectancy in China")
fig.show()

Now, let’s create a loop to create line plots for a few selected countries.

countries = ["India", "China", "United States", "Indonesia", "Pakistan"]

for country_name in countries:
    country_data = gapminder.query("country == @country_name")
    fig = px.line(
        country_data,
        x="year",
        y="life_expect",
        title=f"Life Expectancy in {country_name}",
    )
    fig.show()

This loop creates a separate line plot for each country in our list, showing how life expectancy has changed over time.

Practice

7.0.1 Practice: Population Over Time

Using the gapminder dataset, create a bar chart (with px.bar) of the population for the countries United States, Canada, Mexico and Jamaica over time.

# Your code here
Practice

7.0.2 Practice: Tips Histogram by Day

Using the tips dataset, create a histogram of the total bill for each day of the week.

# Load tips dataset
tips = px.data.tips()
tips.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
# List of days
days = ["Thur", "Fri", "Sat", "Sun"]

# Your loop here

8 Wrap Up!

We’ve covered the very basics of for loops in Python, from simple syntax to practical data analysis applications. Loops are essential for efficient coding, allowing you to automate repetitive tasks. As we progress in the course, we will encounter many other applications of this key programming construct.