Python Program to Print 1 and 0 in alternative Columns

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

Python Program to Print 1 and 0 in alternative Columns 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 until it reaches the user-specified rows and columns.

# Python Program to Print 1 and 0 in alternative Columns
 
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 Columns") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(j % 2 == 0):          
            print('0', end = '  ')
        else:
            print('1', end = '  ')
    print()
Python Program to Print 1 and 0 in alternative Columns 1

Python Program to Display 1 and 0 in alternative Columns using While Loop

In this Python program, we replaced the For Loop with While Loop

# Python Program to Print 1 and 0 in alternative Columns
 
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 Columns")
i = 1
while(i <= rows):
    j = 1
    while(j <= columns):
        if(j % 2 != 0):          
            print('1', end = '  ')
        else:
            print('0', end = '  ')
        j = j + 1
    i = i + 1
    print()

Python output of 1 and 0 in alternative Columns

Please Enter the total Number of Rows  : 7
Please Enter the total Number of Columns  : 12
Print Number Pattern - 1 and 0 in alternative Columns
1  0  1  0  1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  1  0  1  0  

Python Program to Show 1 and 0 in alternative Columns without If

In this Python program, we are finding even or odd columns within the print statement. By this, you can avoid the extra If statement used inside the Nested For loop.

# Python Program to Print 1 and 0 in alternative Columns
 
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 Columns") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        print('%d' %(j % 2), end = '  ')
    print()

Python output of 1 and 0 in alternative Columns

Please Enter the total Number of Rows  : 5
Please Enter the total Number of Columns  : 8
Print Number Pattern - 1 and 0 in alternative Columns
1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0  
1  0  1  0  1  0  1  0 

Python Program to Display 0 and 1 in alternative Columns

If you want to print 0, and 1 in the alternative column, then replace 1 in print statement with 0, and 0 with 1

# Python Program to Print 1 and 0 in alternative Columns
 
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 Columns") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(j % 2 != 0):          
            print('0', end = '  ')
        else:
            print('1', end = '  ')
    print()

Python output of 0 and 1 in alternative Columns

Please Enter the total Number of Rows  : 6
Please Enter the total Number of Columns  : 9
Print Number Pattern - 1 and 0 in alternative Columns
0  1  0  1  0  1  0  1  0  
0  1  0  1  0  1  0  1  0  
0  1  0  1  0  1  0  1  0  
0  1  0  1  0  1  0  1  0  
0  1  0  1  0  1  0  1  0  
0  1  0  1  0  1  0  1  0