Reverse Descending Number Triangle in Python

What You’ll Learn
How to print a reverse descending number triangle in Python using nested loops. Each row prints a decreasing sequence like 54321, then 4321, then 321, and so on.
This pattern strengthens your understanding of counting backwards with range() and combining it with the row limit from an outer loop.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
54321
4321
321
21
1Complete Python Program
The outer loop counts down the row length. The inner loop prints numbers from that length down to 1.
rows = 5
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end="")
print()🧠 How It Works
Choose the row count
rows = 5 sets the maximum starting digit for the first row.
Outer loop (row length)
for i in range(rows, 0, -1) sets the row length: 5, then 4, then 3, down to 1.
Inner loop (count down to 1)
for j in range(i, 0, -1) prints i, i-1, ..., 1 using print(j, end="").
New line
print() ends the row and moves to the next line.
Reverse descending triangle
Total digits printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows at runtime using input():
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Add spaces between numbers with
print(j, end=" ") - Change the row direction to print
1..iinstead (Program 1 style) - Right-align the triangle by printing leading spaces before each row
- Print characters instead of numbers to create alphabet patterns
Avoid
- Forgetting
print()between rows - Using the wrong step in
range()when counting down - Mixing up row control and column control (outer loop should define the row)
- Assuming user input is always valid (wrap
int()conversion if needed)
Key Takeaways
The outer loop controls the row length by counting down from rows to 1.
The inner loop counts down from i to 1 to print each row in reverse.
Total printed digits follow the triangular number count: n(n+1)/2.
This is a classic pattern for practicing reverse ranges in Python.
❓ Frequently Asked Questions
print(j, end=" "). If you want to avoid trailing spaces, collect the row values and print " ".join(...).1..i, and i decreases each row.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
Reverse ranges like range(n, 0, -1) are a common Python pattern for counting down. Pairing them with nested loops makes many reverse triangles easy to build.
12 people found this page helpful
