Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If/Else in a Nested Loop | Nested Loops
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
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.
1234567891011121314151617181920212223
# 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
copy

Explanation

  • The outer while loop iterates through each trip in the travel_costs list using the index i;
  • 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.
Task
test

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:

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 3. Chapter 3
toggle bottom row

book
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.
1234567891011121314151617181920212223
# 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
copy

Explanation

  • The outer while loop iterates through each trip in the travel_costs list using the index i;
  • 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.
Task
test

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:

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 3. Chapter 3
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