Diamond Number Pyramid in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a diamond number pyramid in Python. The top half grows using odd-length sequences (1, 123, 12345...) up to the widest row, and the bottom half mirrors the pattern back down.

This pattern is a great exercise for mastering two-phase loops: one loop for the top half and another for the bottom half.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete Python Program

This approach prints the top half (increasing odd-length rows) and then prints the bottom half (decreasing odd-length rows), using spaces to keep the pyramid centered.

Python
rows = 5

# top half (1, 123, 12345, ...)
for i in range(1, rows + 1):
    print(" " * (rows - i), end="")
    for k in range(1, 2 * i):
        print(k, end="")
    print()

# bottom half (..., 12345, 123, 1)
for i in range(rows - 1, 0, -1):
    print(" " * (rows - i), end="")
    for k in range(1, 2 * i):
        print(k, end="")
    print()

🧠 How It Works

1

Set the row count

rows = 5 determines the diamond size.

Setup
2

Top half (increasing)

For row i, we print rows-i spaces and then print numbers from 1 to 2*i-1.

Build up
3

Bottom half (decreasing)

We repeat the same idea for i = rows-1 down to 1 to mirror the diamond.

Mirror
4

Odd-length rows keep symmetry

Printing 2*i-1 numbers makes every row symmetric around the center.

Shape
=

Diamond number pyramid

Total printed digits grow roughly with the area of the diamond, so runtime is O(n²) for n rows.

2

Variation — User Input Version

Let the user pick the diamond size. For cleaner alignment with larger rows, we also validate rows > 0.

Python
rows = int(input("Enter the number of rows: "))
if rows < 1:
    raise ValueError("rows must be at least 1")

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

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

💡 Tips for Enhancement

Try These

  • Print numbers with spaces (e.g., 1 2 3) and adjust centering
  • Print descending numbers on the right half to make a full palindromic row (e.g., 12321)
  • Use fixed-width formatting for multi-digit rows (helps when rows > 5)
  • Swap digits for letters to create an alphabet diamond
  • Turn this into a hollow diamond by printing only borders

Avoid

  • Forgetting to print the bottom half (you’ll only get a pyramid, not a diamond)
  • Using inconsistent spacing (alignment will look off)
  • Printing without a newline after each row
  • Assuming user input is always valid (handle rows < 1)

Key Takeaways

1

The diamond is made from two pyramids: one increasing and one decreasing.

2

Each row prints an odd number of digits: 2*i-1.

3

Leading spaces keep rows centered: rows-i.

4

Runtime grows about quadratically with rows, so it’s O(n²).

❓ Frequently Asked Questions

It’s a diamond-like pattern because we print a top half pyramid and then mirror it with a bottom half.
Odd-length rows (2*i-1) make the pattern symmetric around the center when centered with spaces.
Print with end=" " and adjust the leading spaces (each printed number now takes more width).
O(n²) for n rows because the amount of printed output grows with the area of the diamond.

Explore More Python Number Patterns!

Once you master diamonds, try palindromic pyramids and Floyd’s triangle patterns.

All Number Patterns →
Did you know?

The widest row of this diamond has \(2n-1\) digits for n rows. That odd width is what keeps the diamond symmetric.

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.

8 people found this page helpful