Shape Rule
i..1 per row
Row with outer i = 1 prints 1; row with i = 5 prints 54321 — digits grow from the right.

Program 7 prints a reverse row number triangle: each row shows digits from the current row index down to 1 — 1, 21, 321, and so on. This tutorial covers the shape rule, ascending outer loop, inner countdown i..1, a live preview, worked Python examples, edge cases, and complexity.
i..1 per row
Row with outer i = 1 prints 1; row with i = 5 prints 54321 — digits grow from the right.
i = 1..rows
for i in range(1, rows + 1) — row width increases from one digit to rows digits.
j = i..1
for j in range(i, 0, -1) prints digits in reverse order on each row.
Same line / next line
Digits use print(j, end=""); end each row with print().
rows = 3..9
Pick row count and draw the reverse row triangle in the browser.
Complexity
Total prints = 1+2+…+n = n(n+1)/2 — a triangular number.
A reverse row number triangle grows digits from the right: each row prints numbers from the current row index down to 1. With rows = 5, you get 1, 21, 321, 4321, 54321.
In Python use an outer loop counting up from 1 to rows, an inner loop printing j from i down to 1, then print() after each row.
It pairs with Program 6’s left-growing triangle — the inner loop counts down instead of up, teaching reverse iteration.
i = 1..rows — narrow row first.
Countdown from i to 1.
Program 6 outer down, inner i..rows; Program 7 outer up, inner i..1.
Follow Program 6; continue to Program 8 next.
In short: outer i = 1..rows, inner j = i..1, print(j, end="") per digit, then print().
Given row count rows = 5, print a reverse row number triangle — row outer index i shows digits i..1.
# rows = 5
#1
#21
#321
#4321
#54321 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — also the widest row digit count. |
i (outer) | int | Current row index — runs 1 up to rows. |
j (inner) | int | Prints i..1 with print(j, end=""). |
| Row width | int | Row with outer i prints exactly i digits. |
| First row | int | Single digit 1 when i = 1. |
| Last row | string | Digits rows..1 when i = rows. |
for i from 1 to rows:
for j from i down to 1:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Ascending outer | for i in range(1, rows + 1) | Narrow-first row order |
| Inner i..1 | Countdown from i to 1 | Right-growing triangle |
| User-input rows | int(input()) | Flexible height |
| Compact trace | rows = 3 on paper first | Quick dry-runs |
| Spaced output | print(j, end="") | Readable columns |
| Goal | Pattern |
|---|---|
| Outer loop | for i in range(1, rows + 1) |
| Inner loop | for j in range(i, 0, -1): print(j, end="") |
| End row | print() |
| Program 6 contrast | Program 6: outer down, inner i..rows; Program 7: outer up, inner i..1 |
Same reverse row triangle — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
int(input())Read row count from console
rows = 3Quick dry-run on paper
i = 1..rowsAscending row index
j = i..1Countdown per row
Reach for this pattern when teaching ascending outer loops, inner countdown, and comparing shapes with Program 6.
Natural companion to Program 6 — same triangular print count, inner loop counts down instead of up.
Inner loop j-- from i to 1 — essential countdown practice.
Classic nested-loop question — explain outer up, inner countdown before coding.
Compare this right-growing triangle with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in ascending outers, inner countdown, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the reverse row number triangle in the browser.
Three complete Python programs — fixed rows, user input, and a compact trace with rows = 3. Click View Output to reveal sample console results.
Print five rows of the reverse row number triangle with nested loops.
rows = 5Hard-coded height — outer loop up, inner loop counts down each row.
rows = 5
for i in range(1, rows + 1):
for j in range(i, 0, -1):
print(j, end="")
print() Outer i runs 1 to 5 — inner j prints i down to 1 on each row.
Read row count from the user with validation.
Configurable height with int(input()) and a positive-rows check.
try:
rows = int(input("Enter the number of rows: "))
if rows <= 0:
raise ValueError
except ValueError:
print("Please enter a positive integer.")
else:
for i in range(1, rows + 1):
for j in range(i, 0, -1):
print(j, end="")
print() Same nested loops — only the row count comes from console input with safe parsing.
Use rows = 3 for a quick paper trace before larger triangles.
rows = 3 TraceSmall triangle — easy to dry-run on paper before scaling up.
rows = 3
for i in range(1, rows + 1):
for j in range(i, 0, -1):
print(j, end="")
print() Three rows, six total digits — trace i and j on paper before coding rows = 5.
int rows = 5; sets how many rows to print.
for i in range(1, rows + 1) moves from row 1 to row 5.
for j in range(i, 0, -1) prints digits in reverse order for each row.
print() moves to the next row after each line is printed.
Total printed digits follow triangular numbers: n(n+1)/2, so time complexity is O(n²).
rows = 5Trace each row — outer i sets width, inner j counts down from i to 1.
| Row (i) | Inner j values | Output line |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2, 1 | 21 |
| 3 | 3, 2, 1 | 321 |
| 4 | 4, 3, 2, 1 | 4321 |
| 5 | 5, 4, 3, 2, 1 | 54321 |
Total digits printed: 1+2+3+4+5 = 15 = 5×6/2 — the fifth triangular number.
Where this tiny pattern (and its countdown inner loop) shows up beyond the homework prompt.
Inner j-- from i to 1 — concrete reverse iteration practice.
Example: trace row 3 and watch j print 3, 2, 1.
Program 6 grows digits from the left; Program 7 grows from the right — same O(n²) total.
Example: print both patterns side by side for rows = 5.
Practice print(j, end="") vs print() 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: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with int(input()) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer up and inner countdown first — then write the loops.
Why this pattern earns a permanent spot in beginner Python courses.
Using j++ instead of j-- shows up immediately as wrong row order.
Only loops and console output — no arrays or math libraries.
Add spaces, right-align, or swap digits for stars with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row adds one digit on the right.
Small habits that keep reverse-row triangle code clean.
Use rows (or n) and keep i/j for row/column loops.
Avoid crashes when the user types letters instead of a number.
Only call print() after the inner loop finishes the row.
for j in range(i, 0, -1) matches “row i prints digits i..1” naturally.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if rows print ascending digits (12, 123, 1234), you used j++ instead of j--.
Mistakes that commonly break reverse row number triangles.
j++ prints ascending digits per row — 12, 123, 1234 instead of 21, 321, 4321.
→ Use for j in range(i, 0, -1).
All digits print on one long line without a row break.
→ Call print() after the inner loop.
Invalid input may print nothing or behave unexpectedly.
→ Validate rows > 0 before the loops.
Letters or empty input raise ValueError when int(input()) is unchecked.
→ Wrap in try/except ValueError and re-prompt on failure.
Each digit prints on its own line — vertical output instead of a triangle.
→ Use print(j, end="") inside, print() outside only.
Check these inputs before calling the solution done.
Prints only 1 — inner loop runs once with j = 1.
Outer loop never runs — print nothing or show a message.
Output 1 then 21 — good quick test.
Reject with validation — outer loop condition fails silently otherwise.
Bare int(input()) raises ValueError — use try/except.
Still O(n²) prints — cap rows for console demos.
Try these variations to lock in the pattern.
print(j, end="")rows = 3 before codingi prints exactly i digits — the triangle widens from the right.n(n+1)/2 — a triangular number. For rows = 5, that is 15 digits.1..i ascending; Program 7 prints i..1 descending — mirror per-row logic.rows down to 1 — e.g. 54321 when rows = 5.Quick Takeaway: outer i = 1..rows, inner j = i..1, print(j, end=""), then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Compact trace (Example 3) | O(rows²) | O(1) |
The reverse row number triangle is a compact nested-loop exercise: outer counts up, inner counts down, and each row grows one digit wider from the right. Master the fixed rows = 5 version, then try user input with int(input()) and the compact rows = 3 trace.
Practice the three examples above, then continue to Program 8 for the next pattern in the series.
Use range(i, 0, -1) for reverse digits per row, keep print() outside the inner loop, and validate row count when reading from the console.
for j in range(i, 0, -1)print() after each inner looprows > 0 for user inputrows = 3 on paper firstrange(1, i + 1) when the pattern needs countdownprint() inside the inner looprows = 3 dry-run before larger demosPrint the reverse row number triangle the beginner-friendly way.
Row i prints i..1
Definitioni = 1..rows
Loopj = i..1 countdown
Loopprint then newline
I/OO(n²) time
AnalysisEach row prints digits in reverse order — outer i runs 1..rows, inner j counts down from i to 1 — producing 1, 21, 321, and so on. Total prints grow as O(n²).
Move on to the next pattern in the Python number-pattern series.
11 people found this page helpful