Decreasing Length Number Triangle in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a decreasing-length number triangle in Python where each new row contains one fewer number, while values continue sequentially from 1 up to 15.

This pattern is useful for learning how the inner loop range controls how many values are printed per row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
 1  2  3  4  5
 6  7  8  9
10 11 12
13 14
15
1

Complete Python Program

Use a counter k that increments for each printed value. The inner loop prints fewer items as the row index increases.

Python
rows = 5
k = 0

for i in range(1, rows + 1):
    for _ in range(i, rows + 1):
        k += 1
        print(f"{k:2d}", end=" ")
    print()

🧠 How It Works

1

Initialize the counter

k = 0 tracks the next number to print.

Setup
2

Outer loop (rows)

for i in range(1, rows + 1) iterates row by row from 1 to rows.

Row control
3

Inner loop (decreasing length)

for _ in range(i, rows + 1) prints fewer values as i grows: 5 values on row 1, then 4, 3, 2, and 1.

Width control
4

Increment and print

Each inner-loop iteration increments k and prints it using f"{k:2d}" to keep spacing tidy.

Number printing
=

Decreasing-length triangle

Total printed values are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the number of rows at runtime:

Python
rows = int(input("Enter the number of rows: "))
k = 0

for i in range(1, rows + 1):
    for _ in range(i, rows + 1):
        k += 1
        print(f"{k:2d}", end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Start from a different number by initializing k to start - 1
  • Use fixed-width printing ({k:3d}) if values get larger
  • Print the same numbers in an increasing-length triangle by swapping the inner range
  • Center-align the triangle by adding leading spaces per row

Avoid

  • Forgetting print() after each row
  • Changing the counter inside the outer loop (it must keep running)
  • Using inconsistent spacing (alignment will look jagged)
  • Assuming user input is always valid (wrap int() if needed)

Key Takeaways

1

The outer loop decides the row number.

2

The inner loop prints fewer values on each next row.

3

A running counter k keeps the numbers continuous across rows.

4

Total printed values are n(n+1)/2, giving O(n²) time.

❓ Frequently Asked Questions

It is 15, because the total printed count is 5(5+1)/2 = 15.
It keeps spacing consistent when numbers become two digits (10, 11, …).
Yes. Use an inner loop like for _ in range(1, i + 1) so rows grow from 1 up to rows.
O(n²), because the total amount printed is \(1+2+\dots+n\).

Explore More Python Number Patterns!

From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.

All Number Patterns →
Did you know?

The number of values printed is a triangular number. With 5 rows, that’s 15—which is also the last value printed.

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