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.
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
- 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 start coding
You are given a list of travel expenses for multiple trips. Each trip is represented as a nested list containing various expenses such as transportation, accommodation, food, and activities. Your task is to process these expenses by identifying "cheap" costs while keeping the same nested structure.
- You are given a two-dimensional list (list of lists), where each inner list represents a single trip’s expenses.
- Iterate through each trip’s expense list.
- Replace any expense of $100 or less with
"Cheap"
, keeping other values unchanged. - Store the transformed expenses in a new two-dimensional list with the same structure
Solution
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.
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
- 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 start coding
You are given a list of travel expenses for multiple trips. Each trip is represented as a nested list containing various expenses such as transportation, accommodation, food, and activities. Your task is to process these expenses by identifying "cheap" costs while keeping the same nested structure.
- You are given a two-dimensional list (list of lists), where each inner list represents a single trip’s expenses.
- Iterate through each trip’s expense list.
- Replace any expense of $100 or less with
"Cheap"
, keeping other values unchanged. - Store the transformed expenses in a new two-dimensional list with the same structure
Solution
Thanks for your feedback!