Shape Rule
Sequence + fill
Row 1 prints 5 5 5 5 5, row 2 prints 4 5 5 5 5, row 3 prints 3 4 5 5 5, and so on.

The fill-with-5 number triangle pads each row with the maximum value so every line has width n — a natural step after alternating odd/even patterns. This tutorial covers the shape rule, two inner loops, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Sequence + fill
Row 1 prints 5 5 5 5 5, row 2 prints 4 5 5 5 5, row 3 prints 3 4 5 5 5, and so on.
n..1
for i in range(n, 0, -1) walks rows from the top (all fill) down to the full sequence.
i..n then pad
for j in range(i, n + 1) prints the sequence; for j in range(1, i) fills with n.
Same line / next line
Numbers use print(j, end=" ") or print(n, end=" "); end each row with print().
1–15 width
Pick a triangle width and draw the fill-with-n pattern instantly in the browser.
Complexity
Each of n rows prints n numbers — total prints = n²; extra memory stays O(1).
A fill-with-5 number triangle prints an ascending sequence on each row, then pads the rest with the maximum value so every row has the same width. With n = 5, the output is 5 5 5 5 5, 4 5 5 5 5, 3 4 5 5 5, 2 3 4 5 5, 1 2 3 4 5.
In Python you use a descending outer loop, print j from i to n in the first inner loop, fill remaining slots with n in the second inner loop, then print() ends each row.
It combines two inner loops with fixed row width — a step up from Program 18.
for j in range(i, n + 1) prints ascending numbers.
for j in range(1, i) pads with n.
print(j, end=" ") or print(n, end=" ") in inner loops; print() after.
Follow Program 18; continue to Program 20 (continuous number triangle).
In short: for each i from n down to 1, print j from i to n, fill i - 1 times with n, then call print().
Given a positive integer n, print a fill-with-n triangle: each row prints an ascending sequence from i to n, then pads with n so every row has width n.
# n = 5 (conceptual shape)
# 5 5 5 5 5
# 4 5 5 5 5
# 3 4 5 5 5
# 2 3 4 5 5
# 1 2 3 4 5
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ") # sequence i..n
for j in range(1, i):
print(n, end=" ") # pad with n
print() | Item | Type | Description |
|---|---|---|
n | int | Triangle width and fill value (typically ≥ 1). |
| Printed output | text | Each row has n spaced numbers — sequence then padding. |
for i from n down to 1:
for j from i to n:
print j + space
for j from 1 to i - 1:
print n + space
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | 5 5 5 5 5, 4 5 5 5 5, … | Learning and interviews |
Variable n | n = int(input(...)) | User-input version |
| Custom fill | Separate fill constant | Pad with a value other than n |
| Goal | Pattern |
|---|---|
| Walk each row | for i in range(n, 0, -1) |
| Print sequence | for j in range(i, n + 1): print(j, end=" ") |
| Fill padding | for j in range(1, i): print(n, end=" ") |
| End the row | print() |
| User input | n = int(input(...)) |
| Custom fill value | print(fill, end=" ") in second loop |
Same fill-with-n triangle — different ways to structure the padding.
j = i..nFirst inner loop prints ascending numbers
pad nSecond loop runs i - 1 times with n
inputReplace hard-coded 5 with user input in Example 2
width nEvery row must print exactly n numbers
Reach for this pattern when teaching two inner loops and fixed-width row padding.
Natural follow-up after Program 18 — combines sequence printing with right padding.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Compare Program 18 (alternating odd/even) and Program 20 (continuous counter triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a triangle width between 1 and 15 and draw the fill-with-n pattern in the browser.
Three complete Python programs — fixed width, user input, and custom fill constant. Click View Output to reveal sample console results.
Print five rows of the fill-with-5 triangle with two inner loops.
n = 5Hard-coded width — ideal for first demos and screenshots.
n = 5
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ")
for j in range(1, i):
print(n, end=" ")
print() When i = 5, the sequence loop prints 5 once, then the fill loop runs 4 times — all 5s. When i = 3, the sequence prints 3 4 5, then two 5s pad the row. When i = 1, the sequence prints 1 2 3 4 5 with no fill needed. print() after both inner loops starts the next row.
Read the triangle width with input() instead of hard-coding 5.
Read n with input() and int() (wrap in try/except ValueError in real apps); the fill value matches the width.
n = int(input("Enter the triangle width: "))
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ")
for j in range(1, i):
print(n, end=" ")
print() Same nested-loop core as Example 1; only the source of n changes. Both the sequence end bound and the fill value use the same variable. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Use a separate fill constant instead of always padding with n.
Pad with fill = 9 while the sequence still runs up to n = 5.
n = 5
fill = 9
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end=" ")
for j in range(1, i):
print(fill, end=" ")
print() Replace n with fill in the second inner loop only. The sequence loop still prints j from i to n; padding uses the custom constant.
print is built in; use input() when reading input. Set n (fixed or from input).
for i in range(n, 0, -1) walks from the all-fill top row down to the full sequence.
Print j from i to n, then pad i - 1 times with n (or a custom fill value).
print() ends the row so the next outer iteration starts fresh.
Total prints: n² — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, the sequence printed, the fill count, and the final row.
i | Sequence (j = i..n) | Fill count (i - 1) | Row output |
|---|---|---|---|
5 | 5 | 4 | 5 5 5 5 5 |
4 | 4, 5 | 3 | 4 5 5 5 5 |
3 | 3, 4, 5 | 2 | 3 4 5 5 5 |
2 | 2, 3, 4, 5 | 1 | 2 3 4 5 5 |
1 | 1, 2, 3, 4, 5 | 0 | 1 2 3 4 5 |
Total number prints: 5 + 5 + 5 + 5 + 5 = 25 = 5².
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change range(1, i + 1) and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice print(..., end=" ") vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: use print(j, end=" ") for spaced numbers on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with try/except ValueError around int(input()) and positive-width checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the sequence loop first, then add the fill loop — compare with custom fill in Example 3.
Small habits that keep number-pattern code clean.
Use n for both width and fill value unless you need a custom constant.
try/except ValueErrorWrap int(input()) in try/except ValueError so bad input does not leave n unset.
Only call print() after the inner loop finishes the row.
Write row i, sequence j = i..n, and fill count i - 1 before coding.
Trace n = 5 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.
Mistakes that commonly break fill-with-n number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(j, end=" ") or print(n, end=" "); print() only after both inner loops.
Rows have different widths — the top row may be short while the bottom is full.
→ Add for j in range(1, i) to pad with n after the sequence loop.
range(1, i + 1) in the fill loop prints too many padding values.
→ Use range(1, i) so the fill runs exactly i - 1 times.
Omitting print() glues every number onto one endless line.
→ Always end the row after both inner loops.
Letters or empty input raise ValueError with bare int(input()).
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no fill needed.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
int(input()) raises ValueError — validate with try/except first.
Sequence must run range(i, n + 1), not range(1, i + 1).
Without the fill loop, top rows are shorter than the bottom row.
Try these variations to lock in the pattern.
i % 2 and k += 2nn² — each of n rows prints n numbers.print(j, end=" ") stays on the line; print() advances — mix them carefully.n > 0 for interactive programs; n = 1 should print a single 1.Quick Takeaway: descending outer loop, print sequence j = i..n, fill i - 1 times with n, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Custom fill (Example 3) | O(n²) | O(1) |
The fill-with-5 number triangle is a compact lesson in two inner loops: the first prints an ascending sequence, the second pads with the maximum value so every row has width n. Master the fixed-n version, then try user input and a custom fill constant.
Practice the three examples above, then continue to Program 20 for the continuous number triangle.
Run the sequence loop first, then the fill loop — use j < i for padding and validate n when reading input.
print(j, end=" ") and print(n, end=" ")n ≥ 1 for interactive programsint(input()) in try/except ValueError before using nprint() inside the inner digit looprange(1, i + 1) in the fill loopn = 1 edge casePrint the pattern the beginner-friendly way.
Sequence then fill
Definitioni = n..1
j = i..n
Pad i - 1 times
O(n²) time
AnalysisEach row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total prints.
Move on to the continuous number triangle in the Python number-pattern series.
12 people found this page helpful