Concentric Number Square in Python

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:
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 5Note: This is the top half of the concentric square; the full pattern is symmetric and would continue by mirroring these rows back.
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.
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
Choose the maximum value
max_val = 5 defines the outermost layer number.
Outer loop controls the layer
for i in range(max_val, 0, -1) prints rows that move inward from 5 down to 1.
Left-to-center section
The first inner loop goes from max_val down to 2 and prints the larger of j and i.
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.
Layered square effect
Each cell prints the value of its nearest outer layer. Runtime is O(size²).
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.
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 = 0without validation - Printing variable-width numbers without formatting (columns won’t line up)
- Hardcoding the size when you want a scalable pattern
Key Takeaways
The pattern is made of layers from n down to 1.
For max n, the full square size is 2n-1.
Distance-to-edge logic can generate the full pattern cleanly.
Printing the square grid is O(size²).
❓ Frequently Asked Questions
dist and prints the full size = 2*n-1 grid.Explore More Python Number Patterns!
Once you understand layers, you can build framed squares, spirals, and diamond grids.
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.
8 people found this page helpful
