computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

🔺

🎓

⏱ 20 min

Free

Pyramids & Patterns

A loop inside a loop builds shapes, grids and checkerboards.

🎯 What you will learn

  • A loop inside a loop is nested
  • Each outer step runs the inner loop fully
  • Shapes come from changing the row size

👋 Let’s get started!

Minecraft pyramids are built layer by layer, and each layer has more blocks. In code, a loop inside another loop does this. The outer loop picks the row, and the inner loop builds it. 🔺

🪜 Step by step

1. 🔺 Layers

The outer loop goes row by row.

for row in range(1, 4):
    print("Row", row)
2. 🧱 Blocks in a row

The inner loop places the blocks in each row.

for row in range(1, 4):
    print("#" * row)
3. ♟️ Checkerboard

Mix two blocks with if to make a pattern.

for r in range(2):
    line = ""
    for c in range(4):
        line = line + ("X" if (r + c) % 2 == 0 else "O")
    print(line)

🧪 Try it!

Change the code and press ▶ Run to see what happens. You can’t break anything!

Pyramid and checkerboard

for row in range(1, 4):
    print("#" * row)
print()
for r in range(2):
    line = ""
    for c in range(4):
        line = line + ("X" if (r + c) % 2 == 0 else "O")
    print(line)

🎨 Unplugged activity

🔺 Draw a pyramid on graph paper: 1 square, then 2, then 3, up to 5. Then draw a checkerboard 4 squares wide. Compare your drawings with the code output.

Build a 4-step staircase

for step in range(1, 5):
    print("#" * step)
print("Staircase done!")

📖 Key words

Tap a card to flip it over!

Nested loop
A loop inside another loop 🪆
Pattern
Something that repeats in a shape 🌈
% (modulo)
Remainder after dividing; (r + c) % 2 alternates 0 and 1 ➗

🏆 Challenge

Print a pyramid with 3 rows: one #, then two #, then three #. Press ▶ Run.

Challenge

Knowledge Check Quiz

  1. What does "#" * 3 print?
  2. How many times does the inner loop run for each outer row?
  3. What is the second row of the checkerboard in the play example?
← Previous lesson
Next lesson →