Pattern Programs in Python

Pattern Programs in Python

3 mins read2.3K Views 1 Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Sep 16, 2024 11:45 IST

Do you want to learn how to make different patterns in Python? This article provides 20 Python programs for different pyramid patterns.

2022_08_Pattern-Program-in-Python.jpg

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?

Number Pattern in Python

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:

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

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()
Copy code

Output:

2022_08_image-31.jpg
Type Conversion in Python: Types and Examples
How to use For Loop in C++
Comparison Operators in Python

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()
Copy code

Output:

2022_08_image-32.jpg

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()
Copy code

Output:

2022_08_image-33.jpg

Simple Inverted Number Pyramid


 
rows = int(input("Enter the number of rows: "))
#Outer for loop executing in reverse order
for 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()
Copy code

Output:

2022_08_image-34.jpg

Simple Inverted Number Pyramid in Descending Order


 
rows = int(input("Enter the number of rows: "))
#Outer for loop executing in reverse order
for 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()
Copy code

Output:

2022_08_image-36.jpg

Simple Inverted Pyramid of a Digit


 
rows = int(input("Enter the number of rows: "))
num = rows
#Outer for loop executing in reverse order
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):
print(num, end=" ")
#End line after each row
print()
Copy code

Output:

2022_08_image-37.jpg

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()
Copy code

Output:

2022_08_image-38.jpg

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()
Copy code

Output:

2022_08_image-39.jpg

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()
Copy code

Output:

2022_08_image-40.jpg

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()
Copy code

Output:

2022_08_image-41.jpg

Natural Number Pyramid


 
rows = int(input("Enter the number of rows: "))
num = 1
stop = 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
Copy code

Output:

2022_08_image-42.jpg

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()
Copy code

Output:

2022_08_image-43.jpg

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()
Copy code

Output:

2022_08_image-44.jpg

Even Number Pyramid in Descending Order


 
rows = int(input("Enter the number of rows: "))
lastnum = 2 * rows
even = 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()
Copy code

Output:

2022_08_image-45.jpg

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()
Copy code

Output:

2022_08_image-46.jpg

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()
Copy code

Output:

2023_09_Equilateral-Pyramid-of-Pattern.jpg

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()
Copy code

Output:

2022_08_image-48.jpg

Full Pyramid of Numbers 


 
rows = int(input("Enter number of rows: "))
k = 0
count=0
count1=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()
Copy code

Output:

2022_08_image-49.jpg

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()
Copy code

Output:

2022_08_image-50.jpg

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!!

About the Author
author-image
Vikram Singh
Assistant Manager - Content

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)