Python Concentric Number Diamond Pattern

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

What Is This Pattern?

A concentric number diamond is Program 46’s square mirrored top-to-bottom: layers from k down to 1, then back out to k.

Remember
Rule: top i=k..1, bottom i=2..k; cell = (j if j > i else i)

5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5   ← k = 5

Follows the top-half concentric square in Program 46; next is the powers of 11 pattern in Program 48.

How to Solve It

Reuse the square’s row builder twice — once going in, once going out.

MethodIdeaBest for
Two-half diamondTop k..1, bottom 2..k, same cell ruleLearning, interviews, exams
Input kSame logic with a user-chosen peakPractice / demos

Pseudocode

Pseudocode
for i from k down to 1:
    print left half j = k..1 and right half j = 2..k
    (value = j if j > i else i)
for i from 2 to k:
    print the same row rule again
    print newline after each row

Cheat sheet

GoalPattern
Top halffor i in range(k, 0, -1):
Bottom halffor i in range(2, k + 1):
Left / rightrange(k, 0, -1) / range(2, k + 1)
Cell valueprint(j if j > i else i, end=" ")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(..., end=" ")Stays on the same lineEach number plus a trailing space
print()Ends the current lineAfter both half-loops finish a row

Print numbers without a newline, then end the row once.

Live Preview

Change k and the concentric diamond updates instantly.

Whole numbers from 1 to 7. Size = (2k − 1) × (2k − 1). Tap a chip or type a value — the preview redraws as you go.

Live result k = 5 · 9 × 9
5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5 

Worked Walkthrough — k = 3

Top half walks in; bottom half walks out (skip repeating the center).

HalfiPrinted row
Top33 3 3 3 3
Top23 2 2 2 3
Top13 2 1 2 3
Bottom23 2 2 2 3
Bottom33 3 3 3 3

Total lines = 2*k − 1. Cells for k = 5 = 9 × 9 = 81.

Python Programs

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

Example 1 — Fixed k = 5

Top half + bottom half with the same j > i cell rule.

Python
k = 5

for i in range(k, 0, -1):
    for j in range(k, 0, -1):
        print(j if j > i else i, end=" ")
    for j in range(2, k + 1):
        print(j if j > i else i, end=" ")
    print()

for i in range(2, k + 1):
    for j in range(k, 0, -1):
        print(j if j > i else i, end=" ")
    for j in range(2, k + 1):
        print(j if j > i else i, end=" ")
    print()

How It Works

1. Top half. i runs from 5 down to 1 — same concentric rows as Program 46.

2. Bottom half. i runs from 2 up to 5 so the center row is not printed twice.

3. Cell rule. Both halves use left k..1, right 2..k, and j if j > i else i.

Example 2 — User Input (k)

Read k and build the full diamond with the same row logic.

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

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

for i in range(k, 0, -1):
    for j in range(k, 0, -1):
        print(j if j > i else i, end=" ")
    for j in range(2, k + 1):
        print(j if j > i else i, end=" ")
    print()

for i in range(2, k + 1):
    for j in range(k, 0, -1):
        print(j if j > i else i, end=" ")
    for j in range(2, k + 1):
        print(j if j > i else i, end=" ")
    print()

How It Works

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

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

3. Safer input tip. Cap demos for readable output:

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

Example 3 — Compact k = 3

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

Python
k = 3

for i in range(k, 0, -1):
    for j in range(k, 0, -1):
        print(j if j > i else i, end=" ")
    for j in range(2, k + 1):
        print(j if j > i else i, end=" ")
    print()

for i in range(2, k + 1):
    for j in range(k, 0, -1):
        print(j if j > i else i, end=" ")
    for j in range(2, k + 1):
        print(j if j > i else i, end=" ")
    print()

How It Works

1. Same rules. Only k changes — both outer loops and the cell rule stay identical.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

no bottom

Forgot the bottom half

You get Program 46’s square only. Add for i in range(2, k + 1):.

i = 1..k

Bottom starts at i = 1

The center row prints twice. Start the bottom at i = 2.

k = 1

Single cell

Output is just 1 — the bottom loop never runs.

print() inside

print() inside a half-loop

That breaks the row. Call bare print() only after both inner loops.

j = 1..k

Right half starts at j = 1

The center digit doubles on every row. Start the mirror at j = 2.

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(k²)O(1)
User input (Example 2)O(k²)O(1)

The grid is about (2k − 1)² cells, so total work is quadratic in k.

Key Takeaways

  • Rule: top k..1, bottom 2..k; each cell is j if j > i else i.
  • vs 46: Program 46 is the top half only; this page adds the outward mirror.
  • end=" " vs print(): numbers stay on the line; bare print() advances after both halves.
  • Complexity: O(k²) time; O(1) extra space.

One line: print concentric rows inward to 1, then mirror them outward to finish the diamond.

Frequently Asked Questions

A full concentric number diamond: outer layer k, values decrease to 1 at the center, then increase back to k — a (2k-1)×(2k-1) grid.
The first prints the top half (i = k down to 1). The second prints the bottom half (i = 2 up to k) to mirror the shape.
Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
When column j is still outside the current row layer i, print j. Otherwise print i (the current layer value).
Each row has 2*k - 1 numbers. With k = 5, width and height are both 9.
Change k or read it from input — both outer loops and row width adjust automatically — see Example 2.
O(k²) because the grid has about (2k-1)² cells and each is printed once.
The center prints 1 — the deepest layer of the concentric pattern.
Use try/except ValueError around int(input()) and require k >= 1 — see Example 2.

Did you know?

Top half: for i in range(k, 0, -1):. Bottom half: for i in range(2, k + 1):. Each cell: j if j > i else i. Grid size = 2k - 1 rows and columns.

Next: Powers of 11 Pattern

Print binomial-looking rows like 1, 11, 121, 1331 using powers of 11.

Program 48 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