Right-Aligned Descending Number Triangle in Python

What You’ll Learn
How to print a right-aligned descending number triangle in Python where each row begins with 5 and counts down: 5, 5 4, 5 4 3, and so on.
You’ll learn how to combine leading spaces with a descending number loop to form a neat triangle.
⭐ Pattern Output
For max_num = 5, the pattern looks like this:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1Complete Python Program
Print spaces first to right-align the output, then print descending values from max_num down to the row limit.
max_num = 5
for row in range(1, max_num + 1):
# leading spaces (two spaces per missing number)
for _ in range(max_num - row):
print(" ", end=" ")
# print: max_num, max_num-1, ..., max_num-row+1
for val in range(max_num, max_num - row, -1):
print(val, end=" ")
print()🧠 How It Works
Choose the maximum number
max_num = 5 decides both the height and the starting value for each row.
Outer loop (rows)
for row in range(1, max_num + 1) prints 1 number on the first row, 2 on the second, and so on.
Print leading spaces
The loop max_num - row prints indentation so numbers shift to the right on earlier rows.
Print descending values
range(max_num, max_num - row, -1) prints 5, then 5 4, then 5 4 3, and so on.
Right-aligned descending triangle
Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user choose the maximum number (and rows) at runtime:
max_num = int(input("Enter the maximum number: "))
for row in range(1, max_num + 1):
for _ in range(max_num - row):
print(" ", end=" ")
for val in range(max_num, max_num - row, -1):
print(val, end=" ")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
max_num < 1) before printing - Left-align the pattern by removing the space loop
- Use fixed-width formatting like
print(f"{val:2d}", end=" ")for cleaner alignment - Replace numbers with characters to build alphabet triangles
- Invert the pattern by printing from 1 up to the row limit instead
Avoid
- Forgetting
print()after each row - Using inconsistent spacing (your triangle may look skewed)
- Mixing row and column logic (keep loops single-purpose)
- Assuming user input is always valid (wrap
int()if needed)
Key Takeaways
Print leading spaces to make a right-aligned triangle.
Use a descending loop to print max_num down to the row limit.
The number of items per row increases by 1 each line (1, 2, 3, …).
Total prints are n(n+1)/2, giving O(n²) time.
❓ Frequently Asked Questions
max_num to your new starting value (like 7). The loop will print 7 on the first row, then 7 6, and so on.Explore More Python Number Patterns!
From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.
Many right-aligned patterns are made simply by printing the right number of leading spaces. Once you can control indentation, you can build pyramids, diamonds, and more.
12 people found this page helpful
