Odd-Length Descending Number Triangle in Python

What You’ll Learn
How to print an odd-length descending number triangle in Python using nested loops. The row lengths reduce by 2 each time: 1234567, 12345, 123, and 1.
This pattern is perfect for practicing custom step values in range() while keeping inner-loop printing simple.
⭐ Pattern Output
For this example, the pattern looks like this:
1234567
12345
123
1Complete Python Program
Use an outer loop that decreases by 2 and an inner loop that prints 1..i-1 on each row.
start = 8 # prints 1..7 on the first row
for i in range(start, 0, -2):
for j in range(1, i):
print(j, end="")
print()🧠 How It Works
Pick a start value
start = 8 makes the first row print digits 1 through 7.
Outer loop (step by -2)
for i in range(start, 0, -2) produces 8, 6, 4, 2, so row lengths become 7, 5, 3, 1.
Inner loop (print 1..i-1)
for j in range(1, i) prints a continuous sequence on each line.
New line
print() moves to the next row.
Odd-length triangle
Because the step is 2, the row lengths stay odd. This is still a nested-loop output, so runtime scales with the number of printed characters.
Variation — User Input Version
Let the user pick the maximum width (must be odd to match this exact style):
max_width = int(input("Enter an odd max width (e.g., 7): "))
start = max_width + 1
for i in range(start, 0, -2):
for j in range(1, i):
print(j, end="")
print()💡 Tips for Enhancement
Try These
- Validate that
max_widthis odd (or adjust the formula for even widths) - Add spaces between numbers with
print(j, end=" ") - Center the triangle by printing leading spaces before each row
- Reverse the digits to print descending sequences per row
- Swap digits for letters to create alphabet patterns
Avoid
- Forgetting
print()between rows - Using the wrong step in the outer loop (it must be
-2for odd lengths) - Off-by-one mistakes in
range(1, i)(stop is exclusive) - Assuming user input is always valid
Key Takeaways
The outer loop uses a step of -2 to keep row lengths odd.
The inner loop prints a simple sequence 1..i-1 each row.
Changing the start value changes the maximum width of the first row.
Step-based ranges are powerful for generating structured patterns.
❓ Frequently Asked Questions
i-1. Starting with 8 makes the first row print 1 through 7.start = max_width + 1 and step by -2. For this exact style, use an odd max_width.-1 (or adjust the start and ranges) so you don’t skip lengths. That will print every length, not just odd ones.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
In Python, the third argument of range() is the step. Using -2 is a simple way to skip every other row length and produce only odd-sized (or even-sized) patterns.
12 people found this page helpful
