Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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
4. List and Dictionary Comprehensions

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.

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
  • 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

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

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.

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
  • 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

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

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