Increasing Odd-Length Number Rows in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Step Size Loop

What You’ll Learn

The increasing odd-length number rows pattern uses range(..., 2) in the outer loop so each row prints 1..i with lengths 1, 3, 5, 7, 9 — a natural step after the jump triangle in Program 21. This tutorial covers the shape rule, step-size logic, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.

Shape Rule

Odd row lengths

Row 1 prints 1, row 2 prints 123, row 3 prints 12345, and so on — digits run together with no spaces.

Outer Loop

i += 2

for i in range(1, max_n + 1, 2) walks odd values 1, 3, 5, 7, 9 as row lengths.

Inner Loop

1..i

for j in range(1, i + 1) then print(j, end="") — no space between digits.

print end= vs print()

Same line / next line

Digits use print(j, end=""); end each row with print().

Live Preview

1–15 max

Pick an odd maximum and draw the odd-length rows pattern instantly in the browser.

O(n²)

Complexity

Total prints = 1+3+5+...+max_n; extra memory stays O(1).

Introduction

An increasing odd-length number rows pattern prints consecutive digits 1..i on each row, with row lengths growing by 2 each time. With max_n = 9, the output is 1, 123, 12345, 1234567, 123456789.

In Python you use for i in range(1, max_n + 1, 2) in the outer loop and print(j, end="") in the inner loop, then print() ends each row.

Why it matters?

It introduces loop step sizes — a simple change to range(..., 2) creates a whole new family of patterns.

Key Highlights

Outer i += 2

Row lengths are 1, 3, 5, 7, 9 — always odd.

Inner 1..i

print(j, end="") concatenates digits on one line.

No spaces

Digits run together — 123 not 1 2 3.

Series Foundation

Follow Program 21; continue to Program 23 (number & asterisk mirror) next.

In short: for each odd i up to max_n, print digits 1 through i with print(j, end=""), then call print().

📝 Problem & Approach

Given a positive odd integer max_n, print increasing odd-length rows: for each odd i from 1 to max_n, print digits 1 through i concatenated on one line.

Python
# max_n = 9 (conceptual shape)
# 1
# 123
# 12345
# 1234567
# 123456789

Inputs & Outputs

ItemTypeDescription
max_nintMaximum row length (typically odd, e.g. 9).
iintOuter loop — odd values 1, 3, 5, … up to max_n.
jintInner loop — prints digits 1 through i.
Printed outputtextRow i has i concatenated digits — no spaces.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Outer i += 21, 123, 12345, …Learning and interviews
User-input max_nmax_n = int(input(...))Flexible console programs
Inner j += 21, 13, 135, 1357, …Odd-only digit rows

⚡ Quick Reference

GoalPattern
Walk odd lengthsfor i in range(1, max_n + 1, 2)
Print digitsfor j in range(1, i + 1)
Write digitprint(j, end="")
End the rowprint()
Odd-only variantfor j in range(1, i + 1, 2)
User inputmax_n = int(input(...))

📋 Fixed Max vs User Input vs Odd-Only Digits

Same odd-length rows — different ways to control max_n and inner loop step.

Outer step
range step 2

Row lengths 1, 3, 5, 7, 9

Inner 1..i
print(j, end="")

Concatenate digits — no spaces

Odd digits
j step 2

Print 1, 3, 5 only in Example 3

Learning tip
even max_n

Subtract 1 if user enters an even maximum

Context

When This Pattern Shows Up

Reach for this pattern when teaching loop step sizes and concatenated digit output inside nested loops.

  1. Post-jump exercise

    Natural follow-up after Program 21 — introduces outer loop step range(..., 2).

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with input() for a flexible row count.

  4. Gateway to variants

    Compare Program 21 (jump triangle) and Program 23 (number & asterisk mirror) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose an odd maximum between 1 and 15 and draw the odd-length rows pattern in the browser.

Try 7, 9, or 11. Even values are adjusted down by 1. Max up to 15.

Live result
Press "Draw pattern".

Examples Gallery

Three complete Python programs — fixed maximum, user input, and odd-only digits variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of odd-length consecutive digits with outer range(..., 2).

Example 1 — Fixed max_n = 9

Hard-coded maximum — ideal for first demos and screenshots.

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

How It Works

When i = 1, the inner loop prints 1 once. When i = 3, j runs 1, 2, 3 — output 123. When i = 9, digits 1 through 9 concatenate into 123456789. print() after the inner loop starts the next row.

📈 User Input

Read the maximum with input() instead of hard-coding 9.

Example 2 — User Input

Read max_n with int(input()); adjust to odd if the user enters an even value.

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(1, i + 1):
        print(j, end="")
    print()

How It Works

Same nested-loop core as Example 1; only the source of max_n changes. The if max_n % 2 == 0: max_n -= 1 guard keeps the last row odd-length. Non-numeric input raises ValueError from int(input()) — wrap it in try/except in safer labs.

⚡ Odd-Only Digits

Use step 2 in the inner range to print only odd digits.

Example 3 — Odd-Only Digits range(..., 2)

Keep max_n = 9 but print 1, 3, 5, 7, 9 instead of 1..i on each row.

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

How It Works

Change only the inner loop to range(1, i + 1, 2) — the outer loop and print() logic stay the same. Each row prints odd digits up to i instead of every digit from 1 to i.

🧠 How the Algorithm Prints Rows

1

Set up

print is built in; use input() when reading input. Set max_n and loop variables i, j.

Setup
2

Outer loop + range step 2

for i in range(1, max_n + 1, 2) — row lengths are 1, 3, 5, 7, 9.

Row
3

Inner loop + print(j, end="")

