Skip to content
dreamcode
dreamcode
Map
Nested loops
Lesson 18 of 77
+15 XP on finish
PYTHON BASICSChapter 3 · Loops and iteration

Loops inside loops

A loop inside another loop is a nested loop. The inner loop runs completely from start to finish for each turn of the outer loop. We can also accumulate values inside a loop.

Worked example

How it reads

  • The inner loop runs 2 times for each of the outer loop's 3 turns (6 times total)
  • total = total + 1 accumulates the count
Cloud tip: Nested loops are common for working with grids, tables, coordinates, or multi-dimensional data.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Use a loop inside a loop to print a 3 by 3 grid: three lines of * * *.

Press Run to check your work.