Python Number Diamond Pattern

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

What Is This Pattern?

A number diamond prints ascending digits in odd-length rows that grow to a peak, then shrink back — centered with leading spaces.

Remember
Rule: top 1..levels, bottom levels-1..1; each row prints 1..(2*i-1)

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1   ← levels = 5

Follows the right-aligned triangle in Program 43; next is the X pattern of stars and zeros in Program 45.

How to Solve It

Build the top half, then mirror it downward — spaces first, then digits.

MethodIdeaBest for
Two-half diamondTop 1..levels, bottom levels-1..1Learning, interviews, exams
Levels inputSame logic with a user-chosen levelsPractice / demos

Pseudocode

Pseudocode
for i from 1 to levels:
    print (levels - i) spaces
    print digits 1 to (2*i - 1)
    print newline
for i from levels-1 down to 1:
    print (levels - i) spaces
    print digits 1 to (2*i - 1)
    print newline

Cheat sheet

GoalPattern
Top halffor i in range(1, levels + 1):
Bottom halffor i in range(levels - 1, 0, -1):
Top indentfor j in range(i, levels): print(" ", end="")
Bottom indentfor j in range(levels, i, -1): print(" ", end="")
Digitsfor k in range(1, i * 2): print(k, end="")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(" ", end="") / print(k, end="")Stays on the same lineEach space or digit
print()Ends the current lineAfter spaces and digits on a row

Print spaces and digits without a newline, then end the row once.

Live Preview

Change the level count and the number diamond updates instantly.

Whole numbers from 1 to 5 (peak stays within single digits 1–9). Tap a chip or type a value — the preview redraws as you go.

Live result 5 levels · 9 lines
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Worked Walkthrough — levels = 3

Trace spaces and digits on the way up, then back down.

HalfiSpaces / digitsPrinted row
Top12 / 11
Top21 / 123123
Top30 / 1234512345
Bottom21 / 123123
Bottom12 / 11

Total lines = 2 * levels − 1. Digit count on row i = 2 * i − 1.

Python Programs

Three complete programs: fixed 5 levels, input() levels, and a compact 3-level demo. Use View Output for sample results.

Example 1 — Fixed levels = 5

Top half grows to 123456789; bottom half mirrors back to 1.

Python
levels = 5

for i in range(1, levels + 1):
    for j in range(i, levels):
        print(" ", end="")
    for k in range(1, i * 2):
        print(k, end="")
    print()

for i in range(levels - 1, 0, -1):
    for j in range(levels, i, -1):
        print(" ", end="")
    for k in range(1, i * 2):
        print(k, end="")
    print()

How It Works

1. Top half. i grows from 1 to 5; each row prints levels − i spaces, then digits 1..(2*i−1).

2. Bottom half. i shrinks from 4 to 1 so the peak row is not printed twice.

3. Newline. Bare print() after spaces and digits starts the next row.

Example 2 — User Input Levels

Read the level count with input() and build the same diamond.

Python
try:
    levels = int(input("Enter levels: "))
except ValueError:
    print("Please enter a positive whole number.")
    raise SystemExit(1)

if levels < 1:
    print("Please enter a positive whole number.")
    raise SystemExit(1)

for i in range(1, levels + 1):
    for j in range(i, levels):
        print(" ", end="")
    for k in range(1, i * 2):
        print(k, end="")
    print()

for i in range(levels - 1, 0, -1):
    for j in range(levels, i, -1):
        print(" ", end="")
    for k in range(1, i * 2):
        print(k, end="")
    print()

How It Works

1. Validate levels. Catch ValueError; require levels >= 1.

2. Same diamond. Only the bound changes from the literal 5 to the user value.

3. Safer input tip. Keep single-digit peaks for readable demos:

Safer input tip
if levels < 1 or levels > 5:
    print("Enter a whole number from 1 to 5.")
    raise SystemExit(1)

Example 3 — Compact levels = 3

Same algorithm with a smaller peak — easier to trace by hand.

Python
levels = 3

for i in range(1, levels + 1):
    for j in range(i, levels):
        print(" ", end="")
    for k in range(1, i * 2):
        print(k, end="")
    print()

for i in range(levels - 1, 0, -1):
    for j in range(levels, i, -1):
        print(" ", end="")
    for k in range(1, i * 2):
        print(k, end="")
    print()

How It Works

1. Same rules. Only levels changes — loops and digit math stay identical.

2. Quick check. Five lines total; the middle row is 12345.

Edge Cases & Pitfalls

Check these before calling the solution done.

peak twice

Bottom loop starts at levels

The widest row prints twice. Start the bottom at levels - 1.

no spaces

Forget the space loops

Rows left-align and the diamond shape disappears. Keep levels − i spaces.

k <= i

Print only to i

Rows become 1, 12, 123… instead of odd lengths. Use range(1, i * 2) (or range(1, 2 * i)).

levels > 5

Digits past 9

print(k) prints 10, 11… and columns break. Cap at 5 for single-digit rows, or format with fixed width.

levels = 1

Single cell

Output is just 1 — bottom loop never runs.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

There are 2n − 1 rows and up to O(n) digits per row, so total work is quadratic in the level count.

Key Takeaways

  • Rule: grow i to levels, then shrink; each row prints 1..(2*i−1).
  • Center: print levels − i spaces before the digits on every row.
  • end="" vs print(): spaces and digits stay on the line; bare print() advances after each row.
  • Complexity: O(n²) time; O(1) extra space.

One line: indent, print an odd-length ascending digit row, grow to the peak, then mirror down.

Frequently Asked Questions

A centered diamond of ascending digits: 1, 123, 12345, 1234567, 123456789, then the same rows mirrored back down to 1.
The row prints 2*i-1 digits (odd length): 1, 3, 5, 7, 9, … using for k in range(1, i * 2).
A space loop prints leading spaces before the digits. Top half uses for j in range(i, levels); bottom half uses for j in range(levels, i, -1).
The first builds the top half (i = 1..levels). The second mirrors back down (i = levels-1..1) to complete the diamond.
Program 43 is a right-aligned triangle with fixed-width columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
Replace 5 with a levels variable in both outer loops and space bounds — see Example 2.
O(n²) for n levels because each level prints O(n) digits and there are O(n) rows overall.
Use try/except ValueError around int(input()) and require levels >= 1 — see Example 2.
Only one row prints — a single centered 1.

Did you know?

Top half runs i = 1..levels; bottom half mirrors i = levels-1..1. Each row prints 2*i-1 digits (1 to 2*i-1) with leading spaces to center the diamond.

Next: X Pattern with Stars and Zeros

Print * on diagonals and the center column, 0 elsewhere.

Program 45 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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