Shifted Odd Number Triangle in Python

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:
13579
3579
579
79
9Complete Python Program
The outer loop chooses the starting odd number. The inner loop prints the remaining odds up to 9.
for i in range(1, 10, 2):
for j in range(i, 10, 2):
print(j, end="")
print()🧠 How It Works
Pick the odd range
We print odd numbers from 1 to 9 using a step of 2.
Outer loop chooses row start
for i in range(1, 10, 2) produces 1, 3, 5, 7, 9 as row starts.
Inner loop prints i..9 (odd)
for j in range(i, 10, 2) prints odd numbers starting at i and ending at 9.
New line per row
print() moves to the next line after each row.
Shifted odd triangle
Each row starts later, so the triangle shifts and shrinks naturally.
Variation — User Input Version
Let the user choose the maximum odd number (must be odd):
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
Using range(..., step=2) iterates only through odd numbers.
The outer loop sets the starting odd number for each row.
The inner loop prints the remaining odds up to the maximum.
Each row gets shorter naturally, forming a shifted triangle.
❓ Frequently Asked Questions
range(..., 10, 2), and 10 is exclusive, so the last printed odd is 9.range(2, max_even + 1, 2) for both loops.print(j, end=" ") inside the inner loop. For neat output, avoid trailing spaces by building the row first.Explore More Python Number Patterns!
Keep going to discover more number pattern programs and strengthen your loop skills.
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.
12 people found this page helpful
