Course Content
Python Loops Tutorial
Python Loops Tutorial
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.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing all destinations for city in travel_list: print(city) else: print("All destinations have been listed.")
Example: Handling Premature Termination
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.
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.")
In this case, the else
block does not execute because the loop was interrupted with break
.
Example: Using not in
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.
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.")
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.
Swipe to show code editor
Imagine you have a list of dream destinations and a smaller list of your favorite cities. Your task is to check if every city on your travel list is among your favorites. If not, stop the loop and inform the user. If all cities are favorites, celebrate!
- Check if all cities in the
travel_list
are on your favorites list. - If any city is not on your favorites list, print
"Not all cities are favorites!"
and terminate the loop. - If all cities are favorites, print
"All cities are favorites!"
in theelse
block.
Thanks for your feedback!
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.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Printing all destinations for city in travel_list: print(city) else: print("All destinations have been listed.")
Example: Handling Premature Termination
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.
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.")
In this case, the else
block does not execute because the loop was interrupted with break
.
Example: Using not in
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.
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.")
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.
Swipe to show code editor
Imagine you have a list of dream destinations and a smaller list of your favorite cities. Your task is to check if every city on your travel list is among your favorites. If not, stop the loop and inform the user. If all cities are favorites, celebrate!
- Check if all cities in the
travel_list
are on your favorites list. - If any city is not on your favorites list, print
"Not all cities are favorites!"
and terminate the loop. - If all cities are favorites, print
"All cities are favorites!"
in theelse
block.
Thanks for your feedback!