Python Binary Number Triangle Pattern (Starting with 1)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An alternating binary triangle starting with 1 grows one digit per row, printing 0 or 1 from j % 2 as the inner index counts up — so every row begins with 1.
In Python you solve it with ascending outer and inner loops: print(j % 2, end="") prints each bit, then bare print() ends the row. Program 15 is the descending-inner twin (1, 01, 101…).
Approach
How to Solve It
Outer loop from 1 to rows. Inner loop from 1 to current i, printing j % 2. End each row with print().
Method
Idea
Best for
Ascending + j % 2
Count j up; print parity as the bit
Learning, interviews, exams
Flip bits
Print 1 - (j % 2)
Complementary triangle labs
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j % 2 (no newline)
print newline
Cheat sheet
Goal
Pattern
Grow row length
for i in range(1, rows + 1):
Print binary digit
for j in range(1, i + 1): print(j % 2, end="")
End the row
print()
Flip every bit
print(1 - (j % 2), end="")
Program 15 twin
for j in range(i, 0, -1): print(j % 2, end="")
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j % 2, end="")
Stays on the same line
Each binary digit
print()
Ends the current line
After the inner loop
Print all bits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the binary triangle updates instantly.
Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
10
101
1010
10101
Trace
Worked Walkthrough
Trace three outer values when rows = 5 — watch j % 2 flip as j counts up.
i
Inner j
j % 2
Printed row
1
1
1
1
2
1, 2
1, 0
10
3
1, 2, 3
1, 0, 1
101
5
1..5
1,0,1,0,1
10101
Total digits are 1+2+3+4+5 = 15 — the triangular number n(n+1)/2.
Code
Python Programs
Three complete programs: fixed height, flipped bits, and input(). Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer grows, inner counts up, print j % 2.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j % 2, end="")
print()
Output
1
10
101
1010
10101
How It Works
1. Outer grows the length.i runs from 1 to rows — that is how many bits each row gets.
2. Inner prints parity.j counts up from 1 to i; j % 2 is the bit — so each row starts with 1.
3. End the row. Call bare print() only after the inner loop finishes.
Example 2 — Flip with 1 - (j % 2)
Invert every bit so the first row starts with 0 instead of 1.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(1 - (j % 2), end="")
print()
Output
0
01
010
0101
01010
How It Works
1. Same loop bounds. Outer and inner ranges match Example 1.
2. Flip the bit.1 - (j % 2) turns 1 into 0 and 0 into 1.
3. First row becomes 0. Useful when a lab asks for the complementary binary triangle.
Example 3 — User Input
Read the row count from the keyboard. Prefer a try / except in real apps (shown in the tip below).
Python
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j % 2, end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
1
10
101
1010
How It Works
1. Same loop core. Only the source of rows changes from a literal to keyboard input.
2. Entering 4 stops early. You get four binary rows ending at 1010.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the number of rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong twin
for j in range(i, 0, -1)
That is Program 15 (1, 01, 101). Keep counting up for this pattern.
print(j)
print(j, end="") instead of print(j % 2, end="")
That prints the ascending numbers. This pattern needs the parity bit only.
print() inside
Bare print(j % 2) inside the inner loop
That puts each bit on its own line. Use print(j % 2, end="") for bits; call bare print() only after the row finishes.
rows = 1
Single bit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
spaces
Do not add spaces between bits
The classic sample is concatenated (10101), not spaced (1 0 1 0 1).
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / flip / input (Examples 1–3)
O(n²)
O(1)
There are n rows printing 1+2+…+n bits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for each row i, print j % 2 while j counts from 1 up to i.
Starts with 1: because the first j is always odd.
end= vs print(): bits stay on the line; bare print() advances after each row.
Next step: Program 17 prints left-shifted odd numbers in a growing triangle.
One line: for each row length i, print j % 2 from 1 up to i with end="", then call print().
Frequently Asked Questions
Modulo 2 returns the remainder after dividing by 2. Any integer is either even (remainder 0) or odd (remainder 1).
On row 2, the inner loop prints j = 1 then j = 2. That becomes 1 % 2 = 1 then 2 % 2 = 0, so the row is 10.
print(j % 2, end="") stays on the same line. print() ends the current line. Binary digits use end=""; the row break uses print() after the inner loop.
Row 3 prints j = 1, 2, 3 and j % 2 becomes 1, 0, 1 — concatenated as 101.
Program 16 counts the inner loop up (range(1, i + 1)) producing 1, 10, 101, 1010, 10101. Program 15 counts down (range(i, 0, -1)) producing 1, 01, 101, 0101, 10101.
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
O(n²) where n is the number of rows. Total digit prints equal n+(n-1)+…+1 = n(n+1)/2.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require rows >= 1.
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 prints alternating 0 and 1 using j % 2. The inner loop counts up with range(1, i + 1), so every row starts with 1 — still O(n²) total prints.