Python Program to Print 1 and 0 in alternative rows

Write a Python Program to Print 1 and 0 in alternative Rows using For Loop and While Loop with an example.

Python Program to Print 1 and 0 in alternative rows using For Loop

This Python program allows user to enter the total number of rows and columns. Next, we used Python Nested For Loop to print 1’s and 0’s in alternative rows until it reaches the user-specified rows and columns.

# Python Program to Print 1 and 0 in alternative rows
 
rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : "))

print("Print Number Pattern - 1 and 0 in alternative rows") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(i % 2 != 0):          
            print('1', end = '  ')
        else:
            print('0', end = '  ')
    print()
Python Program to Print 1 and 0 in alternative rows

Python Program to Show 1 and 0 in alternative rows using While Loop

In this Python program, we replaced the Python For Loop with a Python while syntax

# Python Program to Print 1 and 0 in alternative rows
 
rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : "))

print("Print Number Pattern - 1 and 0 in alternative rows") 
i = 1
while(i <= rows):
    j = 1
    while(j <= columns):
        if(i % 2 != 0):          
            print('1', end = '  ')
        else:
            print('0', end = '  ')
        j = j + 1
    i = i + 1
    print()
Please Enter the total Number of Rows  : 12
Please Enter the total Number of Columns  : 18
Print Number Pattern - 1 and 0 in alternative rows
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
>>> 

Python Program to Display 1 and 0 in alternative rows without If

In this program, we are finding even or odd rows within the print statement. By doing this, you can avoid using the Python If statement inside a Python Nested For loop.

# Python Program to Print 1 and 0 in alternative rows
 
rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : "))

print("Print Number Pattern - 1 and 0 in alternative rows") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        print('%d' %(i % 2), end = '  ')
    print()
Please Enter the total Number of Rows  : 8
Please Enter the total Number of Columns  : 12
Print Number Pattern - 1 and 0 in alternative rows
1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  
>>> 

Python Program to Display 0 and 1 in alternative rows

If you want to print 0 and 1 in an alternative row, then replace 1 in print statement with 0, and 0 with 1. For more patterns, please refer to the Python Number pattern programs.

# Python Program to Print 1 and 0 in alternative rows
 
rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : "))

print("Print Number Pattern - 0 and 1 in alternative rows") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(i % 2 != 0):          
            print('0', end = '  ')
        else:
            print('1', end = '  ')
    print()
Please Enter the total Number of Rows  : 10
Please Enter the total Number of Columns  : 15
Print Number Pattern - 0 and 1 in alternative rows
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
>>> 

Also Read