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

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.

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}")
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
Task

Swipe to start coding

You are working on a travel application that needs to analyze city names for display purposes. Your task is to calculate the total number of characters across all city names from the provided list.

  • Iterate through the list of countries.
  • Calculate the total length of all city names, including spaces and special characters.

Solution

# List of travel destinations
countries = ['Wales', 'Denmark', 'Belgium', 'South Korea', 'Barcelona', 'South Africa', 'Indonesia', 'Singapore', 'Australia', 'India', 'Saudi Arabia', 'Mexico', 'Turkey', 'Greece', 'Netherlands', 'Tokyo', 'Finland', 'Monako', 'United Arab Emirates', 'Egypt', 'Morocco', 'Brazil', 'Argentina', 'Ireland', 'Portugal', 'Chile', 'Paris', 'Spain', 'Czech Republic', 'Sweden', 'Switzerland', 'Liverpool', 'Thailand', 'Luxemburg', 'New Zealand', 'France', 'Italy', 'Germany', 'New York', 'China', 'Munchen', 'Canada', 'Hungary', 'Scotland', 'Norway', 'Austria', 'Ukraine', 'Poland']

# Counter variable
total_length = 0

for country in countries:
total_length += len(country)

# Testing
print("Total length of all country names:", total_length)
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
# List of travel destinations
countries = ['Wales', 'Denmark', 'Belgium', 'South Korea', 'Barcelona', 'South Africa', 'Indonesia', 'Singapore', 'Australia', 'India', 'Saudi Arabia', 'Mexico', 'Turkey', 'Greece', 'Netherlands', 'Tokyo', 'Finland', 'Monako', 'United Arab Emirates', 'Egypt', 'Morocco', 'Brazil', 'Argentina', 'Ireland', 'Portugal', 'Chile', 'Paris', 'Spain', 'Czech Republic', 'Sweden', 'Switzerland', 'Liverpool', 'Thailand', 'Luxemburg', 'New Zealand', 'France', 'Italy', 'Germany', 'New York', 'China', 'Munchen', 'Canada', 'Hungary', 'Scotland', 'Norway', 'Austria', 'Ukraine', 'Poland']

# Counter variable
total_length = 0



# Testing
print("Total length of all country names:", total_length)
toggle bottom row
some-alt