Python Binary Rows Pattern (Alternating 1 and 0)

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

What Is This Pattern?

An alternating 1 and 0 pattern prints a shrinking triangle where odd rows are all 1s and even rows are all 0s, chosen with i % 2.

Remember
Rule: odd i → 1, even i → 0; length = rows - i + 1

11111
0000
111
00
1   ← 5 rows

Follows the rotating number pattern in Program 39; next is the square numbers pyramid in Program 41.

How to Solve It

One nested-loop idea driven by row parity — pick the digit once per row, then repeat it while the length shrinks.

MethodIdeaBest for
If/else + moduloEven i → 0; odd i → 1Learning, interviews, exams
print(i % 2)Same parity check in one expressionShorter demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        if i is even: print 0 else print 1
    print newline

Cheat sheet

GoalPattern
Walk each rowfor i in range(1, rows + 1):
Shrink the rowfor j in range(i, rows + 1):
Pick 1 or 0print("0" if i % 2 == 0 else "1", end="")
Short formprint(i % 2, end="")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(..., end="")Stays on the same lineEach 1 or 0
print()Ends the current lineAfter the inner loop

Print characters without a newline, then end the row once.

Live Preview

Change the row count and the alternating binary triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 chars
11111
0000
111
00
1

Worked Walkthrough — rows = 4

Trace parity and length. Odd rows use 1; even rows use 0.

iParity / countPrinted row
1odd → 1 × 41111
2even → 0 × 3000
3odd → 1 × 211
4even → 0 × 10

Row length is rows - i + 1. The character depends only on i, not on j.

Python Programs

Three complete programs: fixed height with if/else, input() with i % 2, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

If/else on i % 2; each row repeats one digit while the length shrinks.

Python
rows = 5

for i in range(1, rows + 1):
    for j in range(i, rows + 1):
        if i % 2 == 0:
            print("0", end="")
        else:
            print("1", end="")
    print()

How It Works

1. Outer loop. i grows from 1 to 5 — odd rows start with 1, even rows with 0.

2. Parity check. i % 2 == 0 means even → print 0; otherwise print 1.

3. Shrink. Inner range(i, rows + 1) prints rows - i + 1 characters, then bare print().

Example 2 — User Input Rows

Read rows with input(), validate, then use the short i % 2 form.

Python
try:
    rows = int(input("Enter rows: "))
except ValueError:
    print("Please enter a positive whole number.")
    raise SystemExit(1)

if rows < 1:
    print("Please enter a positive whole number.")
    raise SystemExit(1)

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

How It Works

1. Prompt and validate. Catch ValueError; require rows >= 1 before printing.

2. Same core. print(i % 2, end="") matches Example 1 — odd prints 1, even prints 0.

3. Safer input tip. Cap demos for readable output:

Safer input tip
if rows < 1 or rows > 9:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit(1)

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm parity and shrinking length.

Python
rows = 3

for i in range(1, rows + 1):
    for j in range(i, rows + 1):
        if i % 2 == 0:
            print("0", end="")
        else:
            print("1", end="")
    print()

How It Works

1. Same rules. Odd i → 1; even i → 0; length = rows - i + 1.

2. Quick check. The last row is a single 1.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

grow instead

Loop j from 1 to i

That grows rows instead of shrinking them. Keep for j in range(i, rows + 1).

j % 2

Test j % 2 instead of i % 2

That flips inside a row (10101). The digit must depend on the row index i only.

print() inside

Broken rows

If bare print() sits inside the inner loop, you get one character per line. Call it only after the loop.

extra spaces

Accidental spacing

Default print(i % 2) adds a newline. Use end="" for tight digits.

rows = 1

Single digit

Output is just 1. A good sanity check for input validation.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Row lengths are n + (n−1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Rule: odd i prints 1; even i prints 0.
  • Shrink: inner range(i, rows + 1) makes length rows - i + 1.
  • end="" vs print(): digits stay on the line; bare print() advances after each row.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, pick 1 or 0 from i % 2 and repeat it while the row length shrinks.

Frequently Asked Questions

An alternating 1/0 triangle: for rows=5 you get 11111 / 0000 / 111 / 00 / 1 — odd rows are 1s, even rows are 0s, and each row shrinks by one.
It checks i % 2. When i is even, the row prints 0; when i is odd, the row prints 1.
The inner loop runs from j = i to rows, printing rows - i + 1 characters per row — decreasing from rows down to 1.
Print 1 - (i % 2) instead of i % 2, or swap the if/else outputs.
Program 39 rotates digits 1..rows per row. Program 40 prints only 1 or 0 per row based on parity, with shrinking row length.
Use print(i % 2, end=" ") instead of print(i % 2, end="").
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.

Did you know?

Odd rows print 1, even rows print 0 — chosen with i % 2. Row i prints rows - i + 1 characters; total prints = n(n+1)/2.

Next: Square Numbers Pyramid

Continue with a pyramid built from square numbers.

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