Diamond Number Pyramid in Python

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:
1
123
12345
1234567
123456789
1234567
12345
123
1Complete 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.
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
Set the row count
rows = 5 determines the diamond size.
Top half (increasing)
For row i, we print rows-i spaces and then print numbers from 1 to 2*i-1.
Bottom half (decreasing)
We repeat the same idea for i = rows-1 down to 1 to mirror the diamond.
Odd-length rows keep symmetry
Printing 2*i-1 numbers makes every row symmetric around the center.
Diamond number pyramid
Total printed digits grow roughly with the area of the diamond, so runtime is O(n²) for n rows.
Variation — User Input Version
Let the user pick the diamond size. For cleaner alignment with larger rows, we also validate rows > 0.
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
The diamond is made from two pyramids: one increasing and one decreasing.
Each row prints an odd number of digits: 2*i-1.
Leading spaces keep rows centered: rows-i.
Runtime grows about quadratically with rows, so it’s O(n²).
❓ Frequently Asked Questions
2*i-1) make the pattern symmetric around the center when centered with spaces.end=" " and adjust the leading spaces (each printed number now takes more width).Explore More Python Number Patterns!
Once you master diamonds, try palindromic pyramids and Floyd’s triangle patterns.
The widest row of this diamond has \(2n-1\) digits for n rows. That odd width is what keeps the diamond symmetric.
8 people found this page helpful
