Increasing Number Triangle in Python

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

What You’ll Learn

How to print an increasing number triangle in Python where row 1 prints 1, row 2 prints 2 3, row 3 prints 3 4 5, and so on.

This is a simple nested-loop pattern that strengthens your understanding of row control (outer loop) and column printing (inner loop).

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
1

Complete Python Program

On each row i, the inner loop prints i numbers. The value printed is computed using i + j - 1.

Python
rows = 5

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(i + j - 1, end=" ")
    print()

🧠 How It Works

1

Choose the number of rows

rows = 5 sets how many lines the triangle will have.

Setup
2

Outer loop (row control)

for i in range(1, rows + 1) runs from 1 to rows to build each row.

Row control
3

Inner loop (print i numbers)

for j in range(1, i + 1) prints one value per column, producing i values on row i.

Number printing
4

Compute the value per position

i + j - 1 starts at i when j = 1, then increases by 1 as j increases.

Formula
=

Increasing number triangle

Total prints: 1+2+…+n = n(n+1)/2, so the time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows using input():

Python
rows = int(input("Enter the number of rows: "))

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(i + j - 1, end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Start from 0 by using i + j - 2
  • Remove the trailing space by collecting numbers in a list and joining
  • Right-align the triangle using leading spaces
  • Convert it into an alphabet pattern by printing chr() values

Avoid

  • Forgetting print() after each row
  • Using invalid row counts without handling errors
  • Mixing row logic into the inner loop (keep responsibilities clear)
  • Assuming user input is always numeric (wrap int() if needed)

Key Takeaways

1

The outer loop controls the triangle height (rows).

2

The inner loop prints i values on row i.

3

The expression i + j - 1 makes each row start at i and increase by 1.

4

Total printed values follow n(n+1)/2, giving O(n²) time.

❓ Frequently Asked Questions

Because when i = 3, the formula i + j - 1 prints 3, 4, 5 as j goes from 1 to 3.
It prints 1+2+…+n = n(n+1)/2 numbers in total.
Use i + j - 2 instead of i + j - 1.
O(n²) because the amount of output grows like 1+2+…+n.

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?

If you print n rows, you always print n(n+1)/2 values in total—that’s why most triangle patterns have a quadratic runtime as n grows.

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.

12 people found this page helpful