Course Content
Python Loops Tutorial
Python Loops Tutorial
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 isTrue
, the corresponding block of code runs;else
: specifies what to do if the condition in theif
statement isFalse
.
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".
# 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.")
Explanation
- A
for
loop iterates through each city in thetravel_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 by2
without a remainder. The%
operator calculates the remainder of a division, often used to check divisibility or alternating patterns.
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!
Thanks for your feedback!
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 isTrue
, the corresponding block of code runs;else
: specifies what to do if the condition in theif
statement isFalse
.
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".
# 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.")
Explanation
- A
for
loop iterates through each city in thetravel_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 by2
without a remainder. The%
operator calculates the remainder of a division, often used to check divisibility or alternating patterns.
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!
Thanks for your feedback!