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

Kurssisisältö

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
Counting with Loops

In programming, you often have to 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 you want to calculate the sum of all numbers in a specific range, you 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 you want to calculate the total length of all city names in our list.

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('Total length of all city names:', total_length)
copy
Tehtävä

Swipe to start coding

You are working on a travel application that needs to analyze country names for display purposes. Your task is to count the total number of countries that start with the letter 'S'.

  • Iterate through the list of countries.
  • Increment a counter each time you encounter a country that starts with the letter 'S'.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
toggle bottom row

book
Counting with Loops

In programming, you often have to 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 you want to calculate the sum of all numbers in a specific range, you 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 you want to calculate the total length of all city names in our list.

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('Total length of all city names:', total_length)
copy
Tehtävä

Swipe to start coding

You are working on a travel application that needs to analyze country names for display purposes. Your task is to count the total number of countries that start with the letter 'S'.

  • Iterate through the list of countries.
  • Increment a counter each time you encounter a country that starts with the letter 'S'.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt