Ascending Number Triangle in Python

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

What You’ll Learn

How to print an ascending number triangle in Python using nested for loops. Each row starts at 1 and prints up to the current row number: 1, 12, 123, … up to 1..rows.

This pattern is an excellent exercise to understand how the outer loop controls rows and the inner loop controls columns.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
12
123
1234
12345
1

Complete Python Program

The outer loop sets the row length. The inner loop prints from 1 up to the current row number.

Python
rows = 5

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

🧠 How It Works

1

Choose the row count

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

Setup
2

Outer loop (row number)

for i in range(1, rows + 1) moves from row 1 up to row rows.

Row control
3

Inner loop (print 1..i)

for j in range(1, i + 1) prints digits from 1 up to the current row limit i using print(j, end="").

Number printing
4

New line

print() moves to the next row after each line.

Line break
=

Ascending number triangle

Total numbers printed are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows at runtime 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(j, end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing the pattern
  • Add spaces between numbers with print(j, end=" ")
  • Right-align the triangle by printing leading spaces before each row
  • Print a descending version by looping i from rows down to 1
  • Replace numbers with characters to create alphabet patterns

Avoid

  • Forgetting print() between rows
  • Mixing up row/column logic (keep the outer loop for rows)
  • Using the wrong range() endpoints (remember the stop value is exclusive)
  • Assuming user input is always valid (wrap int() conversion if needed)

Key Takeaways

1

The outer loop increases the row length from 1 to rows.

2

The inner loop prints numbers from 1 to the current row index i.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

This pattern is a foundation for many other triangles and pyramids in programming.

❓ Frequently Asked Questions

Because the outer loop increases i by 1 each row, and the inner loop prints from 1 to i.
Yes. Use print(j, end=" "). For cleaner formatting, you can also build a list and join it.
Loop the outer variable downwards using for i in range(rows, 0, -1) and keep printing 1..i each row (like Program 1).
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More Python Number Patterns!

Practice nested loops with progressively richer patterns—from triangles to pyramids and beyond.

All Number Patterns →
Did you know?

The number of printed digits in this triangle forms a triangular number: 1+2+…+n = n(n+1)/2. That’s why many pattern-printing programs naturally have O(n²) output size.

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