Square Numbers Pyramid in Python

What You’ll Learn
How to print a centered pyramid of square numbers in Python. The program prints squares of consecutive integers (1, 4, 9, 16, ...), and increases the count per row as 1, 3, 5, 7, ....
This is a great exercise to understand how nested loops can manage both spacing (alignment) and values (what to print).
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625Complete Python Program
This version prints a centered pyramid and fills it with squares of consecutive numbers. We use m to count 1, 2, 3... and print m*m.
rows = 5
m = 0
for r in range(1, rows + 1):
# leading spaces to center the pyramid
print(" " * (4 * (rows - r)), end="")
# print 1, 3, 5, ... values per row
for _ in range(2 * r - 1):
m += 1
print(f"{m*m:4d}", end="")
print()🧠 How It Works
Set the number of rows
rows = 5 decides the pyramid height.
Row loop (top to bottom)
for r in range(1, rows + 1) prints one row at a time.
Print leading spaces (centering)
" " * (4 * (rows - r)) adds indentation so the pyramid stays centered.
Print odd count of squares
2 * r - 1 produces 1, 3, 5, ... values per row. We increment m and print m*m with a fixed width for neat columns.
Square numbers pyramid
Total values printed are 1+3+5+…+(2n-1) = n², so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user choose how many rows to print. This keeps the same square-filling logic but makes it interactive.
rows = int(input("Enter number of rows: "))
m = 0
for r in range(1, rows + 1):
print(" " * (4 * (rows - r)), end="")
for _ in range(2 * r - 1):
m += 1
print(f"{m*m:4d}", end="")
print()💡 Tips for Enhancement
Try These
- Change the formatting width to align bigger squares (e.g., use
{m*m:6d}) - Print cubes (
m**3) instead of squares (m**2) - Reset
m = 0each row to print repeated square sequences per row - Replace squares with triangular numbers to create a different numeric pyramid
- Add a separator by printing with spaces like
print(f"{m*m:4d} ", end="")
Avoid
- Printing without fixed width formatting (columns will drift as numbers get wider)
- Forgetting to print a newline after each row
- Using negative or zero rows without validating user input
- Mixing alignment logic into the value loop (keep spacing and printing separate)
Key Takeaways
Each row prints an odd count of values: 2r-1.
m tracks consecutive integers, and the printed value is m*m.
Leading spaces keep the pyramid center-aligned.
Total printed values are n² for n rows, so runtime is O(n²).
❓ Frequently Asked Questions
2r-1.m for each printed slot and output m*m. This produces 1, 4, 9, 16, 25, ... in order.rows. Increasing it prints more rows and more squared values.1+3+5+...+(2n-1)=n^2.Explore More Python Number Patterns!
From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.
The sum of the first n odd numbers is n². That’s why a pyramid with 1, 3, 5, ... values per row prints exactly n² square numbers in total.
9 people found this page helpful