for j in range(1, i + 1) then print(j, end="") — digits concatenate with no spaces.

Digits
4

New line

print() ends the row so the next outer iteration starts fresh.

Break
=

Odd-length rows complete

Total prints grow as 1+3+5+...+max_nO(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — max_n = 9

Trace each outer-loop value of i and the digits printed on each row.

iInner j rangeDigits printedRow output
1111
31..31, 2, 3123
51..51, 2, 3, 4, 512345
71..71..71234567
91..91..9123456789

Total digit prints: 1 + 3 + 5 + 7 + 9 = 25.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change j <= i and watch the shape change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: use (i + j) % 2 for row+column parity grids.

3. Console Formatting Drills

Practice print(..., end="") vs row newline without complex math.

Example: put print() inside the inner loop by mistake.

4. Character Substitution

Swap digits for letters, stars, or spaced output once the loop works.

Example: print j + " " for spaced digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for n = 10 still → 55.

6. Input Validation Labs

Pair the pattern with try/except ValueError and positive-max checks.

Example: reject max_n <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner Python courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: learn range(..., 2) in the outer loop first; compare with range(..., 2) odd-digit variant in Example 3.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Bounds Clearly

    Use print(j, end="") without spaces — not print(j, end=" ") unless you want gaps.

  2. 2. Validate input()

    Wrap int(input()) in try/except ValueError so bad input does not crash the script.

  3. 3. Keep print() Outside

    Only call print() after the inner loop finishes the row.

  4. 4. Trace i and j on Paper

    Write each odd i and the j range before coding.

  5. 5. Dry-Run One Small n

    Trace max_n = 7 on paper before coding larger demos.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.

Common Pitfalls

Mistakes that commonly break odd-length row patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use print(j, end=""); print() only after the inner loop.

  2. 2. Forgetting range step 2

    Using step 1 (no step-2 range) prints every length 1, 2, 3, 4 — not odd lengths only.

    → Use for i in range(1, max_n + 1, 2) for odd row lengths.

  3. 3. Adding Spaces Between Digits

    print(j, end=" ") produces 1 2 3 instead of 123.

    → Use print(j, end="") for concatenated digits.

  4. 4. Forgetting the Row Break

    Omitting print() glues every number onto one endless line.

    → Always end the row after the inner loop.

  5. 5. Unchecked input

    Letters or empty input raise ValueError from int(input()).

    → Wrap int(input()) in try/except ValueError and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

max_n = 1

Single digit

Output is just 1 on one line.

max_n = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

max_n < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Large width

Output grows as n² characters — fine for labs, noisy for huge n.

Bad input

Non-numeric input

int(input()) raises ValueError — validate with try/except first.

Even max

User enters even

Subtract 1 to keep odd row lengths — see Example 2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Jump number triangle

2. Number & asterisk mirror

3. Spaced digits

  • Use print(j, end=" ") for gaps
  • Compare with concatenated output

4. No trailing space

  • Print space only between numbers, not after the last
  • Harder follow-up after this page

Notes

  • Odd sum. Total prints = 1+3+5+...+max_n — O(n²) for maximum row length n.
  • print(j, end="") stays on the line; print() advances — mix them carefully.
  • Validate max_n > 0 for interactive programs; max_n = 1 should print a single 1.
  • This page is left-aligned. Centered pyramids need leading spaces — covered later in the series.

Quick Takeaway: use range(..., 2) in the outer loop, print(j, end="") in the inner loop, then print() after each row.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(max²)O(1)
Odd digits (Example 3)O(max²)O(1)
Wrap Up

🎉 Conclusion

The increasing odd-length number rows pattern is a compact lesson in loop step sizes: use range(..., 2) in the outer loop and print(j, end="") in the inner loop to concatenate digits. Master the fixed-max version, then try user input and the odd-only digit variant.

Practice the three examples above, then continue to Program 23 for the number & asterisk mirror pattern.

Use range(..., 2) for odd lengths — validate max_n and adjust even input when reading from the console.

💡 Best Practices

✅ Do

  • Use for i in range(1, max_n + 1, 2) in the outer loop
  • Use print(j, end="") — no space between digits
  • Adjust even max_n with max_n -= 1 for user input
  • Wrap int(input()) in try/except ValueError before using max_n
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call print() inside the inner digit loop
  • Use step 1 when you want odd lengths only
  • Add spaces unless you want separated digits
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the max_n = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this odd-length pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Inner 1..i

print(j, end="") no space

Code
+ 03

Odd lengths

1, 3, 5, 7, 9

Shape
04

j += 2

Odd digits variant

Variant
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Because the outer loop uses range with step 2, so i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
print(j, end="") prints each digit immediately after the previous one on the same line — no space character is added.
Yes. Change the inner loop to range(1, i + 1, 2) and print j to output 1, 13, 135, 1357, 13579 (see Example 3).
print(j, end="") stays on the same line with no trailing space. print() ends the current line. Digits use end=""; the row break uses print() after the inner loop.
Because the outer loop runs while i <= 9 (range(1, 10, 2)). Change 9 to any odd maximum to extend the pattern.
Subtract 1 to make it odd (see Example 2) so the last row still has an odd length.
O(n²) for maximum row length n because total prints are 1+3+5+...+n.
Use a try/except ValueError around int(input()) or check the raw string with .isdigit() before converting so bad input does not crash the script.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you Know? 🔊

Each row length increases by 2 because the outer loop uses range(..., 2) (1, 3, 5, 7, 9). The inner loop prints 1..i with end="" — total prints grow as O(n²) for maximum row length n.

Continue to Program 23

Move on to the number & asterisk mirror pattern in the Python number-pattern series.

Program 23 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