Ascending Number Triangle in Python

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:
1
12
123
1234
12345Complete Python Program
The outer loop sets the row length. The inner loop prints from 1 up to the current row number.
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()🧠 How It Works
Choose the row count
rows = 5 sets how many lines the triangle will have.
Outer loop (row number)
for i in range(1, rows + 1) moves from row 1 up to row rows.
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="").
New line
print() moves to the next row after each line.
Ascending number triangle
Total numbers printed are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows at runtime using input():
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
ifromrowsdown 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
The outer loop increases the row length from 1 to rows.
The inner loop prints numbers from 1 to the current row index i.
Total printed digits follow the triangular number count: n(n+1)/2.
This pattern is a foundation for many other triangles and pyramids in programming.
❓ Frequently Asked Questions
i by 1 each row, and the inner loop prints from 1 to i.print(j, end=" "). For cleaner formatting, you can also build a list and join it.for i in range(rows, 0, -1) and keep printing 1..i each row (like Program 1).Explore More Python Number Patterns!
Practice nested loops with progressively richer patterns—from triangles to pyramids and beyond.
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.
12 people found this page helpful
