Diamond X Number Pattern in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two Sections

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:

Output
1       1
 2     2
  3   3
   4 4
    5
   4 4
  3   3
 2     2
1       1
1

Complete Python Program

We print the top X for rows 1..n, then print the bottom X for rows n-1..1 to mirror it.

Python
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

1

Set the size

n = 5 controls the width and height of the diamond X.

Setup
2

Print the top half

The first section loops i from 1 to n and prints an X for each row.

Top
3

Diagonal positions

We print i only when the column is on a diagonal; otherwise we print spaces, which creates the hollow look.

Diagonals
4

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.

Bottom
=

Diamond X

We print spaces and numbers across two halves, so the overall runtime is O(n²).

2

Variation — User Input Version

Let the user choose n at runtime and print the same top-and-bottom diamond X.

Python
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

1

The pattern is built in two parts: top (1..n) and bottom (n-1..1).

2

Numbers are printed only on diagonal positions; everything else is a space.

3

Mirroring the row index creates symmetry and the diamond shape.

4

Runtime is O(n²) because we scan a grid of spaces and digits.

❓ Frequently Asked Questions

The bottom half is the mirror of the top. Printing i from n-1 down to 1 recreates the same diagonal placements in reverse order.
Yes. Loop rows from 1 to 2n-1 and map each row to an i value that increases then decreases, then print the diagonals.
Use fixed-width formatting (for example print(f"{i:2}", end="")) and print spaces with the same width.
O(n²), because the program prints across a grid of spaces and digits for both halves.

Explore More Python Number Patterns!

Once you master diagonals and symmetry, try matrix diagonals and spiral traversals next.

All Number Patterns →
Did you know?

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.

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.

7 people found this page helpful