Alternating 1-0 Triangle (Starts with 1) in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Modulo Operator

What You’ll Learn

How to print an alternating 1-0 triangle in Python where every row starts with 1: 1, 10, 101, 1010, 10101.

This pattern is a simple exercise in using modulo to generate repeating sequences.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
10
101
1010
10101
1

Complete Python Program

Print j % 2 for j = 1..i. This produces 1,0,1,0,... on each row.

Python
rows = 5

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

🧠 How It Works

1

Set the number of rows

rows = 5 controls how many lines are printed.

Setup
2

Outer loop controls row length

for i in range(1, rows + 1) sets row length to 1, 2, 3, 4, 5.

Row control
3

Inner loop prints parity of j

for j in range(1, i + 1) prints j % 2, producing 1 for odd j and 0 for even j.

Modulo
4

New line after each row

print() moves to the next row.

Line break
=

Alternating sequence triangle

Total digits printed are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows at runtime using input():

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()

💡 Tips for Enhancement

Try These

  • Flip the pattern by printing (j + 1) % 2 instead of j % 2
  • Add spaces between digits for readability
  • Use j % k to create repeating cycles like 0-1-2
  • Build each row as a string and print it once for large sizes
  • Center the triangle by printing leading spaces before each row

Avoid

  • Forgetting print() between rows
  • Mixing up the inner loop bounds (it should run exactly i times)
  • Hardcoding values if you want a reusable pattern size
  • Assuming user input is always valid

Key Takeaways

1

Using j % 2 prints 1 for odd j and 0 for even j.

2

Starting j from 1 ensures every row begins with 1.

3

The outer loop controls row length, growing from 1 to rows.

4

Total digits printed follow the triangular number count: n(n+1)/2.

❓ Frequently Asked Questions

Program 15 may start a row with 0 depending on the starting j value. Program 16 starts j at 1 for each row, so every row begins with 1.
Print (j + 1) % 2 instead of j % 2.
Yes. Use print(j % 2, end=" "). If you want no trailing spaces, build and join a list.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More Python Number Patterns!

Keep going to discover more number pattern programs and strengthen your loop skills.

All Number Patterns →
Did you know?

Parity-based patterns show up everywhere—from chessboard coloring to toggles and bit tricks. Modulo is an easy way to express them in code.

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