Increasing Number Triangle (Starts from 0) in Python

What You’ll Learn
How to print an increasing number triangle in Python that starts at 0. Each row prints one more number than the previous row, and values increase sequentially across the row.
This pattern is an excellent exercise to understand how outer loops build rows while inner loops control columns and printing.
⭐ Pattern Output
For rows = 6, the pattern looks like this:
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10Complete Python Program
The inner loop starts at 0 so that the first printed value becomes 0. The formula i + j - 1 generates the consecutive values on each row.
rows = 6
for i in range(1, rows + 1):
for j in range(0, i):
print(i + j - 1, end=" ")
print()🧠 How It Works
Set the row count
rows = 6 sets the height of the triangle.
Outer loop (rows)
for i in range(1, rows + 1) runs one time per row, increasing the row length each time.
Inner loop (columns)
for j in range(0, i) prints exactly i values on row i.
Compute the value (start from 0)
i + j - 1 becomes 0 when i = 1 and j = 0, then increases by 1 as j increases.
Triangle that starts at 0
Total values printed: 1+2+…+n = n(n+1)/2, so the time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user enter 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(0, i):
print(i + j - 1, end=" ")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Start from 1 by changing the formula to
i + j - Remove the trailing space by collecting row values and joining
- Right-align the triangle using leading spaces
- Transform it into an alphabet pattern using
chr()
Avoid
- Forgetting
print()after each row - Using invalid row counts without handling errors
- Mixing row and column logic in one loop
- Assuming user input is always numeric (wrap
int()if needed)
Key Takeaways
The outer loop controls the number of rows.
The inner loop prints i values on row i.
Using j starting at 0 makes the first row begin with 0.
Total printed values follow n(n+1)/2, giving O(n²) time.
❓ Frequently Asked Questions
j starts at 0 and the formula i + j - 1 becomes 0 when i = 1.i + j - 1 to i + j (or start j from 1).Explore More Python Number Patterns!
From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.
Starting the inner loop at 0 is a common trick to shift a pattern down by one, which is why this triangle begins at 0 instead of 1.
12 people found this page helpful
