Python Program to Print 0-010-01010 Pyramid Number Pattern
Question: write a program in Python to generate a 0-010-01010-0101010 pyramid number pattern up to n lines, where n is given by the user.
Python Source Code: 0-010-01010 Pattern
In this program, we first read the value of n from the user and loop n times to control the number of lines using loop control variable i. Loop controlled by j is responsible for generating related spaces for patterns. Similarly, a loop controlled by k is responsible for generating 0s and 1s for the pyramid pattern. print()
takes control of execution to the new line.
# Program to print 0-010-01010
n = int(input("Enter number of lines of pattern: "))
for i in range(1,n+1):
for j in range(1, n-i+1):
print(" ", end="")
for k in range(1,2*i):
print((k+1)%2, end="")
print()
The output of the above program is:
Enter number of lines of pattern: 10 0 010 01010 0101010 010101010 01010101010 0101010101010 010101010101010 01010101010101010 0101010101010101010