Diamond X Number Pattern in Python

What You’ll Learn
How to print a diamond-like X number pattern in Python by combining a top half and a mirrored bottom half.
This is a great exercise for understanding how to build symmetry using nested loops and diagonal checks.
⭐ Pattern Output
For n = 5, the pattern looks like this:
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1Complete Python Program
We print the top X for rows 1..n, then print the bottom X for rows n-1..1 to mirror it.
n = 5
# Top half (1..n)
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == j:
print(i, end="")
else:
print(" ", end="")
for k in range(n - 1, 0, -1):
if i == k:
print(i, end="")
else:
print(" ", end="")
print()
# Bottom half (n-1..1)
for i in range(n - 1, 0, -1):
for j in range(1, n):
if i == j:
print(i, end="")
else:
print(" ", end="")
for k in range(n, 0, -1):
if i == k:
print(i, end="")
else:
print(" ", end="")
print()🧠 How It Works
Set the size
n = 5 controls the width and height of the diamond X.
Print the top half
The first section loops i from 1 to n and prints an X for each row.
Diagonal positions
We print i only when the column is on a diagonal; otherwise we print spaces, which creates the hollow look.
Mirror for the bottom half
The second section loops i from n-1 down to 1, repeating the same diagonal logic to mirror the pattern.
Diamond X
We print spaces and numbers across two halves, so the overall runtime is O(n²).
Variation — User Input Version
Let the user choose n at runtime and print the same top-and-bottom diamond X.
n = int(input("Enter n: "))
if n < 1:
raise ValueError("n must be at least 1")
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == j:
print(i, end="")
else:
print(" ", end="")
for k in range(n - 1, 0, -1):
if i == k:
print(i, end="")
else:
print(" ", end="")
print()
for i in range(n - 1, 0, -1):
for j in range(1, n):
if i == j:
print(i, end="")
else:
print(" ", end="")
for k in range(n, 0, -1):
if i == k:
print(i, end="")
else:
print(" ", end="")
print()💡 Tips for Enhancement
Try These
- Print
*instead of numbers to create a diamond star X - Use fixed-width formatting if you print multi-digit numbers
- Combine this with a border to create a framed diamond
- Generate the same output using a single row loop (1..2n-1) as a challenge
- Store each row in a list and join it for cleaner formatting
Avoid
- Forgetting the bottom half (the diamond won’t be complete)
- Mixing 0-based and 1-based indexing in the diagonal conditions
- Printing without spaces for larger sizes (the shape becomes unclear)
- Using invalid input without validation
Key Takeaways
The pattern is built in two parts: top (1..n) and bottom (n-1..1).
Numbers are printed only on diagonal positions; everything else is a space.
Mirroring the row index creates symmetry and the diamond shape.
Runtime is O(n²) because we scan a grid of spaces and digits.
❓ Frequently Asked Questions
i from n-1 down to 1 recreates the same diagonal placements in reverse order.2n-1 and map each row to an i value that increases then decreases, then print the diagonals.print(f"{i:2}", end="")) and print spaces with the same width.Explore More Python Number Patterns!
Once you master diagonals and symmetry, try matrix diagonals and spiral traversals next.
The same diagonal checks used here are a common interview building block for matrix problems, like printing diagonals, detecting X shapes, and validating diagonal patterns.
7 people found this page helpful
