Shape Rule
Full diamond
Top half grows from 1 to rows; bottom half mirrors from rows-1 back to 1 — total 2n-1 lines.

Program 54 prints a mirror diagonal diamond pattern: the top half matches Program 53’s V-shape, then a second outer loop mirrors it downward to form a full diamond — a natural step after Program 53’s mirror diagonal pattern. This tutorial covers two outer loops, i == j and i == k conditions, a live preview, worked Python examples, edge cases, and complexity.
Full diamond
Top half grows from 1 to rows; bottom half mirrors from rows-1 back to 1 — total 2n-1 lines.
i = 1..rows
for i in range(1, rows + 1): prints the upper V-half — same logic as Program 53.
i = rows-1..1
for i in range(rows - 1, 0, -1): mirrors the top half without duplicating the peak row.
i == j
print(i if i == j else " ", end="") — digit on the main diagonal each row.
i == k
print(i if i == k else " ", end="") — mirrored diagonal; right loop starts at rows-1.
Complexity
About 2n-1 lines, each scanning 2n-1 positions — total work grows as O(n²).
A mirror diagonal diamond pattern extends Program 53’s V-shape: print the top half from 1 to rows, then mirror the same row logic from rows-1 back to 1. With rows = 5, you get nine lines ending with 1 1 at the bottom.
In Python, use two outer loops — top and bottom — each with left (i == j) and right (i == k) inner loops, printing spaces elsewhere before print().
It bridges Program 53’s single V-half to full symmetry — teaching how to mirror loop ranges without duplicating the peak row.
i = 1..rows — same as Program 53.
i = rows-1..1 — mirrors without duplicating the peak.
Program 53 stops at the V tip; Program 54 adds a second outer loop to complete the diamond.
Follow Program 53; continue to Program 55 next.
In short: top loop for i in range(1, rows + 1):, bottom loop for i in range(rows - 1, 0, -1):, each row uses i == j and i == k, then print().
Given row count rows = 5, print a mirror diagonal diamond — top half grows to rows, bottom half mirrors back to 1.
# rows = 5
//1 1
// 2 2
// 3 3
// 4 4
// 5
// 4 4
// 3 3
// 2 2
//1 1 | Item | Type | Description |
|---|---|---|
rows | int | Peak row of the diamond (total lines = 2n-1). |
i (top outer) | int | Runs 1 to rows for the upper half. |
i (bottom outer) | int | Runs rows-1 down to 1 for the lower half. |
j (left) | int | Scans 1..rows; prints digit when i == j. |
k (right) | int | Scans rows-1..1; prints digit when i == k. |
| Total lines | int | 2 * rows - 1 lines for a complete diamond. |
for i from 1 to rows:
print row with i == j and i == k logic
for i from rows - 1 down to 1:
print same row logic (mirror bottom half) | Approach | Idea | Best for |
|---|---|---|
| Two outer loops | Top 1..rows, bottom rows-1..1 | Learning symmetry and loop bounds |
| Reuse row logic | Same inner loops in both outer loops | DRY diamond construction |
| User-input rows | int(input()) | Flexible diamond size |
| Compact trace | rows = 3 on paper first | Quick dry-runs (5 lines total) |
| Extract row method | PrintRow(i, rows) called twice | Cleaner code after mastering loops |
| Goal | Pattern |
|---|---|
| Top outer loop | for i in range(1, rows + 1): |
| Bottom outer loop | for i in range(rows - 1, 0, -1): |
| Left half | for j in range(1, rows + 1): print(i if i == j else " ", end="") |
| Right half | for k in range(rows - 1, 0, -1): print(i if i == k else " ", end="") |
| End row | print() |
| Program 53 contrast | Program 53 prints top V-half only; Program 54 adds bottom mirror loop |
Same diamond — three ways to set row count and trace the symmetry.
rows = 5Hard-coded peak for demos (9 lines)
int(input())Read peak row from console
rows = 35-line diamond for paper tracing
i = 1..rowsUpper V — same as Program 53
i = rows-1..1Mirror without duplicating peak
Reach for this pattern when teaching symmetry, mirrored loop ranges, and completing a V-shape into a diamond.
Natural follow-up after Program 53’s V-half — adds the bottom mirror loop to complete the diamond.
Teaches why the bottom loop starts at rows-1 — a pattern used in many diamond and pyramid programs.
About 2n-1 lines, each scanning 2n-1 positions — concrete O(n²) complexity.
Compare this number diamond 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 symmetry, mirrored loop bounds, and O(n²) thinking.
Choose peak row count between 3 and 9 and draw the full mirror diagonal diamond in the browser.
Three complete Python programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Print a full mirror diagonal diamond with peak row 5 — top V-half plus mirrored bottom half.
rows = 5Hard-coded peak row — top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic each row.
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows + 1):
print(i if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(i if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
print(i if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(i if i == k else " ", end="")
print() The first outer loop prints rows 1 through 5 (Program 53’s V-half). The second outer loop prints rows 4 down to 1, reusing the same inner loops — nine lines total without duplicating row 5.
Read peak row count with int(input()) and validation.
Read rows with int(input()) and validate the result.
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive integer.")
else:
if rows <= 0:
print("Please enter a positive integer.")
else:
for i in range(1, rows + 1):
for j in range(1, rows + 1):
print(i if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(i if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
print(i if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(i if i == k else " ", end="")
print() Same two-outer-loop diamond core as Example 1; only the source of rows changes from a literal to user input.
Smaller peak row for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace top loop, bottom loop, and symmetry before scaling to 5 rows.
rows = 3
for i in range(1, rows + 1):
for j in range(1, rows + 1):
print(i if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(i if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows + 1):
print(i if i == j else " ", end="")
for k in range(rows - 1, 0, -1):
print(i if i == k else " ", end="")
print() Five lines total — top 3 rows plus bottom 2 — let you trace both outer loops on paper before running the full rows = 5 demo.
int rows = 5; — the diamond will have 2*rows-1 = 9 lines.
for i in range(1, rows + 1): — same V-half logic as Program 53.
Left loop i == j, right loop i == k — spaces fill all other columns.
for i in range(rows - 1, 0, -1): reuses the same inner loops — skips the peak row.
2n-1 lines, each about 2n-1 characters — O(n²) time, O(1) extra memory.
rows = 5Trace each line’s outer-loop phase, diagonal positions, and full output.
| Line | Phase | i | Row output |
|---|---|---|---|
| 1 | Top | 1 | 1 1 |
| 2 | Top | 2 | 2 2 |
| 3 | Top | 3 | 3 3 |
| 4 | Top | 4 | 4 4 |
| 5 | Top (peak) | 5 | 5 |
| 6 | Bottom | 4 | 4 4 |
| 7 | Bottom | 3 | 3 3 |
| 8 | Bottom | 2 | 2 2 |
| 9 | Bottom | 1 | 1 1 |
The bottom loop starts at i = rows - 1 so line 5 (the peak) is not printed twice.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Two outer loops mirror the same row logic — top then bottom.
Example: trace lines 1–9 in the walkthrough table.
The bottom loop starting at rows-1 is a classic symmetry trick used in many diamond patterns.
Example: line 5 is the peak; lines 6–9 mirror lines 4–1.
Practice print(i if i == j else " ", end="") vs print() with two inner loops per row.
Example: put print() inside the inner loop by mistake.
Total lines = 2n-1 — links symmetry to loop-bound formulas.
Example: Peak row 10 produces 19 lines total.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: Peak row 5 produces 9 lines — see the walkthrough table.
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/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Starting the bottom loop at rows duplicates the peak row immediately.
Two outer loops teach real symmetry — not abstract loop drill.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — 5 lines total, peak at line 3.
Small habits that keep number-pattern code clean.
Scan all columns in the left half — print digit only when i == j.
Avoid crashes when the user types letters instead of a number.
Only call print() after both inner loops finish the row.
Start the right loop at rows - 1 to skip duplicating the center column.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put print() inside the inner loop.
Mistakes that commonly break mirror diagonal diamond patterns.
Each character lands on its own line — you get a column, not a diamond.
→ Use print(i if i == j else " ", end="") in both loops; print() only after both inner loops.
Starting the bottom outer loop at i = rows prints the peak row twice.
→ Use for i in range(rows - 1, 0, -1): for the bottom half.
The middle row of the diamond appears twice — breaking symmetry.
→ Start the bottom outer loop at rows - 1, not rows.
All numbers print on one long line without row breaks.
→ Add print() after both inner loops complete.
Letters or empty input raise ValueError when int(input()) is unchecked.
→ Wrap in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the right loop does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Peak row 5 produces 9 lines — good for dry-runs.
Unchecked int(input()) raises ValueError — use try/except.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
2n-1PrintRow(i, rows)i = 1..rows. Bottom: i = rows-1..1. Same inner loops in both.print(..., end="") stays on the line; print() advances — call it after both inner loops finish each row.rows > 0 for interactive programs; total output lines = 2*rows - 1.2n-1 lines, each scanning 2n-1 positions — total work is O(n²).Quick Takeaway: top loop for i in range(1, rows + 1):, bottom loop for i in range(rows - 1, 0, -1):, each row uses i == j and i == k, then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Total lines | 2n - 1 | About 2n-1 chars per line |
The mirror diagonal diamond pattern is a natural follow-up to Program 53: add a second outer loop to mirror the V-half downward into a full symmetric diamond. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 55 for the next pattern in the series.
Total lines = 2n-1 — bottom loop starts at rows-1 to avoid duplicating the peak.
for i in range(1, rows + 1):for i in range(rows - 1, 0, -1):print() after both inner loops each rowtry/except for user inputi = rows — duplicates the peak rowprint() inside either inner looprows = 3 dry-run before coding rows = 5Print the full diamond the beginner-friendly way.
Top V + mirrored bottom
Definitioni = 1..rows
Codei = rows-1..1
Code2n - 1 lines
LogicO(n²) time
AnalysisProgram 53’s V-shape becomes a full diamond by adding a second outer loop from rows-1 down to 1. Total lines = 2n-1 with about 2n-1 characters per line — O(n²) overall.
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful