Reverse Per-Row Number Triangle in Python

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

What You’ll Learn

How to print a reverse per-row number triangle in Python using nested for loops. Each row prints digits from the current row number down to 1: 1, 21, 321, 4321, and so on.

This pattern helps you practice counting backwards with range() inside a nested loop.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
21
321
4321
54321
1

Complete Python Program

The outer loop increases the row count. The inner loop counts down from i to 1 and prints each digit.

Python
rows = 5

for i in range(1, rows + 1):
    for j in range(i, 0, -1):
        print(j, end="")
    print()

🧠 How It Works

1

Choose the row count

rows = 5 sets the height of the triangle.

Setup
2

Outer loop (row number)

for i in range(1, rows + 1) moves from row 1 up to row rows.

Row control
3

Inner loop (count down i..1)

for j in range(i, 0, -1) prints digits from the current row number down to 1 using print(j, end="").

Number printing
4

New line

print() ends each row.

Line break
=

Reverse per-row triangle

Total digits printed 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 decide the number of rows at runtime using input():

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

for i in range(1, rows + 1):
    for j in range(i, 0, -1):
        print(j, end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing the pattern
  • Add spaces between numbers with print(j, end=" ")
  • Print an ascending row version by changing the inner loop to range(1, i + 1)
  • Right-align the triangle by printing leading spaces before each row
  • Replace numbers with letters to create alphabet triangles

Avoid

  • Forgetting print() between rows
  • Using the wrong step value 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

1

The outer loop controls how many rows to print (from 1 to rows).

2

The inner loop counts down from i to 1, which reverses each row.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

Reverse range() loops make it easy to print descending sequences.

❓ Frequently Asked Questions

Because the inner loop uses range(i, 0, -1), which counts down from i to 1.
Yes. Use print(j, end=" "). If you want consistent alignment, print with a fixed width.
Change the inner loop to count up: for j in range(1, i + 1). That prints each row in ascending order.
O(n²) for n rows: total prints are 1+2+…+n = 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?

Reverse ranges like range(n, 0, -1) are a common Python trick for counting down. Combining them with nested loops makes many reverse triangles easy to generate.

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