Shifted Odd Number Triangle in Python

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

What You’ll Learn

How to print a shifted odd number triangle in Python using nested loops with a step of 2. Each row prints odd numbers starting from a later value: 13579, 3579, 579, 79, 9.

This pattern is a great way to practice custom step ranges and building rows from a moving start value.

⭐ Pattern Output

For odd numbers up to 9, the pattern looks like this:

Output
13579
3579
579
79
9
1

Complete Python Program

The outer loop chooses the starting odd number. The inner loop prints the remaining odds up to 9.

Python
for i in range(1, 10, 2):
    for j in range(i, 10, 2):
        print(j, end="")
    print()

🧠 How It Works

1

Pick the odd range

We print odd numbers from 1 to 9 using a step of 2.

Setup
2

Outer loop chooses row start

for i in range(1, 10, 2) produces 1, 3, 5, 7, 9 as row starts.

Row control
3

Inner loop prints i..9 (odd)

for j in range(i, 10, 2) prints odd numbers starting at i and ending at 9.

Number printing
4

New line per row

print() moves to the next line after each row.

Line break
=

Shifted odd triangle

Each row starts later, so the triangle shifts and shrinks naturally.

2

Variation — User Input Version

Let the user choose the maximum odd number (must be odd):

Python
max_odd = int(input("Enter the max odd number (e.g., 9): "))

for i in range(1, max_odd + 1, 2):
    for j in range(i, max_odd + 1, 2):
        print(j, end="")
    print()

💡 Tips for Enhancement

Try These

  • Add spaces between numbers with print(j, end=" ")
  • Print even numbers instead by starting from 2 and stepping by 2
  • Right-align the triangle by printing leading spaces
  • Reverse each row by printing in descending order
  • Turn it into a function that returns the lines as a list

Avoid

  • Forgetting the step value (without 2, you won’t get only odd numbers)
  • Using an even max_odd in the variation without adjusting the logic
  • Forgetting print() between rows
  • Assuming user input is always valid (validate oddness if needed)

Key Takeaways

1

Using range(..., step=2) iterates only through odd numbers.

2

The outer loop sets the starting odd number for each row.

3

The inner loop prints the remaining odds up to the maximum.

4

Each row gets shorter naturally, forming a shifted triangle.

❓ Frequently Asked Questions

Because the loops use range(..., 10, 2), and 10 is exclusive, so the last printed odd is 9.
Start from 2 and step by 2: range(2, max_even + 1, 2) for both loops.
Yes. Use print(j, end=" ") inside the inner loop. For neat output, avoid trailing spaces by building the row first.
O(n²) in terms of output size (triangular count of printed values).

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, step-based ranges are a clean way to filter sequences without an if. For example, range(1, 10, 2) produces only odd numbers.

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