Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The if/else Statements 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

book
The if/else Statements in a for Loop

In programming, the if/else construct is frequently used within loops to execute specific actions based on conditions. This combination allows us to evaluate each item in a sequence and decide what actions to take based on its value.

  • if: checks a condition. If the condition is True, the corresponding block of code runs;
  • else: specifies what to do if the condition in the if statement is False.

To learn more about how the if/else construct works and how to use it effectively, check out the Introduction to if-else Statement chapter.

Example: Categorizing Cities by Length

Instead of just printing messages, let's categorize the cities in the travel_list based on their name lengths. Cities with names longer than 7 characters will be labeled as "long", while others will be labeled as "short".

123456789
# List of travel destinations travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Categorizing cities by name length for city in travel_list: if len(city) > 7: print(f"{city} has a long name.") else: print(f"{city} has a short name.")
copy

Explanation

  • A for loop iterates through each city in the travel_list;
  • Inside the loop, the len() function checks the length of each city name.
  • If the name length is greater than 7, it categorizes it as having a "long name";
  • Otherwise, it categorizes it as having a "short name".

Note

In mathematics and programming, 0 is considered an even number because it is divisible by 2 without a remainder. The % operator calculates the remainder of a division, often used to check divisibility or alternating patterns.

Task
test

Swipe to show code editor

Traveling can be fun, but managing expenses is crucial! In this task, you'll analyze travel costs and categorize each expense as either "even" or "odd" using Python. Practice your skills with loops, conditionals, and the % operator while organizing these expenses effectively!

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
The if/else Statements in a for Loop

In programming, the if/else construct is frequently used within loops to execute specific actions based on conditions. This combination allows us to evaluate each item in a sequence and decide what actions to take based on its value.

  • if: checks a condition. If the condition is True, the corresponding block of code runs;
  • else: specifies what to do if the condition in the if statement is False.

To learn more about how the if/else construct works and how to use it effectively, check out the Introduction to if-else Statement chapter.

Example: Categorizing Cities by Length

Instead of just printing messages, let's categorize the cities in the travel_list based on their name lengths. Cities with names longer than 7 characters will be labeled as "long", while others will be labeled as "short".

123456789
# List of travel destinations travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Categorizing cities by name length for city in travel_list: if len(city) > 7: print(f"{city} has a long name.") else: print(f"{city} has a short name.")
copy

Explanation

  • A for loop iterates through each city in the travel_list;
  • Inside the loop, the len() function checks the length of each city name.
  • If the name length is greater than 7, it categorizes it as having a "long name";
  • Otherwise, it categorizes it as having a "short name".

Note

In mathematics and programming, 0 is considered an even number because it is divisible by 2 without a remainder. The % operator calculates the remainder of a division, often used to check divisibility or alternating patterns.

Task
test

Swipe to show code editor

Traveling can be fun, but managing expenses is crucial! In this task, you'll analyze travel costs and categorize each expense as either "even" or "odd" using Python. Practice your skills with loops, conditionals, and the % operator while organizing these expenses effectively!

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