Course Content
Python Loops Tutorial
Python Loops Tutorial
If/Else in a Nested Loop
The if/else
statements are essential for adding conditions to nested loops. They allow you to filter, process, or categorize data, such as identifying specific values in lists or matrices.
Let's adapt this concept to a practical task: filtering travel expenses. If an expense exceeds a certain budget threshold, we'll mark it as "Expensive"; otherwise, we'll print the original expense.
Example: Filtering Expenses in Multiple Trips
Suppose you have a list of trips, and each trip contains expenses for categories like flights, hotels, food, and activities. Your goal is to check each expense:
- If the expense exceeds $200, mark it as "Expensive";
- Otherwise, print the original expense.
# Travel expenses for multiple trips travel_costs = [ [500, 150, 100, 50], # Trip 1 [200, 300, 120, 80], # Trip 2 [180, 220, 130, 170] # Trip 3 ] # Setting outer while loop to work with rows (trips) i = 0 while i < len(travel_costs): j = 0 print(f"Trip {i + 1} expenses: ", end='') # Label for the current trip # Setting inner while loop to work with expenses in the current trip while j < len(travel_costs[i]): if travel_costs[i][j] > 200: # Check if expense is greater than 200 print("Expensive", end=' ') else: print(travel_costs[i][j], end=' ') j += 1 # Move to the next expense print('') # Move to the next line after each trip i += 1 # Move to the next trip
Explanation
- The outer while loop iterates through each trip in the
travel_costs
list using the indexi
; - The inner while loop goes through the expenses for the current trip using the index
j
; - The
if/else
condition checks whether an expense is greater than $200; - After processing all expenses for a trip, the program moves to the next line and proceeds to the next trip.
Swipe to show code editor
Write a program to identify and label "Cheap" expenses, where the cost is less than or equal to $100. For all other expenses, print their actual values.
Expected Output:
Thanks for your feedback!
If/Else in a Nested Loop
The if/else
statements are essential for adding conditions to nested loops. They allow you to filter, process, or categorize data, such as identifying specific values in lists or matrices.
Let's adapt this concept to a practical task: filtering travel expenses. If an expense exceeds a certain budget threshold, we'll mark it as "Expensive"; otherwise, we'll print the original expense.
Example: Filtering Expenses in Multiple Trips
Suppose you have a list of trips, and each trip contains expenses for categories like flights, hotels, food, and activities. Your goal is to check each expense:
- If the expense exceeds $200, mark it as "Expensive";
- Otherwise, print the original expense.
# Travel expenses for multiple trips travel_costs = [ [500, 150, 100, 50], # Trip 1 [200, 300, 120, 80], # Trip 2 [180, 220, 130, 170] # Trip 3 ] # Setting outer while loop to work with rows (trips) i = 0 while i < len(travel_costs): j = 0 print(f"Trip {i + 1} expenses: ", end='') # Label for the current trip # Setting inner while loop to work with expenses in the current trip while j < len(travel_costs[i]): if travel_costs[i][j] > 200: # Check if expense is greater than 200 print("Expensive", end=' ') else: print(travel_costs[i][j], end=' ') j += 1 # Move to the next expense print('') # Move to the next line after each trip i += 1 # Move to the next trip
Explanation
- The outer while loop iterates through each trip in the
travel_costs
list using the indexi
; - The inner while loop goes through the expenses for the current trip using the index
j
; - The
if/else
condition checks whether an expense is greater than $200; - After processing all expenses for a trip, the program moves to the next line and proceeds to the next trip.
Swipe to show code editor
Write a program to identify and label "Cheap" expenses, where the cost is less than or equal to $100. For all other expenses, print their actual values.
Expected Output:
Thanks for your feedback!