Pattern Programs in Python
Do you want to learn how to make different patterns in Python? This article provides 20 Python programs for different pyramid patterns.
When preparing for Python-related jobs as a beginner, you need to practice pattern programs to better understand conditional statements and loops in Python. Performing pattern printing are interviewer-favorite questions to test your Python skills. This article has compiled a list of the 20 most common pattern programs in python to help you prepare for that job! So, without further ado, let’s get started, shall we?
Explore How to Find an Armstrong Number Using Python
How to Print Patterns in Python
In Python, we commonly use multiple for loops, and sometimes while loops, too, to print the various patterns. Here, the first outer loop prints the number of rows, and the inner loop prints the number of columns of the pattern. Most pattern programs are variations of each other and work on the same logic.
We will be covering the following pattern programs today:
- Simple * Pattern Pyramid
- Simple Number Pyramid
- Simple Alphabet Pyramid
- Simple Inverted * Pattern Pyramid
- Simple Inverted Number Pyramid
- Simple Inverted Number Pyramid in Descending Order
- Simple Inverted Pyramid of a Digit
- Number Pattern Semi-Pyramid
- Number Pattern Mirrored Semi-Pyramid
- Number Pattern Inverted Semi-Pyramid
- Reverse Number Pyramid
- Natural Number Pyramid
- Palindrome Number Pyramid
- Alternate/Odd Number Pyramid
- Even Number Pyramid in Descending Order
- Horizontal Tables Pyramid
- Equilateral Pyramid of * Pattern
- Inverted Equilateral Pyramid of * Pattern
- Full Pyramid of Numbers
- Pascal’s Triangle Pyramid
Best-suited Python courses for you
Learn Python with these high-rated online courses
Simple * Pattern Pyramid
#Outer for loop to handle number of rows for i in range(0, rows): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, i + 1): print("* ", end=" ") #End line after each row print()
Output:
Also explore: Your One Stop Guide to Learn About Python
Simple Alphabet Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for num in range(rows+1): #Inner for loop to handle number of columns #values change according to the outer loop for i in range(num): print(num, end=" ") #End line after each row print()
Output:
Simple Inverted * Pattern Pyramid
rows = int(input("Enter number of rows: "))
ascii_value = 65
#Outer for loop to handle number of rows for i in range(rows): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(i+1): alphabet = chr(ascii_value) print(alphabet, end=" ") ascii_value += 1 print()
Output:
Simple Inverted Number Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop executing in reverse orderfor i in range(rows + 1, 0, -1): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, i - 1): print("* ", end=" ") #End line after each row print()
Output:
Simple Inverted Number Pyramid in Descending Order
rows = int(input("Enter the number of rows: "))
#Outer for loop executing in reverse orderfor i in range(rows, 0, -1): num = i #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, i): print(num, end=" ") #End line after each row print()
Output:
Simple Inverted Pyramid of a Digit
rows = int(input("Enter the number of rows: ")) num = rows
#Outer for loop executing in reverse orderfor i in range(rows, 0, -1): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, i): print(num, end=" ") #End line after each row print()
Output:
Number Pattern Semi-Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for i in range(1, rows+1): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(1, i + 1): print(j, end=" ") #End line after each row print()
Output:
Number Pattern Mirrored Semi-Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for i in range(1, rows+1): num = 1 #Inner for loop to handle number of columns #values change according to the outer loop for j in range(rows,0,-1): if j > i: print(" ", end=" ") else: print(num, end=" ") num += 1 #End line after each row print()
Output:
Number Pattern Inverted Semi-Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for i in range(rows,0,-1): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, i + 1): print(j, end=" ") #End line after each row print()
Output:
Reverse Number Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for i in range(1, rows+1): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(i,0,-1): print(j, end=" ") #End line after each row print()
Output:
Natural Number Pyramid
rows = int(input("Enter the number of rows: ")) num = 1stop = 2
#Outer for loop to handle number of rows for i in range(rows): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(1, stop): print(num, end=" ") num += 1 #End line after each row print() stop += 2
Output:
Palindrome Number Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for i in range(1, rows+1): for j in range(1, i-1): print(j, end=" ") num += 1 for j in range(i-1,0,-1): print(j, end=" ") print()
Output:
Alternate/Odd Number Pyramid
rows = int(input("Enter the number of rows: ")) i = 1
while i <= rows: j = 1 while j <= i: print((i * 2-1), end=" ") j += 1 i += 1
print()
Output:
Even Number Pyramid in Descending Order
rows = int(input("Enter the number of rows: ")) lastnum = 2 * rowseven = lastnum
#Outer for loop to handle number of rows for i in range(1, rows+1): even = lastnum for j in range(i): print(even, end=" ") even -= 2 print()
Output:
Horizontal Tables Pyramid
rows = int(input("Enter the number of rows: "))
#Outer for loop to handle number of rows for i in range(0, rows): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, i+1): print(i*j, end=" ")
#End line after each row print()
Output:
Equilateral Pyramid of * Pattern
rows = int(input("Enter the number of rows: ")) m = (2*rows)-2
#Outer for loop to handle number of rows for i in range(0, rows): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(0, m): print(end=" ") #decrementing m after each loop m -= 1 for j in range(0, i+1): #printing triangle pyramid using asterik print("*",end=" ") #End line after each row print()
Output:
Inverted Equilateral Pyramid of * Pattern
rows = int(input("Enter the number of rows: ")) m = (2*rows)-2
#Outer for loop to handle number of rows for i in range(rows-1,-1,-1): #Inner for loop to handle number of columns #values change according to the outer loop for j in range(m,0,-1): print(end=" ") #incrementing m after each loop m += 1 for j in range(0, i+1): #printing triangle pyramid using asterik print("*",end=" ") #End line after each row print()
Output:
Full Pyramid of Numbers
rows = int(input("Enter number of rows: "))
k = 0count=0count1=0
for i in range(1, rows+1): for space in range(1, (rows-i)+1): print(" ", end="") count+=1 while k!=((2*i)-1): if count<=rows-1: print(i+k, end=" ") count+=1 else: count1+=1 print(i+k-(2*count1), end=" ") k += 1 count1 = count = k = 0 print()
Output:
Pascal’s Triangle Pyramid
rows = int(input("Enter number of rows: "))coef = 1
for i in range(1, rows+1): for space in range(1, rows-i+1): print(" ",end="") for j in range(0, i): if j==0 or i==0: coef = 1 else: coef = coef * (i - j)//j print(coef, end = " ") print()
Output:
Conclusion
In this article, we have discussed 20 questions on pattern programming in Python. These are basic programming problems for beginners.
Keep Learning!!
Keep Sharing!!
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio
Comments
(1)
P
3 months ago
Report Abuse
Reply to Pooja