Odd-Length Descending Number Triangle in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Step Ranges

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:

Output
1234567
12345
123
1
1

Complete Python Program

Use an outer loop that decreases by 2 and an inner loop that prints 1..i-1 on each row.

Python
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

1

Pick a start value

start = 8 makes the first row print digits 1 through 7.

Setup
2

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.

Row control
3

Inner loop (print 1..i-1)

for j in range(1, i) prints a continuous sequence on each line.

Number printing
4

New line

print() moves to the next row.

Line break
=

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.

2

Variation — User Input Version

Let the user pick the maximum width (must be odd to match this exact style):

Python
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_width is 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 -2 for odd lengths)
  • Off-by-one mistakes in range(1, i) (stop is exclusive)
  • Assuming user input is always valid

Key Takeaways

1

The outer loop uses a step of -2 to keep row lengths odd.

2

The inner loop prints a simple sequence 1..i-1 each row.

3

Changing the start value changes the maximum width of the first row.

4

Step-based ranges are powerful for generating structured patterns.

❓ Frequently Asked Questions

Because the inner loop prints up to i-1. Starting with 8 makes the first row print 1 through 7.
Yes. Use start = max_width + 1 and step by -2. For this exact style, use an odd max_width.
Change the step to -1 (or adjust the start and ranges) so you don’t skip lengths. That will print every length, not just odd ones.
O(n²) in terms of output size, because the total number of printed characters grows with the number of printed digits.

Explore More Python Number Patterns!

Keep going to discover more number pattern programs and strengthen your loop skills.

All Number Patterns →
Did you know?

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.

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