Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Loop Control Statements | 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
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.

Using break Keyword

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.

123456789
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")
copy
  1. The loop iterates through each city in the travel_list;
  2. When it encounters "Barcelona", the break statement is executed, and the loop stops immediately;
  3. Cities after "Barcelona" (like "Munchen") are not processed.

Using continue Keyword

Let's now count the cities in the travel_list that have names shorter than 8 characters while skipping others.

123456789
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}")
copy
  • The loop iterates through each city in the travel_list;
  • When the city is "Barcelona", the if condition evaluates to True. The continue statement is executed, skipping the rest of the code for that iteration (the print() 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. Later, you can replace pass with actual logic.

123456789
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}")
copy
Task

Swipe to start coding

You're planning your next adventure and want to prioritize visa-free travel to make the journey smoother. To keep things manageable, you decide to limit your list to only 10 destinations.

  • Iterate through the countries list. Skip the countries that require a visa.
  • Add only visa-free countries to travel_list.
  • Stop adding once travel_list contains 10 countries.

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 4
toggle bottom row

book
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.

Using break Keyword

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.

123456789
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")
copy
  1. The loop iterates through each city in the travel_list;
  2. When it encounters "Barcelona", the break statement is executed, and the loop stops immediately;
  3. Cities after "Barcelona" (like "Munchen") are not processed.

Using continue Keyword

Let's now count the cities in the travel_list that have names shorter than 8 characters while skipping others.

123456789
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}")
copy
  • The loop iterates through each city in the travel_list;
  • When the city is "Barcelona", the if condition evaluates to True. The continue statement is executed, skipping the rest of the code for that iteration (the print() 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. Later, you can replace pass with actual logic.

123456789
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}")
copy
Task

Swipe to start coding

You're planning your next adventure and want to prioritize visa-free travel to make the journey smoother. To keep things manageable, you decide to limit your list to only 10 destinations.

  • Iterate through the countries list. Skip the countries that require a visa.
  • Add only visa-free countries to travel_list.
  • Stop adding once travel_list contains 10 countries.

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 4
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