Shape Rule
Parity + shrinking width
Row 1 prints 11111, row 2 prints 0000, shrinking until a single 1 — odd rows are 1s, even rows are 0s.

The alternating 1 and 0 triangle prints 11111, 0000, 111, 00, 1 — a natural step after the rotating number pattern in Program 39. This tutorial covers modulo parity, shrinking inner-loop width, nested loops, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Parity + shrinking width
Row 1 prints 11111, row 2 prints 0000, shrinking until a single 1 — odd rows are 1s, even rows are 0s.
1..rows
for i in range(1, rows + 1): walks each row and supplies the parity value via i % 2.
i..rows ascending
for _ in range(i, rows + 1): repeats the row digit rows - i + 1 times.
i % 2
print(i % 2, end="") prints 1 on odd rows and 0 on even rows.
3–9 rows
Pick a row count and draw the alternating 1/0 triangle in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
An alternating 1 and 0 triangle prints odd rows filled with 1s and even rows filled with 0s, while each row gets shorter. With rows = 5, the output is 11111, 0000, 111, 00, 1.
In Python the outer loop runs i = 1..rows, the inner loop repeats i % 2 via range(i, rows + 1), then print() moves to the next line.
It teaches modulo parity and shrinking inner-loop bounds — a key step after Program 39’s rotating rows.
i % 2 picks 1 or 0 for the whole row.
Inner loop starts at i and runs to rows.
Program 39 rotates digits; Program 40 alternates binary digits by row parity.
Follow Program 39; continue to Program 41 (square numbers pyramid) next.
In short: for each i from 1 to rows, print i % 2 repeatedly for range(i, rows + 1), then print().
Given a positive integer rows (e.g. 5), print an alternating 1/0 triangle: odd rows are all 1s, even rows are all 0s, with each row one character shorter.
# rows = 5 (conceptual shape)
# 11111
# 0000
# 111
# 00
# 1 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines and width of the first row. |
i | int | Outer loop — row index from 1 to rows; also supplies parity via i % 2. |
_ | — | Inner loop — repeats the row digit rows - i + 1 times via range(i, rows + 1). |
for i from 1 to rows:
digit = i % 2
for _ from i to rows:
print digit
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops + modulo | 11111, 0000, … | Learning and interviews |
| User-input rows | int(input(...)) | Flexible console programs |
| Spaced output | print(i % 2, end=" ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for i in range(1, rows + 1): |
| Print parity digit | print(i % 2, end="") |
| Repeat per row | for _ in range(i, rows + 1): |
| End the row | print() |
| Spaced digits | print(i % 2, end=" ") |
| Flip 1s and 0s | print(1 - (i % 2), end="") |
| Program 39 contrast | Rotating digits i..rows then wrap — not binary parity |
Same alternating 1/0 triangle — different ways to control rows and formatting.
i = 1..rowsSupplies parity via i % 2
_ = i..rowsShrinking row width each line
i = 1Longest row of 1s on top
i % 2Odd → 1, even → 0
Reach for this pattern when teaching modulo parity and shrinking inner-loop bounds.
Natural follow-up — alternating binary digits by row parity instead of rotating numbers.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Flip parity with 1 - (i % 2) or try per-column alternation with (i + j) % 2.
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 3 and 9 and draw the alternating 1/0 triangle in the browser.
Three complete Python programs — fixed rows, user input, and spaced output variant. Click View Output to reveal sample console results.
Print five rows of the alternating 1/0 triangle with nested loops and modulo.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
rows = 5
for i in range(1, rows + 1):
for _ in range(i, rows + 1):
print(i % 2, end="")
print() When i = 1 (odd), the inner loop prints 1 five times — output 11111. When i = 2 (even), it prints 0 four times — output 0000. The outer loop increases i each row, shortening the inner loop.
Read the row count with input() instead of hard-coding 5.
Read rows with input() and int() (wrap in try/except ValueError in real apps).
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for _ in range(i, rows + 1):
print(i % 2, end="")
print() Same modulo inner-loop core as Example 1; only the source of rows changes from a literal to user input. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Add a space between digits for easier reading on each row.
Keep rows = 5 but print each character followed by a space.
rows = 5
for i in range(1, rows + 1):
for _ in range(i, rows + 1):
print(i % 2, end=" ")
print() Only the print statement changes — print(i % 2, end=" ") instead of print(i % 2, end=""). Loop bounds and parity check stay the same as Example 1.
No imports needed for fixed rows; use input() when reading. Set rows = 5 and loop variable i.
for i in range(1, rows + 1): — ascending outer loop supplies parity via i % 2.
for _ in range(i, rows + 1): — repeats the row digit rows - i + 1 times.
print(i % 2, end="") prints 1 for odd rows and 0 for even rows.
print() ends the row after the inner loop finishes.
Rows shrink from rows characters to one — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i, the inner-loop range, character count, parity, and full row output.
i | Inner loop | Char | Prints | Row output |
|---|---|---|---|---|
1 | 1, 2, 3, 4, 5 | 1 | 5 | 11111 |
2 | 2, 3, 4, 5 | 0 | 4 | 0000 |
3 | 3, 4, 5 | 1 | 3 | 111 |
4 | 4, 5 | 0 | 2 | 00 |
5 | 5 | 1 | 1 | 1 |
Prints per row = rows - i + 1 — total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip parity with 1 - (i % 2) to start rows with zeros.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 41 for a square numbers pyramid.
Practice print vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use print(i % 2, end=" ") between digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).
Pair the pattern with input() return checks 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: trace i and i % 2 on paper for rows = 3 before coding — watch how parity and row width interact.
Small habits that keep number-pattern code clean.
Outer loop uses range(1, rows + 1); inner loop uses range(i, rows + 1) — the stop value is not included.
try/except ValueErrorUse try/except ValueError so bad input does not crash when converting rows.
Only call print() after the inner loop finishes the row.
i % 2 is 1 on odd rows and 0 on even rows — flip with 1 - (i % 2).
Trace i = 1, 2, 3 on paper before coding the full rows = 5 demo.
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 1/0 triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(i % 2, end="") for digits; print() only after the inner loop.
range(1, rows + 1) on every row prints a full rectangle — the start must change with i.
→ Keep for _ in range(i, rows + 1): so each row shortens correctly.
for i in range(0, rows): shifts parity — row 1 becomes all 0s instead of 1s.
→ Use for i in range(1, rows + 1): so row 1 starts with 1s.
Omitting print() glues every digit onto one endless line.
→ Always end the row after the inner loop.
int(input())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.
Two rows: 11 and 0.
Bare int(input()) raises ValueError on bad input — use try/except first.
Each row prints rows - i + 1 characters — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
0 using 1 - (i % 2)(i + j) % 2i = 1..rows. Inner loop: range(i, rows + 1). Digit: i % 2.print(i % 2, 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 i = 1..rows, inner loop range(i, rows + 1) with print(i % 2, end=""), then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The alternating 1 and 0 triangle is a compact nested-loop lesson: modulo parity picks the row digit while a shrinking inner loop shortens each line. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 41 for the square numbers pyramid.
Odd rows print 1, even rows print 0 — keep print(i % 2, end="") for digits and print() for the break.
for i in range(1, rows + 1): in the outer loopfor _ in range(i, rows + 1): repeats the row digitprint(i % 2, end="") for digits and print() after each rowrows ≥ 1 for interactive programsint(input()) in try/except ValueErrorprint() inside the inner digit looprange(0, rows) for the outer loop (shifts parity)range() stop is exclusiverows = 1 edge casePrint the pattern the beginner-friendly way.
Odd rows = 1s, even = 0s
Definitionrange(1, rows + 1)
Coderange(i, rows + 1)
Codei % 2 picks digit
LogicO(n²) time
AnalysisOdd rows print 1, even rows print 0 — chosen with i % 2. Row i prints rows - i + 1 characters; total prints = n(n+1)/2.
Move on to the square numbers pyramid in the Python number-pattern series.
12 people found this page helpful