Step 01 Beginner Problems — Patterns
Pattern 3 — Right-Angled Number Pyramid
The growing star triangle of Pattern 2 re-skinned with numbers: every row lists the counting numbers 1, 2, 3, ... up to the row's length. It teaches you to generate a row's tokens from a formula on the position inside the row (the column j) instead of repeating one fixed token.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
How to think about it
Number the rows i = 0, 1, ..., n-1. Row i has i + 1 entries, and the entries are exactly 1, 2, 3, ..., i+1 — so the entry in column j (0-based) of row i is always j + 1, independent of the row. The outer loop fixes the row length (i + 1); the inner loop walks j from 0 to i and emits str(j + 1). The last entry of row i is i + 1, which is also the row's length — a handy self-check while dry-running.
Code
The inner list comprehension builds [str(j + 1) for j in range(i + 1)] — the numbers 1 through i + 1 as strings — and the row is printed joined by spaces. Converting to str before joining is essential: you cannot join integers with a string separator.
def pattern(n):
for i in range(n):
nums = [str(j + 1) for j in range(i + 1)]
print(" ".join(nums))Key points
- Row i holds i + 1 numbers; the value at column j is j + 1 regardless of the row.
- Two loops, two jobs: the outer sets the length, the inner generates values from the column index.
- Every row restarts at 1 — the counter is the column, not a running total (contrast Pattern 13).
- Convert numbers to strings before ' '.join, or Python raises a TypeError.
- Last value of row i equals i + 1 — use it as a quick correctness check.