Left-Aligned Descending Number Triangle in Python

What You’ll Learn
How to print a left-aligned descending number triangle in Python. Every row starts from the same number (rows) and counts down, but each next row stops earlier: 54321, 5432, 543, 54, 5.
This is a good pattern to practice nested loops where the inner loop always begins from the same value.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
54321
5432
543
54
5Complete Python Program
The outer loop controls where each row stops. The inner loop always counts down from rows.
rows = 5
for i in range(0, rows):
for j in range(rows, i, -1):
print(j, end="")
print()🧠 How It Works
Choose the maximum number
rows = 5 sets the starting number for every row.
Outer loop (controls row shortening)
for i in range(0, rows) controls how many values are removed from the right end as rows progress. As i grows, the inner loop stops sooner.
Inner loop (count down from rows)
for j in range(rows, i, -1) prints rows, rows-1, ..., i+1 using print(j, end="").
New line
print() ends the row and moves to the next line.
Left-aligned 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 maximum number at runtime using input():
rows = int(input("Enter the number of rows: "))
for i in range(0, rows):
for j in range(rows, i, -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=" ") - For a right-aligned look in the console, pad shorter rows with leading spaces before the digits
- Print the same shape using strings (build each row and print once)
- Swap the direction to create
12345,1234... (Program 1)
Avoid
- Forgetting
print()between rows - Using the wrong stop value in
range()(remember it’s exclusive) - Mixing up row control and number printing logic
- Assuming user input is always valid (wrap
int()conversion if needed)
Key Takeaways
The inner loop always starts from rows, which keeps the same first digit every row.
The outer loop changes the inner loop’s stop condition, making each row shorter.
Total printed digits follow the triangular number count: n(n+1)/2.
This pattern is a practical way to learn range(start, stop, step) with a negative step.
❓ Frequently Asked Questions
rows. Only the number of printed digits changes each row.54321, then starts the next row from 4 (4321). Program 4 always starts from 5 and shortens by changing the stopping point (5432, 543, ...).print(j, end=" "). For neat formatting, build a list of numbers and join them with spaces.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
Changing only the stop condition of a loop (while keeping the start fixed) is a neat way to generate shrinking patterns like this one.
12 people found this page helpful
