Course Content
Python Loops Tutorial
Python Loops Tutorial
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 ofname
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}")
Swipe to start coding
Airports display travel destinations on digital screens, but there’s a character limit of 40 per section. The display updates every 5 countries and scrolls if the total character length exceeds 60 characters.
- Iterate through
countries
in groups of 5 at a time. - Calculate the total character length of each batch.
- If a batch's total length exceeds 40 characters, store these 5 countries in
overflowing_countries
. - Stop once the first overflowing batch is found and print it.
Solution
Thanks for your feedback!
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 ofname
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}")
Swipe to start coding
Airports display travel destinations on digital screens, but there’s a character limit of 40 per section. The display updates every 5 countries and scrolls if the total character length exceeds 60 characters.
- Iterate through
countries
in groups of 5 at a time. - Calculate the total character length of each batch.
- If a batch's total length exceeds 40 characters, store these 5 countries in
overflowing_countries
. - Stop once the first overflowing batch is found and print it.
Solution
Thanks for your feedback!