Python Program to Print a 0-1 Triangle Pattern
Beginner · Patterns
A 0-1 triangle, also called a binary triangle, is a right-angled triangle filled with 0s and 1s that take turns. In this version each row starts with the opposite digit to the row above, so the digits also alternate down every column, like a chessboard.
Program
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
How it works
- Row
ihasidigits, numberedjfrom 1 toi, the same shape as a right-angled star triangle. - The digit depends on whether
i + jis even or odd, and moving one place right or one row down changes the sum by 1, so neighbouring digits always differ. - Row 1 starts with 1 because 1 + 1 is even, row 2 starts with 0 because 2 + 1 is odd, and so on, which is why the first column alternates too.
end=" "keeps a row on one line with spaces between the digits, and the emptyprint()ends the row.
Other ways to write it
Every row starts with 1
1 1 0 1 0 1 1 0 1 0 1 0 1 0 1
(j + 1) % 2 is 1 for the first digit of each row, then 0, then 1, so every row reads 1 0 1 0 ... from the left, the other common version of this pattern.
Flipping one bit across the rows
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
A single bit flips from one digit to the next and carries on into the next row, like the counter in Floyd's triangle.
Try it yourself
Print * in place of 1 and a space in place of 0, and look at the pattern it makes.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises