Square Numbers Pyramid in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
                  1
              4   9  16
         25  36  49  64  81
    100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
1

Complete 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.

Python
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

1

Set the number of rows

rows = 5 decides the pyramid height.

Setup
2

Row loop (top to bottom)

for r in range(1, rows + 1) prints one row at a time.

Row control
3

Print leading spaces (centering)

" " * (4 * (rows - r)) adds indentation so the pyramid stays centered.

Alignment
4

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.

Value printing
=

Square numbers pyramid

Total values printed are 1+3+5+…+(2n-1) = n², so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose how many rows to print. This keeps the same square-filling logic but makes it interactive.

Python
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 = 0 each 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

1

Each row prints an odd count of values: 2r-1.

2

m tracks consecutive integers, and the printed value is m*m.

3

Leading spaces keep the pyramid center-aligned.

4

Total printed values are for n rows, so runtime is O(n²).

❓ Frequently Asked Questions

A centered pyramid grows by 2 values per row, so the row length follows the odd numbers: 2r-1.
We increment m for each printed slot and output m*m. This produces 1, 4, 9, 16, 25, ... in order.
Change rows. Increasing it prints more rows and more squared values.
O(n²) for n rows, because the total printed values are 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.

All Number Patterns →
Did you know?

The sum of the first n odd numbers is . That’s why a pyramid with 1, 3, 5, ... values per row prints exactly square numbers in total.

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.

9 people found this page helpful