Lesson 11 · Hierarchical Thinking

Patterns Inside Patterns: When Your Brain Goes Inception Mode

What You'll Learn

How to think in layers — patterns that contain other patterns — and use nested loops to create 2D art, grids, and text banners.

Patterns All the Way Down

Think about your school. It has a structure:

School → Grades → Classes → Students

Each grade contains multiple classes. Each class contains multiple students. It's a pattern inside a pattern — a hierarchy.

Or think about a calendar: Year → Months → Weeks → Days. Or a video game: World → Levels → Rooms → Enemies. Or even a song: Album → Tracks → Verses → Lines.

When patterns are nested inside other patterns, we call that hierarchical thinking. And in code, we handle it with one of the most powerful tools in programming: loops inside loops.

A Loop Inside a Loop

Let's start simple. What if we want to print a grid — rows and columns?

Python

How Nested Loops Work

The outer loop (for row in range(3)) runs 3 times — once for each row.

For each row, the inner loop (for col in range(5)) runs 5 times — once for each column.

So the inner code runs 3 × 5 = 15 times total.

Think of it like this: "For each row, build a line by going through each column. Then print that line and move to the next row."

Try changing range(3) and range(5) to different numbers. Try replacing "⬜ " with "🟦 " or "★ ".

Patterns From Position

A plain grid is boring. The magic happens when the content depends on the position. The row and col variables tell us exactly where we are:

Python · Checkerboard

The Trick

(row + col) % 2 == 0 checks whether the sum of row and column is even. If even → dark square. If odd → light square.

This creates the checkerboard pattern because adjacent squares always have different even/odd sums. Position 0,0 (sum 0, even) is dark. Position 0,1 (sum 1, odd) is light. And so on.

Let's try more patterns:

Python · Triangle
Python · Diamond

New Thing · range(start, stop, step)

range(size - 2, -1, -1) counts backwards. The third number is the step: -1 means count down by 1.

So if size is 5, this gives: 3, 2, 1, 0 — perfect for the bottom half of the diamond.

The diamond is actually two triangles — one pointing down, one pointing up. Decomposition strikes again!

Building Emoji Art

Now let's use nested loops to create actual pictures. The trick is using a grid — a list of strings where each character represents one pixel:

Python · Pixel Art

Each string in the list is a row. Each character in the string is a column. The nested loop goes through every single position: for each row, for each character in that row.

Try changing "❤️" to "🟪" or "🔥". Or try designing your own shape by editing the grid.

Block Letter Banners

Let's build something you can actually show off — a program that turns any text into a giant block-letter banner. First, we need to define what each letter looks like:

Python · Block Letters

How the Banner Works

The outer loop goes through rows 0 to 4 (since each letter is 5 rows tall).

The inner loop goes through each character in the text.

For each character at each row, we look up that letter's pattern and grab the right row: letters[char][row].

This is hierarchical: for each row of the banner, we assemble pieces from each letter's corresponding row. Two levels of organization working together.

Try changing text = "HI!" to "HELLO" or "WORLD" or "ACE" (only letters that are defined will work).

Adding Style

The # characters work, but let's make it customizable — the user chooses what character to use:

Python · Styled Banner

Try entering "HI" with fill character "🔥". Or "ACE" with "★". Or "BODY" with "X". The same function handles any text and any style.

Pattern Borders and Frames

Let's use nested loops to build decorative frames around content:

Python · Framed Message

The Complete Emoji Art Studio

Python · Emoji Art Studio

Try all three options. Option 3 generates random art every time — run it several times for different results.

Thinking in Layers

Here's the key insight about nested loops: each level of nesting adds a dimension.

Nesting = Dimensions

One loop — goes through a list (1D: a line of items)

Two loops — rows × columns (2D: a grid, like the art we made)

Three loops — pages × rows × columns (3D: like a book of grids)

Every level multiplies the work. 10 × 10 = 100 operations. 10 × 10 × 10 = 1,000. This is why nested loops are powerful — but also why you need to be careful about how many levels you use.

Python · Three Levels

Three sections, each with 3 rows of 8 columns. The outer loop picks the section (and its colors). The middle loop handles rows. The inner loop handles columns. Three levels of pattern, all working together.

What You Just Did

You learned to think in layers — patterns that contain other patterns. The outer loop controls the big structure (which row), the inner loop controls the details (which column in that row). Together, they create 2D patterns from 1D code.

This is how screens work (every image is a grid of pixels), how spreadsheets work (rows and columns), how game worlds are built (rooms in a grid), and how buildings are organized (floors × rooms × furniture).

The Big Idea

When you see something complex, ask: "Is this a pattern inside a pattern?" Look for the layers. The outer layer controls the big structure. The inner layer handles the details. Once you see the hierarchy, the complexity becomes manageable.

Thinking Questions

  1. If a nested loop has an outer loop of 10 and inner loop of 20, how many times does the inner code run? What about 100 × 100?
  2. Can you think of something in real life that has three levels of hierarchy? (Example: country → state → city)
  3. Why does the outer loop control rows and the inner loop control columns? Could you reverse them? What would change?

Level Up

Challenge 1 · Multiplication Table

Use nested loops to print a multiplication table (1-10). The outer loop is rows, the inner loop is columns. Format it neatly with .rjust(4) so numbers line up.

Challenge 2 · More Letters

Add at least 5 more letters to the block letter dictionary (try F, J, K, M, U). Test them by typing your full name in the banner generator.

Challenge 3 · Custom Pixel Art

Design your own pixel art by creating a grid (like the heart example) with at least 3 different characters that map to different emojis. Make something cool — a star, a face, your initial, or anything you can imagine on an 8×8 grid.

What You Learned

Thinking Skill

Hierarchical Thinking — recognizing patterns within patterns. Understanding that complex structures have layers: outer structures contain inner structures. Ask: "What's the big pattern, and what patterns are inside it?"

Python Concepts

Nested loops — a loop inside a loop (creates 2D grids)

Building strings row by row — constructing each line in the inner loop, printing in the outer loop

range(start, stop, step) — counting backwards with negative step

Position-based logic — using row and col to determine content

(row + col) % 2 — the checkerboard trick

Grid data — representing 2D art as lists of strings

.replace() — swapping characters in a string for styling