Continuous Number Triangle in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Counters

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:

Output
1
2 3
4 5 6
7 8 9 10
1

Complete Python Program

Increment a counter each time you print a number.

Python
rows = 4
num = 0

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

🧠 How It Works

1

Initialize a counter

num = 0 starts the sequence.

Setup
2

Outer loop controls row length

i goes from 1 to rows, so each row prints one more number than the previous.

Row control
3

Inner loop prints and increments

Each iteration does num += 1 and prints num, so the sequence continues across rows.

Counter
4

New line after each row

print() moves to the next row.

Line break
=

Continuous triangle

Because num is not reset, the sequence stays continuous.

2

Variation — User Input Version

Let the user choose the number of rows at runtime:

Python
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

1

A counter variable makes numbers continue across rows.

2

Row i prints i numbers, so the shape is triangular.

3

Total printed values are n(n+1)/2 for n rows.

4

This is a foundational pattern for many sequence-based triangles.

❓ Frequently Asked Questions

It keeps the current number to print and increments on each output, so numbers don’t restart on a new line.
Store row values in a list and print them with " ".join(...).
Yes. Initialize num = -1 and increment before printing, or increment after printing starting from 0.
O(n²) for n rows because the output size is \(n(n+1)/2\).

Explore More Python Number Patterns!

Keep going to discover more number pattern programs and strengthen your loop skills.

All Number Patterns →
Did you know?

The total count of printed numbers in a triangle with n rows is a triangular number: \(1+2+\cdots+n = n(n+1)/2\).

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