Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Counting with Loops | The for Loop
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

book
Counting with Loops

In programming, we often use a counter variable to perform basic arithmetic operations within a loop. This approach allows us to iteratively process data, such as summing values or tracking totals.

For example, if we want to calculate the sum of all numbers in a specific range, we can initialize a counter variable and update it during each iteration.

Let's adapt this concept to our common topic, working with the travel_list. Suppose we want to calculate the total length of all city names in our list.

Note

An f-string in Python is a concise way to format strings. Prefix the string with f and include expressions or variables in curly braces {}. For example, f"Hello, {name}!" inserts the value of name dynamically.

1234567891011
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize counter total_length = 0 # Iteration through the list for city in travel_list: # Add the length of each city name total_length += len(city) print(f"Total length of all city names: {total_length}")
copy

Note

The += operator is a shorthand for addition and assignment. It adds the value on the right to the variable on the left and updates the variable. For example, x += 5 is equivalent to x = x + 5.

Task
test

Swipe to show code editor

Write a Python program to count how many city names in the travel_list have more than 8 characters. Use a counter variable to keep track of the count and iterate through the list.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

book
Counting with Loops

In programming, we often use a counter variable to perform basic arithmetic operations within a loop. This approach allows us to iteratively process data, such as summing values or tracking totals.

For example, if we want to calculate the sum of all numbers in a specific range, we can initialize a counter variable and update it during each iteration.

Let's adapt this concept to our common topic, working with the travel_list. Suppose we want to calculate the total length of all city names in our list.

Note

An f-string in Python is a concise way to format strings. Prefix the string with f and include expressions or variables in curly braces {}. For example, f"Hello, {name}!" inserts the value of name dynamically.

1234567891011
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize counter total_length = 0 # Iteration through the list for city in travel_list: # Add the length of each city name total_length += len(city) print(f"Total length of all city names: {total_length}")
copy

Note

The += operator is a shorthand for addition and assignment. It adds the value on the right to the variable on the left and updates the variable. For example, x += 5 is equivalent to x = x + 5.

Task
test

Swipe to show code editor

Write a Python program to count how many city names in the travel_list have more than 8 characters. Use a counter variable to keep track of the count and iterate through the list.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt