Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn The Else Statement in a for Loop | 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
4. List and Dictionary Comprehensions

book
The Else Statement in a for Loop

In Python, the else statement can be used with a for loop. The else block executes when the loop completes all its iterations without being interrupted by a break statement. This feature is particularly useful for confirming that the loop ran to completion.

Let's adapt this concept to the travel_list. We'll print each destination, and when all destinations are processed without interruption, the else block will confirm completion.

1234567
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing all destinations for city in travel_list: print(city) else: print("All destinations have been listed.")
copy

Now, let's add a condition to terminate the loop prematurely using break. If we're looking for a specific city (e.g., "Barcelona") and find it, the loop stops, and the else block does not execute.

123456789
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Searching for a specific city for city in travel_list: print(city) if city == "Barcelona": break else: print("All destinations have been listed.")
copy

In this case, the else block does not execute because the loop was interrupted with break.

We can use not in to confirm that a city is absent from the list. If the city is not found in the list, we can execute specific actions.

123456789
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Checking if a city is NOT in the list search_city = "Paris" if search_city not in travel_list: print(f"{search_city} is not in the travel list.") else: print(f"{search_city} is in the travel list.")
copy

The not in operator checks whether an element is not present in a collection (like a list, tuple, or string). Use it to check whether a city is not in the favorite_city list.

Task

Swipe to start coding

You’re planning your next trip, but not all destinations are currently open for travel. To streamline the process, you decide to automate destination selection by prioritizing available options.

Check your priority countries if any are in the list of open countries, select the first available one. If none are open, move to your backup countries and choose the first available destination.

  • countries a full list of places you are considering for travel;
  • priority_countries your topchoice destinations that you want to visit first;
  • backup_countries alternative destinations in case none of your priority countries are available;
  • open_countries countries that currently allow travelers.

Solution

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
The Else Statement in a for Loop

In Python, the else statement can be used with a for loop. The else block executes when the loop completes all its iterations without being interrupted by a break statement. This feature is particularly useful for confirming that the loop ran to completion.

Let's adapt this concept to the travel_list. We'll print each destination, and when all destinations are processed without interruption, the else block will confirm completion.

1234567
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing all destinations for city in travel_list: print(city) else: print("All destinations have been listed.")
copy

Now, let's add a condition to terminate the loop prematurely using break. If we're looking for a specific city (e.g., "Barcelona") and find it, the loop stops, and the else block does not execute.

123456789
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Searching for a specific city for city in travel_list: print(city) if city == "Barcelona": break else: print("All destinations have been listed.")
copy

In this case, the else block does not execute because the loop was interrupted with break.

We can use not in to confirm that a city is absent from the list. If the city is not found in the list, we can execute specific actions.

123456789
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Checking if a city is NOT in the list search_city = "Paris" if search_city not in travel_list: print(f"{search_city} is not in the travel list.") else: print(f"{search_city} is in the travel list.")
copy

The not in operator checks whether an element is not present in a collection (like a list, tuple, or string). Use it to check whether a city is not in the favorite_city list.

Task

Swipe to start coding

You’re planning your next trip, but not all destinations are currently open for travel. To streamline the process, you decide to automate destination selection by prioritizing available options.

Check your priority countries if any are in the list of open countries, select the first available one. If none are open, move to your backup countries and choose the first available destination.

  • countries a full list of places you are considering for travel;
  • priority_countries your topchoice destinations that you want to visit first;
  • backup_countries alternative destinations in case none of your priority countries are available;
  • open_countries countries that currently allow travelers.

Solution

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