Shape Rule
Full diamond
Top half: row i prints digit i on mirror diagonals. Bottom half: same logic with i counting down.

Program 58 prints a diagonal mirror number diamond: the top half grows from 1 to rows like Program 57, then a second outer loop mirrors back down to 1. This tutorial covers top/bottom halves, conditional diagonal printing, a live preview, worked Python examples, edge cases, and complexity.
Full diamond
Top half: row i prints digit i on mirror diagonals. Bottom half: same logic with i counting down.
i = 1..rows
for i in range(1, rows + 1) — same pyramid half as Program 57.
i = rows-1..1
for i in range(rows - 1, 0, -1) — mirrors the top half without repeating the peak row.
j = rows..1
print(i if i == j else " ", end="") — reused in both outer loops.
k = 2..rows
print(i if i == k else " ", end="") — mirrors the left half from column 2 onward.
Complexity
2×rows-1 lines, each scanning about 2×rows-1 positions — total work grows as O(n²).
A diagonal mirror number diamond extends Program 57’s pyramid: print the top half from 1 to rows, then mirror back down with a second outer loop from rows-1 to 1. With rows = 5, you get nine lines — peak at row 5, then symmetric descent to a single 1.
Each row reuses Program 57’s inner loops: left diagonal j = rows..1, right diagonal k = 2..rows, printing the digit only when i == j or i == k.
It bridges Program 57’s single pyramid to full symmetry — one extra outer loop turns a half-pattern into a complete diamond.
i = 1..rows — pyramid grows upward.
i = rows-1..1 — mirror without repeating peak.
Program 57 is the top half only; Program 58 adds the mirrored bottom loop.
Follow Program 57; continue to Program 59 next.
In short: top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic per row, then print().
Given row count rows = 5, print a diagonal mirror number diamond — top half 1..rows, bottom half rows-1..1, with mirror diagonals on every line.
# rows = 5
// 1
// 2 2
// 3 3
// 4 4
// 5 5
// 4 4
// 3 3
// 2 2
// 1 | Item | Type | Description |
|---|---|---|
rows | int | Half-height — diamond has 2×rows-1 total lines. |
i (top outer) | int | Runs 1 to rows — builds the upper half. |
i (bottom outer) | int | Runs rows-1 down to 1 — mirrors without repeating peak. |
j (left) | int | Scans rows..1 — prints digit when i == j. |
k (right) | int | Scans 2..rows — prints digit when i == k. |
| Total lines | int | rows + (rows - 1) = 2×rows - 1. |
for i from 1 to rows:
print row i with left and right diagonal logic
for i from rows - 1 down to 1:
print row i with same inner loop logic | Approach | Idea | Best for |
|---|---|---|
| Two outer loops | Top 1..rows, bottom rows-1..1 | Learning and interviews |
| Reuse inner logic | Same j and k loops in both halves | DRY diamond patterns |
| User-input rows | int(input()) | Flexible diamond size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Character swap | Replace digit with * for an X-diamond | Visual debugging |
| Goal | Pattern |
|---|---|
| Top outer loop | for i in range(1, rows + 1) |
| Bottom outer loop | for i in range(rows - 1, 0, -1) |
| Left diagonal | for j in range(rows, 0, -1): print(i if i == j else " ", end="") |
| Right diagonal | for k in range(2, rows + 1): print(i if i == k else " ", end="") |
| End row | print() |
| Program 57 contrast | Program 57 is top half only; Program 58 adds bottom mirror loop |
Same diagonal mirror diamond — three ways to set row count and trace the logic.
rows = 5Hard-coded half-height for demos
int(input())Read row count from console
rows = 35-line diamond dry-run
i = 1..rowsProgram 57 pyramid logic
i = rows-1..1Mirror without peak repeat
Reach for this pattern when teaching symmetry, mirroring loops, and extending a half-pattern into a full diamond.
Natural follow-up after Program 57’s pyramid — one extra outer loop completes the diamond.
Top and bottom halves share inner logic — good bridge to palindrome and mirror problems.
Separate top and bottom boundaries — concrete loop-boundary practice.
Compare this hollow 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 extension that locks in mirroring, symmetry, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered diagonal mirror number 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 diagonal mirror diamond with half-height five — top loop 1..rows, bottom loop rows-1..1.
rows = 5Hard-coded half-height — print the top pyramid, then mirror with a second outer loop using the same inner diagonal logic.
rows = 5
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
print(i if i == j else " ", end="")
for k in range(2, rows + 1):
print(i if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(rows, 0, -1):
print(i if i == j else " ", end="")
for k in range(2, rows + 1):
print(i if i == k else " ", end="")
print() The first outer loop prints rows 1 through 5 (Program 57 logic). The second outer loop prints rows 4 down to 1 — reusing the same inner loops so the bottom half mirrors the top without repeating row 5.
Read half-height from the console with safe parsing.
Read rows from the console with int(input()) — reject invalid input gracefully.
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(rows, 0, -1):
print(i if i == j else " ", end="")
for k in range(2, rows + 1):
print(i if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(rows, 0, -1):
print(i if i == j else " ", end="")
for k in range(2, rows + 1):
print(i if i == k else " ", end="")
print() Same top-and-bottom outer loops as Example 1; only the source of rows changes from a literal to user input.
Smaller half-height for quick tracing on paper or in interviews.
rows = 3Use rows = 3 for a 5-line diamond — trace both outer loops before scaling to 5.
rows = 3
for i in range(1, rows + 1):
for j in range(rows, 0, -1):
print(i if i == j else " ", end="")
for k in range(2, rows + 1):
print(i if i == k else " ", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(rows, 0, -1):
print(i if i == j else " ", end="")
for k in range(2, rows + 1):
print(i if i == k else " ", end="")
print() With half-height 3 you get 5 total lines — enough to trace top loop, peak row, and bottom mirror on paper before the full demo.
rows = 5 is half-height — the diamond prints 2×rows-1 = 9 lines.
for i in range(1, rows + 1) — Program 57 pyramid logic with left and right diagonal inner loops.
for i in range(rows - 1, 0, -1) — same inner loops, counting down to avoid repeating the peak row.
Left j = rows..1, right k = 2..rows — print digit when i == j or i == k, else space.
2×rows-1 lines total — O(n²) time, O(1) extra memory.
rows = 5Trace each line’s half (top or bottom), row index, and diagonal hits — nine lines total.
| Line | Half | i | Left hit | Right hit |
|---|---|---|---|---|
| 1 | Top | 1 | j=1 | (none) |
| 2 | Top | 2 | j=2 | k=2 |
| 3 | Top | 3 | j=3 | k=3 |
| 4 | Top | 4 | j=4 | k=4 |
| 5 | Top (peak) | 5 | j=5 | k=5 |
| 6 | Bottom | 4 | j=4 | k=4 |
| 7 | Bottom | 3 | j=3 | k=3 |
| 8 | Bottom | 2 | j=2 | k=2 |
| 9 | Bottom | 1 | j=1 | (none) |
The bottom loop starts at rows-1 so line 5 (peak) is not printed twice — total lines = 2×rows-1.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Two inner loops with conditional printing — classic diagonal placement drill.
Example: trace each row in the walkthrough table — left hit, right hit.
Each row mirrors digits on two diagonals — compare with Program 53’s single diagonal V-shape.
Example: row 5 prints 5 at column 5 and again at column 9.
Practice print(..., end="") vs print() with digit-or-space decisions per column.
Example: put print() inside the inner loop by mistake.
Starting the right loop at 2 avoids a third digit at the center — keeps exactly two prints per row.
Example: try k = 1 and see the center digit triple on some rows.
Each row scans about 2n positions — makes O(n²) concrete for beginners.
Example: row 5 with rows = 5 scans 9 character slots — see the walkthrough table.
Pair the pattern with try/except ValueError and positive-row validation.
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 Python courses.
The full diamond is instantly recognizable — top half grows, bottom half mirrors symmetrically.
Conditional digit-or-space printing teaches real console alignment — not abstract loop drill.
Swap digits for * to get an X-diamond, or try fixed-width formatting for rows beyond 9.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — 5 lines total, peak at row 3, then mirror back to 1.
Small habits that keep number-pattern code clean.
Print the digit when i == j; otherwise print a single space.
Avoid crashing when the user types letters instead of a number.
Only call print() after both inner loops finish the row.
Use for k in range(2, rows + 1) so the center position is not duplicated.
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 diagonal mirror number diamond patterns.
Each digit lands on its own line — you get a column, not a pyramid.
→ Use print(i if i == j else " ", end=""); call print() only after both inner loops.
Starting at k = 1 can print a third digit at the center — row looks crowded.
→ Use for k in range(2, rows + 1) — mirror from column 2 onward.
Using j == rows instead of i == j places digits on the wrong diagonal.
→ Always compare the outer row index i with the inner loop variable j or k.
All numbers print on one long line without row breaks.
→ Add print() after both inner loops complete.
Letters or empty input raise ValueError or leave rows unset.
→ Wrap int(input()) in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the right loop (k = 2..1) does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Bottom row has two copies of 5 across 9 positions — good for dry-runs before scaling up.
Bare int(input()) raises ValueError — wrap it in try/except.
Row 9 scans 17 character positions (2×9-1) — total work grows as O(n²).
Try these variations to lock in the pattern.
rows-1..1 for the full diamondrows and see the middle row print twicerows-1print("*", end="")i = 1..rows. Bottom: i = rows-1..1. Same inner diagonal logic in both.print(..., end="") stays on the line; print() advances — call it only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints one line — bottom loop does not run.2×rows-1 lines, each scanning about 2×rows-1 positions — total work grows as O(n²).Quick Takeaway: top 1..rows, bottom rows-1..1, same inner diagonal logic, then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Digits on row i | 2i - 1 | No storage beyond loop counters |
The diagonal mirror number diamond is a natural follow-up to Program 57: one extra outer loop mirrors the pyramid 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 59 for the next pattern in the series.
Total output is 2×rows-1 lines — peak at row rows, then mirrored descent to 1.
for j in range(rows, 0, -1) with if i == jfor k in range(2, rows + 1) with if i == kprint() after both inner loopstry/except ValueError around int(input()) for user inputk = 1 — can triple-print at centerj == rows instead of i == j — wrong diagonalprint() inside any inner looprows = 3 dry-run before coding rows = 5Print the full mirror-diagonal diamond the beginner-friendly way.
2×rows-1 lines
Definitionj = rows..1
Codek = 2..rows
Codei == j or i == k
LogicO(n²) time
AnalysisPrint the Program 57 pyramid for the top half, then mirror with for i in range(rows - 1, 0, -1). Total lines = 2×rows-1 — each row scans about 2×rows-1 positions.
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful