Python Mirror Number Pattern (0-Centered)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A 0-centered descending mirror prints ascending digits, a fixed 0, then descending digits — rows grow as the outer index drops toward 1.

Remember
Rule: for i = max+1 down to 1
      print i..max, then 0, then max..i

0
909
89098
…
1234567890987654321     ← max = 9

Unlike Program 27’s growing peak, the center is always 0 — a natural step after the palindrome triangle in Program 27.

How to Solve It

Count i down from max + 1; print i..max, then 0, then max..i.

MethodIdeaBest for
Three partsAscend, 0, descendLearning, interviews
input() maxSame loops; clamp max to 1–9Interactive practice
Spaced digitsprint(..., end=" ") in both halvesReadable output

Pseudocode

Pseudocode
for i from max + 1 down to 1:
    for j from i to max:
        print j (same line)
    print 0 (same line)
    for k from max down to i:
        print k (same line)
    print newline

Cheat sheet

GoalPattern
Set maxmax_n = 9
Outer loopfor i in range(max_n + 1, 0, -1):
Left halffor j in range(i, max_n + 1): print(j, end="")
Centerprint("0", end="")
Right halffor k in range(max_n, i - 1, -1): print(k, end="")
End rowprint()
Spacedprint(j, end=" ") / print("0 ", end="")

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="") / print("0", end="")Stays on the same lineEach digit
print()Ends the current lineAfter all three parts

Glue digits with end="", then break once with print(). Putting print() inside an inner loop prints one digit per line.

Live Preview

Change the max digit and the 0-centered mirror updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result max = 5 · rows = 6
0
505
45054
3450543
234505432
12345054321

Worked Walkthrough — max_n = 3

Trace each outer i, the left half, the center 0, and the right mirror.

iLeft i..3CenterRight 3..iPrinted row
4none0none0
3303303
22303223032
112303211230321

Outer i starts at max + 1 so the first row is only the center zero.

Python Programs

Three complete programs: fixed max = 9, input() max digit, and spaced digits. Use View Output for sample results.

Example 1 — Fixed max = 9

Hard-coded max digit — outer i = 10..1, left i..9, center 0, right 9..i.

Python
for i in range(10, 0, -1):
    for j in range(i, 10):
        print(j, end="")
    print("0", end="")
    for k in range(9, i - 1, -1):
        print(k, end="")
    print()

How It Works

1. Outer countdown. i runs from 10 down to 1 — start of the left half (or empty when i = 10).

2. Left then center. Print j from i to 9, then print "0".

3. Right mirror. Print k from 9 down to i; then print() ends the row.

Example 2 — input() Max Digit

Read the max digit at runtime; clamp to 1–9 and start the outer loop at max_n + 1.

Python
max_n = int(input("Enter max digit (1-9): "))

if max_n < 1:
    max_n = 1
if max_n > 9:
    max_n = 9

for i in range(max_n + 1, 0, -1):
    for j in range(i, max_n + 1):
        print(j, end="")
    print("0", end="")
    for k in range(max_n, i - 1, -1):
        print(k, end="")
    print()

How It Works

1. Read and clamp. Parse the answer; keep max_n in 1–9.

2. Generalize bounds. Outer starts at max_n + 1; both halves use max_n instead of 9.

3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:

Safer input
raw = input("Enter max digit (1-9): ").strip()
try:
    max_n = int(raw)
except ValueError:
    print("Enter a digit from 1 to 9.")
    raise SystemExit(1)
if max_n < 1 or max_n > 9:
    print("Enter a digit from 1 to 9.")
    raise SystemExit(1)

Example 3 — Spaced Digits

Keep max_n = 5 but print a space after each digit (and after 0).

Python
max_n = 5

for i in range(max_n + 1, 0, -1):
    for j in range(i, max_n + 1):
        print(j, end=" ")
    print("0", end=" ")
    for k in range(max_n, i - 1, -1):
        print(k, end=" ")
    print()

How It Works

1. Same structure. Outer countdown and both half-loops match Example 2.

2. Only end= changes. Use end=" " for left digits, center 0, and right digits.

3. Same shape. Values and row count stay the same — only spacing differs.

Edge Cases & Pitfalls

Check these before calling the solution done.

no 0

Missing center

Forgetting print("0", end="") glues left and right halves together without the zero pivot.

i = max

Wrong outer start

Starting at i = max_n skips the lone 0 row. Use range(max_n + 1, 0, -1).

print() inside

Vertical digits

If print() is inside either half-loop, each digit lands on its own line. Call it only after all three parts.

max_n = 1

Smallest mirror

Output is 0 then 101.

max > 9

Multi-digit cells

Values above 9 print as two characters and break the single-digit look. Clamp to 9 in input examples.

input()

Catch ValueError

Letters crash bare int(input()) — wrap in try/except and require 1–9.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

About n + 1 rows, each printing up to 2n + 1 digits → O(n²).

Key Takeaways

  • Rule: print i..max, then 0, then max..i.
  • Start at max+1: first row is only the center zero.
  • end= vs print(): digits stay on the line; print() advances after each row.
  • Complexity: O(n²) for max digit n.

One line: grow both sides toward the max digit while keeping a fixed 0 in the middle of every row.

Frequently Asked Questions

print("0", end="") sits between the ascending and descending loops, creating a fixed center on every row.
When i = max + 1, both side loops are empty — only 0 is printed.
Starting one past the max digit makes the first row have empty left and right halves, giving the single 0 row.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward max on both sides as i decreases.
Replace 9 with max_n and start i at max_n + 1 — see Example 2.
Use print(j, end=" ") and print(k, end=" ") (and print("0 ", end="")) — see Example 3.
O(n²) for max digit n because each row prints O(n) digits and there are O(n) rows.
Use try/except ValueError around int(input()) and clamp max_n to 1..9 so loop bounds stay valid.
Two rows: 0 and 101 — the smallest non-trivial mirror with a zero center.

Did you know?

This pattern prints ascending digits from i to max, a fixed 0 in the center, then descending digits from max down to i. As i decreases, each row grows into the long mirror 1234567890987654321.

Next: Spaced Mirror Number Pattern

Continue with the next pattern in the Python number-pattern series.

Program 29 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful