Python Odd Number Triangle Pattern (Left-Shifted)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A left-shifted odd number triangle prints only odd values from the row start up to max_n, stepping by 2 — so you see 13579, 3579, 579, 79, 9.

Remember
Rule: for i = 1..max step 2, print j = i..max step 2

13579
3579
579
79
9   ← max = 10

In Python both loops use range(..., 2) from an odd start: print(j, end="") prints each odd digit, then bare print() ends the row. Program 18 alternates odd and even rows next.

How to Solve It

Outer loop from 1 to max_n stepping by 2. Inner loop from current i to max_n stepping by 2. End each row with print().

MethodIdeaBest for
Odd + step 2Both loops use range(..., 2) from an odd startLearning, interviews, exams
Even mirrorStart both loops at 2Comparing odd-only vs even-only

Pseudocode

Pseudocode
for i from 1 to max step 2:
    for j from i to max step 2:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Outer (odd starts)for i in range(1, max_n + 1, 2):
Inner (odds to max)for j in range(i, max_n + 1, 2): print(j, end="")
End the rowprint()
Force odd maxif max_n % 2 == 0: max_n -= 1
Even mirrorfor i in range(2, max_n + 1, 2):

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="")Stays on the same lineEach odd digit
print()Ends the current lineAfter the inner loop

Print all digits without a newline, then end the row once.

Live Preview

Change the maximum and the left-shifted odd triangle updates instantly.

Whole numbers from 1 to 20. Odd steps never print even values — tap a chip or type a value.

Live result max = 10 · 15 digits
13579
3579
579
79
9

Worked Walkthrough

Trace three outer values when max_n = 10 — watch the start shift while only odds print.

iInner jLengthPrinted row
11, 3, 5, 7, 9513579
33, 5, 7, 943579
55, 7, 93579
9919

10 never prints because the loops stay on odd values. Raising i by 2 drops one odd digit from the left each row.

Python Programs

Three complete programs: fixed max, input() with odd adjust, and an even-number mirror. Use View Output for sample results.

Example 1 — Fixed max_n = 10

Hard-coded maximum — both loops step by 2 from an odd start.

Python
max_n = 10

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

How It Works

1. Outer picks the start. i runs 1, 3, 5, 7, 9 — that value is where each row begins.

2. Inner prints odds to max. j steps from i to max_n by 2 with print(j, end="").

3. End the row. Call bare print() only after the inner loop finishes.

Example 2 — User Input

Read the maximum from the keyboard. Subtract 1 when the value is even so the bound is odd.

Python
max_n = int(input("Enter the maximum value: "))

if max_n % 2 == 0:
    max_n -= 1

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

How It Works

1. Adjust even input. 8 becomes 7 so the last odd matches a clear odd bound.

2. Same loop core. The nested step-2 loops match Example 1.

3. Safer input tip. Validate before adjusting:

Safer input
raw = input("Enter the maximum value: ").strip()
try:
    max_n = int(raw)
except ValueError:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if max_n < 1:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if max_n % 2 == 0:
    max_n -= 1

Example 3 — Even Number Mirror

Start both loops at 2 to print only even digits with the same left-shift shape.

Python
max_n = 10

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

How It Works

1. Same left-shift structure. Only the start value changes from 1 to 2.

2. Even values only. i and j visit 2, 4, 6, 8, 10.

3. Compare side by side. Seeing both odd and even mirrors makes the step-2 idea easy to remember.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong step

Use step 2, not step 1

range(i, max_n + 1) reprints consecutive integers, not an odd-only triangle.

even max

Optional: force an odd bound

if max_n % 2 == 0: max_n -= 1 keeps the last odd clear when reading input.

print() inside

Bare print(j) inside the inner loop

That puts each digit on its own line. Use print(j, end="") for digits; call bare print() only after the row finishes.

max = 1

Single digit

Output is just 1 — a good sanity check.

max ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

max_n > 9

print(11, end="") prints two characters. Labs often stick to max ≤ 10 for a clean single-digit look.

Time and Space Complexity

ProgramTimeExtra space
Odd triangle (Examples 1–2)O(n²)O(1)
Even mirror (Example 3)O(n²)O(1)

About half the integers up to n are visited, but nested loops still make total work O(n²).

Key Takeaways

  • Rule: for each odd start i, print odds from i to max_n with step 2.
  • Shift: each row starts at a larger odd value, so the left edge moves right.
  • end= vs print(): digits stay on the line; bare print() advances after each row.
  • Next step: Program 18 alternates odd and even number rows.

One line: start at an odd i, print odds through max_n with step 2, then raise the start.

Frequently Asked Questions

Both loops use range with step 2 starting from an odd value, so they visit only odd values: 1, 3, 5, 7, 9.
Each next row starts from a larger odd number (i increases by 2), so fewer digits print on that row.
On the last iteration, i = 9, so the inner loop prints only 9 once.
print(j, end="") stays on the same line. print() ends the current line. Digits use end=""; the row break uses print() after the inner loop.
Subtract 1 (if max_n % 2 == 0: max_n -= 1) so the last printed odd matches the intended width, as shown in Example 2.
Yes. Start both loops at 2 instead of 1 — see Example 3 for the even mirror.
O(n²) for maximum value n. Only about half the numbers are visited due to step size 2, but nested loops still dominate.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require max_n >= 1.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Both loops step by 2 with range(..., 2), so only odd numbers print. Each row starts at a larger odd value, so the triangle shifts left — still O(n²) for maximum n.

Next: Alternating Odd/Even Triangle

Continue with rows that flip between odd and even number sequences.

Program 18 tutorial →

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