TalentJR

🐍 Python for kids · Class 6 · Lesson 5 of 18 · Free

Nested Loops – Patterns and Grids

⏱ About 35 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Trace a nested loop and count how many times the inner block runs.
  • Print rectangles, triangles and number patterns with two loops.
  • Build grids and pairs, like a times table or match fixtures.

A cinema hall has rows, and every row has seats. To check every seat, an usher walks row by row, and in each row, seat by seat. That is a loop inside a loop, and Python calls it a nested loop.

What is a nested loop in Python?

A nested loop is a loop written inside another loop. The inside one is called the inner loop.

For every single round of the outer loop, the inner loop runs completely from start to end.

So 2 outer rounds of 3 inner rounds give 2 x 3 = 6 inner runs.

🎬 How Python thinks — step 1 of 20Python is about to run line 1
▶for row in range(1, 3):
2 for seat in range(1, 4):
3 print(f"Row {row}, Seat {seat}")
4 print("--- end of row", row)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Row 1, Seat 1
Row 1, Seat 2
Row 1, Seat 3
--- end of row 1
Row 2, Seat 1
Row 2, Seat 2
Row 2, Seat 3
--- end of row 2

💡 Watch the trace: seat goes 1, 2, 3 and starts again at 1 when row changes.

How do I print a rectangle of stars with nested loops?

The outer loop decides the rows. The inner loop prints the stars in one row.

print("*", end="") prints a star but stays on the same line. The empty print() then moves to a new line.

Without that empty print(), all the stars would end up on one long line.

🐍 Python — you can edit this code
Output
Rows: 3
Columns: 8
********
********
********

How do I make a triangle pattern in Python?

For a triangle, each row has a different number of stars. Row 1 has one star, row 2 has two, and so on.

The trick is to let the inner range depend on the outer variable. Here the inner loop runs row times.

This one idea unlocks almost every pattern question in exams.

🎬 How Python thinks — step 1 of 34Python is about to run line 1
▶for row in range(1, 5):
2 for star in range(row):
3 print("*", end="")
4 print()
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
*
**
***
****

How do I print a number triangle with nested loops?

Instead of stars, print the inner loop variable itself. Each row then counts up from 1.

A space after each number keeps them apart. Change the outer range to make the triangle bigger.

🐍 Python — you can edit this code
Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

💡 Here we build each row as a string first, then print the whole row at once.

How do I make a multiplication table grid?

A times-table grid is a perfect nested loop. The outer loop picks the row number, and the inner loop picks the column.

Each cell shows row * col. The {:4} in the f-string gives every number four spaces, so the columns line up.

🐍 Python — you can edit this code
Output
   1   2   3   4   5
   2   4   6   8  10
   3   6   9  12  15
   4   8  12  16  20
   5  10  15  20  25

How do I make all pairs from a list with nested loops?

Nested loops can match every item with every other item. This is how a league fixture list is made.

We loop over positions with range(len(teams)). Starting the inner loop at i + 1 avoids repeats.

So Mumbai vs Chennai appears once, and no team plays itself.

🎬 How Python thinks — step 1 of 31Python is about to run line 1
▶teams = ["Mumbai", "Chennai", "Kolkata", "Delhi"]
2matches = 0
3for i in range(len(teams)):
4 for j in range(i + 1, len(teams)):
5 print(teams[i], "vs", teams[j])
6 matches = matches + 1
7print("Total matches:", matches)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Mumbai vs Chennai
Mumbai vs Kolkata
Mumbai vs Delhi
Chennai vs Kolkata
Chennai vs Delhi
Kolkata vs Delhi
Total matches: 6

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

count = 0
for a in range(3):
    for b in range(4):
        count = count + 1
print(count)

Question 2. What does this print?

for i in range(3, 0, -1):
    print("#" * i)

Question 3. What does this print?

for x in range(1, 3):
    for y in range(1, 3):
        print(x * 10 + y)

🧩 Your challenge

Staircase of numbers! Ask for the number of rows. Row 1 shows 1, row 2 shows 2 2, row 3 shows 3 3 3, and so on. Each row repeats its own row number.

Expected output (with the inputs given):
How many rows? 5
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

for row in range(3):
    for c in range(4):
        print("*", end="")
print()

No error message – but all 12 stars come out on one line instead of 3 rows.

✅ Right

for row in range(3):
    for c in range(4):
        print("*", end="")
    print()

The empty print() must be inside the outer loop, lined up with the inner for. Then it runs after every row.

❌ Wrong

for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end=" ")
        print()

No error message – but every number prints on its own line, so there is no grid.

✅ Right

for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end=" ")
    print()

With extra indentation, print() runs after every number. Move it back four spaces so it runs once per row.

❌ Wrong

for i in range(3):
    for i in range(2):
        print("Row", i)

No error message – but the outer row number is lost and only 0 and 1 ever print.

✅ Right

for i in range(3):
    for j in range(2):
        print("Row", i, "item", j)

Both loops used the name i, so the inner loop overwrote the outer value. Give each loop its own variable.

📌 Remember

  • A nested loop is a loop inside a loop; the inner loop runs fully for each outer round.
  • Total inner runs = outer rounds x inner rounds.
  • Use print(..., end="") to stay on a line, and print() to start the next row.
  • Let the inner range use the outer variable to make triangles.
  • Start the inner loop at i + 1 to make pairs without repeats.
  • Give each loop its own variable name, like row and col.

Finished the quiz and the challenge?

Questions students ask

How many times does a nested loop run?

Multiply the rounds. An outer loop of 5 and an inner loop of 10 run the inner block 50 times.

Why do my nested loop stars print on one line?

Your empty print() is missing or is outside the outer loop. Put it inside the outer loop, after the inner loop ends.

Can I put a while loop inside a for loop?

Yes. Any loop can sit inside any other loop. The rule is the same: the inner one finishes before the outer one moves on.

Does break stop both loops?

No. break only leaves the loop it is written in. The outer loop carries on with its next round.