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:
| Code | What 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.
n- n = 3
- for i in range(1, n + 1):
- for j in range(i):
- print("*", end="")
- print()
Variables
- n
- 3 (just changed)
n is the number of rows to print.
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.
- n = 3
- for i in range(1, n + 1):
- for s in range(n - i):
- print(" ", end="")
- for j in range(2 * i - 1):
- print("*", end="")
- print()
Variables
- n
- 3 (just changed)
n is the number of rows to print.
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.
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.
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?
| Shape | Row i prints | Row 3, n = 5 |
|---|---|---|
| Right triangle | i stars | 3 stars |
| Pyramid | n - i spaces, then 2 * i - 1 stars | 2 spaces, 5 stars |
| Inverted triangle | n - i + 1 stars | 3 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.
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.
| Row | i | Spaces | Stars |
|---|---|---|---|
| 1 | 1 | 2 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 0 | 5 |
| 4 | 2 | 1 | 3 |
| 5 | 1 | 2 | 1 |
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.
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.
| Row | i | Stars | Spaces | Stars |
|---|---|---|---|---|
| 1 | 1 | 1 | 4 | 1 |
| 2 | 2 | 2 | 2 | 2 |
| 3 | 3 | 3 | 0 | 3 |
| 4 | 3 | 3 | 0 | 3 |
| 5 | 2 | 2 | 2 | 2 |
| 6 | 1 | 1 | 4 | 1 |
That is n = 3. Step through it at n = 2 below, then drag n to 3 and check the table.
n- def row(i, n):
- for j in range(i):
- print("*", end="")
- for s in range(2 * (n - i)):
- print(" ", end="")
- for j in range(i):
- print("*", end="")
- print()
- n = 3
- for i in range(1, n + 1):
- row(i, n)
- for i in range(n, 0, -1):
- row(i, n)
Variables
None yet
row(i, n) prints one row of the butterfly. It runs only when it is called.
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.