Continuous Number Triangle in Python

What You’ll Learn
How to print a continuous number triangle in Python using nested loops and a counter variable. Numbers don’t reset each row; they continue from where the previous row ended.
This is a classic exercise for understanding how to maintain state (the counter) across iterations.
⭐ Pattern Output
For rows = 4, the pattern looks like this:
1
2 3
4 5 6
7 8 9 10Complete Python Program
Increment a counter each time you print a number.
rows = 4
num = 0
for i in range(1, rows + 1):
for _ in range(i):
num += 1
print(num, end=" ")
print()🧠 How It Works
Initialize a counter
num = 0 starts the sequence.
Outer loop controls row length
i goes from 1 to rows, so each row prints one more number than the previous.
Inner loop prints and increments
Each iteration does num += 1 and prints num, so the sequence continues across rows.
New line after each row
print() moves to the next row.
Continuous triangle
Because num is not reset, the sequence stays continuous.
Variation — User Input Version
Let the user choose the number of rows at runtime:
rows = int(input("Enter the number of rows: "))
num = 0
for i in range(1, rows + 1):
for _ in range(i):
num += 1
print(num, end=" ")
print()💡 Tips for Enhancement
Try These
- Remove trailing spaces by building each row and joining it
- Start the counter from a different number (e.g., 10)
- Print the triangle in reverse by storing rows and printing later
- Format numbers with fixed width for alignment
- Try Floyd’s triangle by changing spacing/formatting
Avoid
- Resetting the counter inside the outer loop (it would break continuity)
- Forgetting
print()between rows - Assuming user input is always valid
- Printing huge triangles without considering output size
Key Takeaways
A counter variable makes numbers continue across rows.
Row i prints i numbers, so the shape is triangular.
Total printed values are n(n+1)/2 for n rows.
This is a foundational pattern for many sequence-based triangles.
❓ Frequently Asked Questions
" ".join(...).num = -1 and increment before printing, or increment after printing starting from 0.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
The total count of printed numbers in a triangle with n rows is a triangular number: \(1+2+\cdots+n = n(n+1)/2\).
12 people found this page helpful
