You just watched a nested loop: a loop inside a loop. The outer loop picks a row. The inner loop goes across that row. Row 1 got 1 star, row 5 got 5, and the cursor checked every one of the 25 cells.

Quick check

Quick check: At n = 5, how many stars are on the grid at the end?

A loop inside a loop#

Two print tricks draw each row:

CodeWhat it prints
print("*", end="")A star, and it stays on the same line
print()Only a new line, which ends the row

Normally print jumps to a new line after it prints. end="" stops that jump. Here is the triangle as code, at n = 3. Watch i and j: i stays on one row while j runs across it. Then i moves down and j starts again from 0.

A loop inside a loopline 1
Try another n
  1. n = 3
  2. for i in range(1, n + 1):
  3. for j in range(i):
  4. print("*", end="")
  5. print()

Variables

n
3 (just changed)

n is the number of rows to print.

Step 1 of 23

Quick check

Quick check: Set n to 1 on the slider. What does the code print?

Quick check

Quick check: Line 3 says range(i). On row 3, how many times does the inner loop print a star?

Pyramid: spaces first, then stars#

A pyramid is a triangle pushed into the centre. Each row has two inner loops, one after the other. The first prints n - i spaces. The second prints the stars. Each row adds one star on each side, so the stars grow by 2: 1, 3, 5. That is 2 * i - 1. And because the row grows by one star on each side, it loses one space on the left.

Pyramid at n = 3line 1
  1. n = 3
  2. for i in range(1, n + 1):
  3. for s in range(n - i):
  4. print(" ", end="")
  5. for j in range(2 * i - 1):
  6. print("*", end="")
  7. print()

Variables

n
3 (just changed)

n is the number of rows to print.

Step 1 of 38

Look at row 3: n - i is 0, so range(0) is empty and the space loop runs zero times. The stars start at the left edge.

Quick check

Quick check: Pyramid with n = 6. What does row 4 print?

Now the same rule at n = 5, on a 5 by 9 grid (the widest row has 2 * 5 - 1 = 9 stars). Watch the blank cells on the left shrink by one each row.

Visualizer

Pyramid

Open full →

Inverted triangle: count down#

Flip the rule and the shape flips. Row i gets n - i + 1 stars, so at n = 5 the rows print 5, 4, 3, 2, 1 stars. The + 1 is there because rows start at 1: row 1 must print all n stars.

Visualizer

Inverted Triangle

Open full →

The cursor sweeps the same 25 cells as in the right triangle. Only the rule for which cells get a star has changed.

Quick check

Quick check: Inverted triangle, n = 5, rows counted from 1. How many stars on row 2?

One rule per row#

Every pattern asks two things: how many rows, and on row i, how many spaces and how many stars?

ShapeRow i printsRow 3, n = 5
Right trianglei stars3 stars
Pyramidn - i spaces, then 2 * i - 1 stars2 spaces, 5 stars
Inverted trianglen - i + 1 stars3 stars

Diamond: a pyramid plus an upside-down pyramid#

A diamond needs no new rule. Put one pyramid row into a function, row(i, n). Print rows 1 to n, then the pyramid's rows again in reverse order, for i = n - 1 down to 1. The bottom half is not the inverted triangle above: it keeps the pyramid's spaces and odd star counts.

Python

In range(n - 1, 0, -1), the third number, -1, is the step: count down by 1, stopping before 0. At n = 3 it gives 2, then 1. The widest row is row n, and this loop starts at n - 1, so it is not printed twice.

RowiSpacesStars
1121
2213
3305
4213
5121

A diamond has 2 * n - 1 rows. At n = 5 that is 9 rows on a 9 by 9 grid. The bottom four rows repeat the top four in reverse.

Visualizer

Diamond

Open full →

Butterfly: two wings and a gap#

A butterfly is two right triangles, back to back, with a gap between them. On row i of the top half, print i stars, then 2 * (n - i) spaces, then i stars. Every row is the same width: i + 2 * (n - i) + i is always 2 * n. The gap shrinks by 2 each row, so on row n it is 0 and the wings touch. The bottom half prints the same rows in reverse.

RowiStarsSpacesStars
11141
22222
33303
43303
52222
61141

That is n = 3. Step through it at n = 2 below, then drag n to 3 and check the table.

Butterflyline 1
Try another n
  1. def row(i, n):
  2. for j in range(i):
  3. print("*", end="")
  4. for s in range(2 * (n - i)):
  5. print(" ", end="")
  6. for j in range(i):
  7. print("*", end="")
  8. print()
  9. n = 3
  10. for i in range(1, n + 1):
  11. row(i, n)
  12. for i in range(n, 0, -1):
  13. row(i, n)

Variables

None yet

row(i, n) prints one row of the butterfly. It runs only when it is called.

Step 1 of 64

Quick check

Quick check: Butterfly with n = 4. How many spaces are on row 2?

Quick check

Quick check: The diamond's bottom loop started at n - 1. What happens if the butterfly's does too?

One last check#

Quick check

Quick check: A diamond with n = 4. What does its 6th row print?

Next, you write the right triangle yourself. It is the simplest rule, i stars on row i, so it is the place to get the two loops right.