Concentric Number Square in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Grid Logic

What You’ll Learn

How to print a concentric number square in Python. For max = 5, the output forms layers: the border is all 5, then inner borders of 4, 3, 2, and the center becomes 1.

This pattern is a great way to practice thinking in terms of layers inside a grid.

⭐ Pattern Output

For max = 5, the pattern looks like this:

Output
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

Note: This is the top half of the concentric square; the full pattern is symmetric and would continue by mirroring these rows back.

1

Complete Python Program

This version matches the reference output. It prints 5 rows and uses two inner loops to build each row from left to center and from center to right.

Python
max_val = 5

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

    for j in range(1, max_val + 1):
        if j > i:
            print(j, end=" ")
        else:
            print(i, end=" ")
    print()

🧠 How It Works

1

Choose the maximum value

max_val = 5 defines the outermost layer number.

Setup
2

Outer loop controls the layer

for i in range(max_val, 0, -1) prints rows that move inward from 5 down to 1.

Layer
3

Left-to-center section

The first inner loop goes from max_val down to 2 and prints the larger of j and i.

Left half
4

Center-to-right section

The second inner loop goes from 1 up to max_val and applies the same rule to complete the row symmetrically.

Right half
=

Layered square effect

Each cell prints the value of its nearest outer layer. Runtime is O(size²).

2

Variation — Full Concentric Square (User Input)

This generalized version prints the full \((2n-1) \times (2n-1)\) concentric square using a distance-from-edges formula.

Python
n = int(input("Enter max value (n >= 1): "))
if n < 1:
    raise ValueError("n must be at least 1")

size = 2 * n - 1

for i in range(size):
    for j in range(size):
        dist = min(i, j, size - 1 - i, size - 1 - j)
        val = n - dist
        print(val, end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Print without spaces for a tighter look (adjust formatting if needed)
  • Use fixed-width formatting for multi-digit values (e.g., print(f"{val:2d}", end=" "))
  • Generate a rectangular version by changing the distance calculation
  • Replace numbers with characters to create layered ASCII art
  • Mirror only horizontally or vertically for partial patterns

Avoid

  • Mixing index bases (0-based vs 1-based) in the same formula
  • Using n = 0 without validation
  • Printing variable-width numbers without formatting (columns won’t line up)
  • Hardcoding the size when you want a scalable pattern

Key Takeaways

1

The pattern is made of layers from n down to 1.

2

For max n, the full square size is 2n-1.

3

Distance-to-edge logic can generate the full pattern cleanly.

4

Printing the square grid is O(size²).

❓ Frequently Asked Questions

Each position prints the number based on how close it is to the outer border. Equal distance cells form a layer.
For a full symmetric concentric pattern, yes. But you can also print partial sections (like the top half) for practice.
Use the variation code that computes dist and prints the full size = 2*n-1 grid.
O(size²), because we print every cell once.

Explore More Python Number Patterns!

Once you understand layers, you can build framed squares, spirals, and diamond grids.

All Number Patterns →
Did you know?

For a full concentric square, the value at cell \((i, j)\) can be computed using its minimum distance to any edge. That single idea generates the entire pattern elegantly.

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