Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Nested for loop execution
📖 Scenario: You are organizing a small event and want to create a seating chart. You have a list of tables and a list of guests. You want to pair each guest with each table to see all possible seating options.
🎯 Goal: Build a program that uses nested for loops to print every combination of tables and guests.
📋 What You'll Learn
Create a list called tables with the values 'Table 1', 'Table 2', and 'Table 3'
Create a list called guests with the values 'Alice', 'Bob', and 'Charlie'
Use a nested for loop with variables table and guest to iterate over tables and guests
Print each combination in the format: Guest Alice sits at Table 1
💡 Why This Matters
🌍 Real World
Event planners often need to consider all seating options to organize guests efficiently.
💼 Career
Understanding nested loops is essential for tasks like scheduling, pairing data, and generating combinations in programming jobs.
Progress0 / 4 steps
1
Create the tables list
Create a list called tables with these exact values: 'Table 1', 'Table 2', and 'Table 3'
Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Create the guests list
Create a list called guests with these exact values: 'Alice', 'Bob', and 'Charlie'
Python
Hint
Use the same list syntax as before to create the guests list.
3
Write the nested for loops
Use nested for loops with variables table and guest to iterate over tables and guests respectively
Python
Hint
Write one for loop inside another. The inner loop goes through guests.
4
Print each seating combination
Inside the nested loops, write a print statement to display: Guest {guest} sits at {table} using an f-string
Python
Hint
Use print(f"Guest {guest} sits at {table}") inside the inner loop.
Practice
(1/5)
1. What does a nested for loop do in Python? for i in range(2): for j in range(3): print(i, j)
easy
A. Runs the inner loop fully for each outer loop step
B. Runs both loops only once
C. Runs the outer loop fully for each inner loop step
D. Runs only the outer loop
Solution
Step 1: Understand nested loop structure
The outer loop runs 2 times (i=0,1). For each i, the inner loop runs 3 times (j=0,1,2).
Step 2: Observe loop execution order
For each i, the inner loop completes all its iterations before the outer loop moves to next i.
Final Answer:
Runs the inner loop fully for each outer loop step -> Option A