Shape Rule
i % 2 picks parity
Row 1 prints 1, row 2 prints 2 4, row 3 prints 1 3 5, and so on as width grows.

The alternating odd/even number triangle switches row parity to print odd or even sequences — a natural step after left-shifted odd patterns. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
i % 2 picks parity
Row 1 prints 1, row 2 prints 2 4, row 3 prints 1 3 5, and so on as width grows.
1..rows
for i in range(1, rows + 1) makes each new row one number longer than the previous.
k += 2 sequence
for j in range(1, i + 1) prints k, then k += 2 keeps odd or even parity on each row.
Same line / next line
Numbers use print(k, end=" "); end each row with print().
1–20 rows
Pick a row count and draw the alternating odd/even triangle instantly in the browser.
Complexity
Total prints = rows(rows+1)/2; extra memory stays O(1).
An alternating odd/even number triangle grows each row by one number while switching between odd and even sequences using row parity. With rows = 5, the output is 1, 2 4, 1 3 5, 2 4 6 8, 1 3 5 7 9.
In Python you pick start value k with i % 2, print k in the inner loop, update k += 2, then print() ends each row.
It combines parity checks with growing row width — a step up from Program 17.
i % 2 picks odd start 1 or even start 2.
Stays odd-only or even-only within each row.
print(k, end=" ") in the inner loop; print() after.
Follow Program 17; continue to Program 19 (fill-with-5 triangle).
In short: for each row i from 1 to rows, set k from i % 2, print k then k += 2 for i numbers, then call print().
Given a positive integer rows, print an alternating odd/even triangle: odd rows print odd numbers starting at 1, even rows print even numbers starting at 2, each row has i numbers with k += 2.
# rows = 5 (conceptual shape)
# 1
# 2 4
# 1 3 5
# 2 4 6 8
# 1 3 5 7 9
for i in range(1, rows + 1):
if i % 2 == 0:
k = 2
else:
k = 1
for j in range(1, i + 1):
print(k, end=" ")
k += 2
print() | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Each row has i spaced numbers — odd or even by row parity. |
for i from 1 to rows:
if i is even: k = 2 else k = 1
for j from 1 to i:
print k + space
k += 2
print newline | Approach | Idea | Best for |
|---|---|---|
| Parity + k += 2 | 1, 2 4, 1 3 5, … | Learning and interviews |
| Conditional start | k = 2 if i % 2 == 0 else 1 | Compact user-input version |
| Flip parity | Swap odd/even row assignment | Even rows odd, odd rows even |
| Goal | Pattern |
|---|---|
| Walk each row | for i in range(1, rows + 1) |
| Pick start by parity | if i % 2 == 0: k = 2 else: k = 1 |
| Print and step | print(k, end=" "); k += 2 |
| End the row | print() |
| Conditional shortcut | k = 2 if i % 2 == 0 else 1 |
| Flip parity rows | k = 1 if i % 2 == 0 else 2 |
Same alternating triangle — different ways to set the row start value k.
parityOdd row → k=1, even row → k=2
sequenceKeeps odd or even within the row
compactOne-line start pick in Example 2
reset kSet k fresh each outer-loop iteration
Reach for this pattern when teaching row parity and the k += 2 sequence inside nested loops.
Natural follow-up after Program 17 — combines parity with growing row width.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Compare Program 17 (left-shifted odds) and Program 19 (fill-with-5 triangle) next.
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.
Choose a row count between 1 and 20 and draw the alternating odd/even triangle in the browser.
Three complete Python programs — fixed row count, compact conditional expression user input, and flipped parity variant. Click View Output to reveal sample console results.
Print five rows of the alternating odd/even triangle with i % 2 and k += 2.
rows = 5Hard-coded height — ideal for first demos and screenshots.
rows = 5
for i in range(1, rows + 1):
if i % 2 == 0:
k = 2
else:
k = 1
for j in range(1, i + 1):
print(k, end=" ")
k += 2
print() When i = 1 (odd), k starts at 1 and prints once. When i = 2 (even), k starts at 2 and prints 2 then 4. When i = 3, k runs 1, 3, 5 as 1 3 5, and so on as row width grows. print() after the inner loop starts the next row.
Read the row count at runtime with input().
Read rows with input() and int() (wrap in try/except ValueError in real apps); use a compact conditional expression for k.
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
k = 2 if i % 2 == 0 else 1
for j in range(1, i + 1):
print(k, end=" ")
k += 2
print() Same nested-loop core as Example 1; only the source of rows changes. The conditional expression 2 if i % 2 == 0 else 1 replaces the if/else block. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Swap the assignment so even rows print odds and odd rows print evens.
Even rows start at 1 (odds); odd rows start at 2 (evens).
rows = 5
for i in range(1, rows + 1):
if i % 2 == 0:
k = 1
else:
k = 2
for j in range(1, i + 1):
print(k, end=" ")
k += 2
print() Swap the if/else branches so even rows get k = 1 and odd rows get k = 2. The inner loop and k += 2 logic stay the same — only parity assignment changes.
print is built in; use input() when reading input. Set rows (fixed or from input).
for i in range(1, rows + 1) makes each row print i numbers.
Set k from i % 2, then print(k, end=" ") and k += 2 for i iterations.
print() ends the row so the next outer iteration starts fresh.
Total prints: rows(rows+1)/2 — O(n²) time, O(1) extra memory.
rows = 4Trace each outer-loop value of i, the starting k, and the numbers printed on each row.
i | Parity | Numbers printed | Row output |
|---|---|---|---|
1 | odd | 1 | 1 |
2 | even | 2, 4 | 2 4 |
3 | odd | 1, 3, 5 | 1 3 5 |
4 | even | 2, 4, 6, 8 | 2 4 6 8 |
Total number prints: 1 + 2 + 3 + 4 = 10 = 4×5/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice print(..., end=" ") vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: use print(k, end=" ") for spaced numbers on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with try/except ValueError around int(input()) and positive-row checks.
Example: reject rows <= 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.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn i % 2 for row parity first; compare with flipped assignment in Example 3.
Small habits that keep number-pattern code clean.
Use rows (or n) and reset k at the start of each outer-loop iteration.
try/except ValueErrorWrap int(input()) in try/except ValueError so bad input does not leave rows unset.
Only call print() after the inner loop finishes the row.
Write row i, start k, and each k += 2 step before coding.
Trace rows = 5 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.
Mistakes that commonly break alternating odd/even number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(k, end=" ") for numbers; print() only after the inner loop.
Reusing k from the previous row mixes odd and even sequences.
→ Set k from i % 2 at the start of each outer-loop iteration.
k += 1 mixes odd and even numbers within the same row.
→ After each print, update with k += 2 to keep parity.
Omitting print() glues every number onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input raise ValueError with bare int(input()).
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
int(input()) raises ValueError — validate with try/except first.
k += 1 mixes odd and even — use k += 2 within each row.
Set k fresh each row from i % 2 — do not carry over from the previous row.
Try these variations to lock in the pattern.
+= 2nrows(rows+1)/2 — O(n²) for n rows.print(k, end=" ") stays on the line; print() advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.Quick Takeaway: outer loop grows row width, set k from i % 2, print k then k += 2, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Flip parity (Example 3) | O(rows²) | O(1) |
The alternating odd/even number triangle is a compact lesson in row parity: i % 2 picks the start value, and k += 2 keeps each row odd-only or even-only. Master the if/else version, then try the conditional-expression and flip-parity variants.
Practice the three examples above, then continue to Program 19 for the fill-with-5 number triangle.
Reset k each row from i % 2 — use k += 2 inside the inner loop and validate rows when reading input.
i % 2 row parity before codingprint(k, end=" ") and reset k each rowrows ≥ 1 for interactive programsint(input()) in try/except ValueError before using rowsprint() inside the inner digit loopk += 1 instead of k += 2 within a rowk at the start of each rowrows = 1 edge casePrint the pattern the beginner-friendly way.
i % 2 picks parity
Definition1 for odd rows, 2 for even
CodeStays odd or even
CodeRow i prints i nums
ShapeO(n²) time
AnalysisRow parity picks the start value: odd rows begin at 1, even rows at 2. Then k += 2 keeps each row odd-only or even-only — still O(n²) total prints for n rows.
Move on to the fill-with-5 number triangle in the Python number-pattern series.
12 people found this page helpful