Course Content
Python Loops Tutorial
Python Loops Tutorial
Loop Control Statements
When working with loops, the break and continue statements help manage the flow of iteration:
break
: exits the loop prematurely when a condition is met;continue
: skips the current iteration and moves to the next one, allowing selective execution;pass
: is a placeholder that does nothing when executed.
Let's explore these concepts using the travel_list
.
Using break
to Exit a Loop
Imagine searching for a specific city in a list. If we want to stop searching as soon as we find the city "Barcelona"
, we can use the break
statement.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Searching for a specific city for city in travel_list: if city == "Barcelona": print("Found Barcelona!") break else: print(f"{city} is not Barcelona")
- The loop iterates through each city in the
travel_list
; - When it encounters
"Barcelona"
, thebreak
statement is executed, and the loop stops immediately; - Cities after
"Barcelona"
(like "Munchen") are not processed.
Using continue
to Skip Iterations
Let's now count the cities in the travel_list
that have names shorter than 8 characters while skipping others.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] for city in travel_list: if city == "Barcelona": # Skip the rest of the code for this iteration continue print(f"{city} is marked for later processing.") # This won't run after `continue` else: print(f"Processing {city}")
How It Works:
- The loop iterates through each city in the
travel_list
; - When the city is
"Barcelona"
, theif
condition evaluates toTrue
. Thecontinue
statement is executed, skipping the rest of the code for that iteration (theprint()
statement is not executed); - For all other cities, the else block runs, printing the message
"Processing {city}"
. This ensures that only"Barcelona"
is skipped from processing.
The pass
Keyword
The pass statement in Python is a placeholder that does nothing when executed. It's often used as a temporary placeholder for code you plan to write later, allowing the program to run without errors.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] for city in travel_list: if city == "Barcelona": # Placeholder for future logic pass print(f"{city} is marked for later processing.") # Code still runs after 'pass' else: print(f"Processing {city}")
Later, you can replace pass
with actual logic for "Barcelona".
Swipe to show code editor
Search for Your Dream Destination
Imagine you're on a mission to find your dream city in a list of exciting travel destinations! Your task is to write a program that searches for a specific city in the travel_list
.
- If the city is found, celebrate by printing a message and stop searching further—your mission is complete!
- If the city isn't in the list, print a message if the city is not found.
Thanks for your feedback!
Loop Control Statements
When working with loops, the break and continue statements help manage the flow of iteration:
break
: exits the loop prematurely when a condition is met;continue
: skips the current iteration and moves to the next one, allowing selective execution;pass
: is a placeholder that does nothing when executed.
Let's explore these concepts using the travel_list
.
Using break
to Exit a Loop
Imagine searching for a specific city in a list. If we want to stop searching as soon as we find the city "Barcelona"
, we can use the break
statement.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Searching for a specific city for city in travel_list: if city == "Barcelona": print("Found Barcelona!") break else: print(f"{city} is not Barcelona")
- The loop iterates through each city in the
travel_list
; - When it encounters
"Barcelona"
, thebreak
statement is executed, and the loop stops immediately; - Cities after
"Barcelona"
(like "Munchen") are not processed.
Using continue
to Skip Iterations
Let's now count the cities in the travel_list
that have names shorter than 8 characters while skipping others.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] for city in travel_list: if city == "Barcelona": # Skip the rest of the code for this iteration continue print(f"{city} is marked for later processing.") # This won't run after `continue` else: print(f"Processing {city}")
How It Works:
- The loop iterates through each city in the
travel_list
; - When the city is
"Barcelona"
, theif
condition evaluates toTrue
. Thecontinue
statement is executed, skipping the rest of the code for that iteration (theprint()
statement is not executed); - For all other cities, the else block runs, printing the message
"Processing {city}"
. This ensures that only"Barcelona"
is skipped from processing.
The pass
Keyword
The pass statement in Python is a placeholder that does nothing when executed. It's often used as a temporary placeholder for code you plan to write later, allowing the program to run without errors.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] for city in travel_list: if city == "Barcelona": # Placeholder for future logic pass print(f"{city} is marked for later processing.") # Code still runs after 'pass' else: print(f"Processing {city}")
Later, you can replace pass
with actual logic for "Barcelona".
Swipe to show code editor
Search for Your Dream Destination
Imagine you're on a mission to find your dream city in a list of exciting travel destinations! Your task is to write a program that searches for a specific city in the travel_list
.
- If the city is found, celebrate by printing a message and stop searching further—your mission is complete!
- If the city isn't in the list, print a message if the city is not found.
Thanks for your feedback!