Increasing Number Triangle in Python

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:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9Complete Python Program
On each row i, the inner loop prints i numbers. The value printed is computed using i + j - 1.
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
Choose the number of rows
rows = 5 sets how many lines the triangle will have.
Outer loop (row control)
for i in range(1, rows + 1) runs from 1 to rows to build each row.
Inner loop (print i numbers)
for j in range(1, i + 1) prints one value per column, producing i values on row i.
Compute the value per position
i + j - 1 starts at i when j = 1, then increases by 1 as j increases.
Increasing number triangle
Total prints: 1+2+…+n = n(n+1)/2, so the time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows 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(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
The outer loop controls the triangle height (rows).
The inner loop prints i values on row i.
The expression i + j - 1 makes each row start at i and increase by 1.
Total printed values follow n(n+1)/2, giving O(n²) time.
❓ Frequently Asked Questions
i = 3, the formula i + j - 1 prints 3, 4, 5 as j goes from 1 to 3.i + j - 2 instead of i + j - 1.Explore More Python Number Patterns!
From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.
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.
12 people found this page helpful
