Unit 3 · Lesson 4

Nested Loops

Put a loop inside a loop and unlock a new dimension — literally. Nested loops let you work with grids, tables, and 2D patterns.

Nest a for loop inside another
Understand inner vs outer iteration
Build grids and tables
Use end="" to print on the same line

Loops Inside Loops

When you put one loop inside another, the inner loop runs completely for each iteration of the outer loop:

for row in range(3): # outer: 3 rows for col in range(4): # inner: 4 columns per row print("*", end=" ") # print on same line print() # new line after each row

Output:

* * * * * * * * * * * *

The key trick: print("*", end=" ") prints without a newline. Then print() by itself creates the newline at the end of each row.

How it works: The outer loop says "do 3 rows." For each row, the inner loop says "print 4 stars." That's 3 × 4 = 12 stars total, arranged in a 3×4 grid.

Multiplication Table

for i in range(1, 4): for j in range(1, 4): print(f"{i*j:4}", end="") print() # Output: # 1 2 3 # 2 4 6 # 3 6 9

The :4 in the f-string right-aligns each number in a 4-character-wide space, keeping the columns neat.

⚠️ Performance note: Nested loops multiply iterations. A 100×100 grid is 10,000 iterations. A 1000×1000 grid is 1,000,000. Avoid nesting more than 2–3 levels deep.
Practice Time

Challenges

Challenge 1: Grid Printer

Guided

Print an N×M grid of dots.

Instructions: Using nested loops, print a grid that is rows tall and cols wide. Each cell is a . separated by spaces. Use end=" " for same-line printing.

Example (3×4):
. . . .
. . . .
. . . .
Hint 1: Outer: for r in range(rows):, Inner: for c in range(cols):
Hint 2: Inner body: print(".", end=" "). After inner loop: print()
✖️

Challenge 2: Multiplication Table

Guided

Print a formatted multiplication table from 1 to size.

Instructions: Print a multiplication table where each cell shows i × j. Use f"{i*j:4}" for neat column alignment (4-char width). Each row on its own line.

Example (size=3):
   1   2   3
   2   4   6
   3   6   9
Hint 1: for i in range(1, size + 1): for rows, same for j (columns)
Hint 2: print(f"{i*j:4}", end="") then print() after inner loop
📦

Challenge 3: Box Drawer

Solo

Print a hollow rectangle of # characters.

Instructions: Print a hollow box that is width wide and height tall. The border is # and the inside is spaces.

Example (6×4):
######
#    #
#    #
######

Hint: A cell is on the border if it's in the first/last row or first/last column.
Hint 1: For each position, check: is it on the border? if r == 0 or r == height-1 or c == 0 or c == width-1:
Hint 2: If on border: print("#", end=""), else print(" ", end=""). Then print() after each row.
💎

Challenge 4: Diamond

Stretch

Print a diamond shape made of * characters.

Instructions: Given n (always odd), print a diamond. The middle row has n stars. Each row above/below has 2 fewer stars, centered with spaces.

Example (n=5):
  *
 ***
*****
 ***
  *
Hint 1: Split into top half (including middle) and bottom half.
Hint 2: Top half: for stars in range(1, n+1, 2): spaces = (n - stars) // 2, print(" " * spaces + "*" * stars)
Hint 3: Bottom half: for stars in range(n-2, 0, -2): same pattern