Junior Codes
Clockwork Woods

Python Nested Loops for Kids

Put a loop inside a loop to build rows, columns and patterns.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Nested loops, explained simply

A nested loop is a loop inside another loop. The inner loop runs all the way through every single time the outer loop goes round once.

Think of a grid: the outer loop picks a row, and the inner loop fills in every square in that row. Three rows of four squares means the inner line runs twelve times.

Nested loops are how programs draw patterns, fill game boards and check every pair of things against each other.

Example: A tiny mosaic

for row in range(3):
    line = ""
    for col in range(5):
        line = line + "#"
    print(line)

Output

#####
#####
#####

Try this

  • Change the numbers to make a tall, thin rectangle.
  • Use range(row + 1) for the inner loop and discover the triangle.
  • Alternate two symbols to make a chequered pattern.

Practise it in Wonderwild

“The mosaic machine”

Mission 16 in Clockwork Woods, one of 36 Python missions in Wonderwild.

Your child writes and runs real Python in the browser while Pip reacts to every line. Nothing to install. For ages 7+.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Questions parents and kids ask

How many times does the inner code run?+

Multiply the repeats: an outer loop of 3 and an inner loop of 5 run the inner code 15 times.

Can I nest more than two loops?+

Yes, but it gets hard to follow quickly. If you need three levels, it is often clearer to move the inner part into a function.

Prefer learning with a teacher?

Our live Python for Young Developers course teaches these ideas in small online classes led by a software engineer, with projects and feedback. Every live course also includes 12 months of full Wonderwild access for practice.

See the Python for Young Developers course