Pattern Programs in Python

Pattern Programs in Python

3 mins read2.3K Views Comment
clickHere
Updated on Oct 13, 2023 14:05 IST

Do you want to learn how to make different patterns in python.Here in this article you will find 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
Number Pattern in Python
Pattern programs in Python are a type of problem that uses nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

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 is used to print the number of rows, and the inner loop is used to print the number of columns of the said pattern. Most pattern programs are a variation of each other and work on the same logic.

We will be covering the following pattern programs today:

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
Type Conversion in Python: Types and Examples
Type conversion in Python refers to the direct conversion of object of one data type to another data type. In this conversion, Python interpreter automatically performs conversion.
How to use For Loop in C++
How to use For Loop in C++
Loops are used to repeat the same, or similar code number of times. In this article we will discuss how to implement for loops in c++.
Comparison Operators in Python
Comparison Operators in Python
Operators are symbols that are used to perform operation on variables and values. In this article we will discuss 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
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments