Pyramid Pattern in Python
In this tutorial, we will look at 12 different pyramid pattern in Python programming language. We will also look at the code for each pattern.
Pyramid patterns are very famous in programming interviews. So, it is very important to know how to print patterns in Python. Let's look at 12 different pyramid patterns using stars, numbers, and alphabets.
Prerequisites
- Star Pyramid Pattern
- Hollow Star Pyramid Pattern
- Reverse Star Pyramid Pattern
- Hollow Reverse Star Pyramid Pattern
- Number Pyramid Pattern
- Hollow Number Pyramid Pattern
- Reverse Number Pyramid Pattern
- Hollow Reverse Number Pyramid Pattern
- Alphabet Pyramid Pattern
- Hollow Alphabet Pyramid Pattern
- Reverse Alphabet Pyramid Pattern
- Hollow Reverse Alphabet Pyramid Pattern
Table Of Contents
1. Star Pyramid Pattern in Python
* *** ***** ******* *********
You can look at the pyramid pattern of stars above. Geometrically it looks like an equilateral triangle.
The steps to create this pattern are as follows:
- Take input from the user for the size of the pyramid.
- Create an external loop that will run from 0 to size.
- Inside external loop creates 2 internal loops. One for printing spaces and another for printing stars.
- Inside the first internal loop, print size - i - 1 space. Here i is the variable of the external loop.
- Inside the second internal loop, print 2 * i + 1 stars.
- Print a new line at the end of both internal loops.
- Method 1
- Method 2
# pyramid star pattern
size = int(input("Enter the size: "))
for i in range(size):
for j in range(size - i - 1):
print(' ', end='')
for k in range(2 * i + 1):
print('*', end='')
print()
# pyramid star pattern
size = int(input("Enter the size: "))
for i in range(size):
print(' ' * (size - i - 1) + '*' * (2 * i + 1))
Output:
Enter the size: 5 * *** ***** ******* *********
2. Hollow Star Pyramid Pattern in Python
* * * * * * * *********
By the name you can understand that this pyramid has no internal parts. It is hollow from the inside. Stars are only printed at the edges.
This is a bit more complex pattern than the previous one. So, let's look at the steps to create this pattern:
- First, take the size input.
- The external loop will run from 0 to size.
- The first internal loop prints the space before the edge stars. That is from 0 to size - i - 1. Where i is the current row number.
- The main trick is at the second internal loop. Here we print stars only at the first and last position of the pyramid. That is at k == 0 and k == 2 * i. If the current row is the last row then print a star at every position.
# hollow pyramid using star
size = int(input("Enter the size: "))
for i in range(size):
# printing spaces
for j in range(size - i - 1):
print(' ', end='')
# printing stars
for k in range(2 * i + 1):
# print star at the start and end of the row
if k == 0 or k == 2 * i:
print('*', end='')
else:
if i == size - 1:
print('*', end='')
else:
print(' ', end='')
print()
Output:
Enter the size: 5 * * * * * * * *********
3. Reverse Star Pyramid Pattern in Python
********* ******* ***** *** *
A reverse pyramid is the same as a pyramid pattern but it's upside down. So, the steps to create this pattern are the same as the previous one. The only difference is that we will print the stars in reverse order.
Inside the external loop, print spaces start from 0 and increase by 1 in each iteration. and print star for 2 * (size - i - 1) + 1 times.
# reverse pyramid pattern
size = int(input("Enter the size: "))
for i in range(size):
# printing spaces
for j in range(i):
print(' ', end='')
# printing stars
for j in range(2 * (size - i - 1) + 1):
print('*', end='')
print()
Output:
Enter the size: 5 ********* ******* ***** *** *
4. Hollow Reverse Star Pyramid Pattern in Python
********* * * * * * * *
The hollow reverse star pyramid is the same as the reverse pyramid but hollow inside.
The only difference in the code comes when you start printing stars. Print stars only at the boundary of the pyramid. That is at k == 0 and k == 2 * (size - i - 1). If the current row is the first row then print a star at every position.
# hollow reverse pyramid pattern
size = int(input("Enter the size: "))
for i in range(size):
# printing spaces
for j in range(i):
print(' ', end='')
# printing stars
for k in range(2 * (size - i - 1) + 1):
if k == 0 or k == 2 * (size - i - 1):
print('*', end='')
else:
# print only stars in the first row
if i == 0:
print('*', end='')
else:
print(' ', end='')
print()
Output:
Enter the size: 5 ********* * * * * * * *
5. Number Pyramid Pattern in Python
1 123 12345 1234567 123456789
The number pyramid is the same as the pyramid pattern but instead of stars, we will print numbers.
# number pyramid
size = int(input("Enter the size: "))
for i in range(size):
# print spaces
for j in range(size - i - 1):
print(" ", end="")
# print numbers
for k in range(2 * i + 1):
print(k+1, end="")
print()
Output:
Enter the size: 5 1 123 12345 1234567 123456789
6. Hollow Number Pyramid Pattern in Python
1 1 2 1 2 1 2 123456789
Here is the Python program for the hollow number pyramid pattern.
# hollow number pyramid
size = int(input("Enter the size: "))
for i in range(size):
# print spaces
for j in range(size - i - 1):
print(" ", end="")
# print numbers
count = 1;
for k in range(2 * i + 1):
if i == 0 or i == size - 1:
print(count, end="")
count += 1
else:
if k == 0 or k == 2 * i:
print(count, end="")
count += 1
else:
print(" ", end="")
print()
Output:
Enter the size: 5 1 1 2 1 2 1 2 123456789
7. Reverse Number Pyramid Pattern in Python
123456789 1234567 12345 123 1
Here is the Python program for reverse number pyramid.
# reverse number pyramid
size = int(input("Enter the size: "))
for i in range(size):
# print spaces
for j in range(i):
print(" ", end="")
# print numbers
for k in range(2 * (size - i) - 1):
print(k+1, end="")
print()
Output:
Enter the size: 5 123456789 1234567 12345 123 1
8. Hollow Reverse Number Pyramid Pattern in Python
123456789 1 2 1 2 1 2 1
Here is the Python program for the hollow reverse number pyramid.
size = int(input("Enter the size: "))
for i in range(size):
# print spaces
for j in range(i):
print(" ", end="")
# print numbers
count = 1
for k in range(0, 2 * (size - i) - 1):
if k == 0 or k == 2 * (size - i) - 2:
print(count, end="")
count += 1
else:
if i == 0:
print(count, end="")
count += 1
else:
print(" ", end="")
print()
Output:
Enter the size: 5 123456789 1 2 1 2 1 2 1
9. Alphabet Pyramid Pattern in Python
A ABC ABCDE ABCDEFG ABCDEFGHI
Here is the Python program for the alphabet pyramid.
# alphabet pyramid pattern
size = int(input("Enter the size: "))
alpha = 65
for i in range(size):
# print spaces
for j in range(size - i):
print(" ", end="")
# print alphabets
for k in range(2 * i + 1):
print(chr(alpha + k), end="")
print()
Output:
Enter the size: 6 A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFGHIJK
10. Hollow Alphabet Pyramid Pattern in Python
A B C D E F G HIJKLMNOP
Here is the Python program for the hollow alphabet pyramid.
# hollow alphabet pyramid pattern
size = int(input("Enter the size: "))
alpha = 65
for i in range(size):
count = 0
# print spaces
for j in range(size - i - 1):
print(" ", end="")
# print alphabet
for k in range(2 * i + 1):
if i == 0 or i == size - 1:
print(chr(alpha+count), end="")
count += 1
else:
if k == 0 or k == 2 * i:
print(chr(alpha+count), end="")
count += 1
else:
print(" ", end="")
print()
Output:
Enter the size: 5 A A B A B A B ABCDEFGHI
11. Reverse Alphabet Pyramid Pattern in Python
ABCDEFGHI ABCDEFG ABCDE ABC A
Here is the Python program for the reverse alphabet pyramid.
# reverse alphabet pyramid pattern
size = int(input("Enter the size: "))
alpha = 65
for i in range(size):
# print spaces
for j in range(i):
print(" ", end="")
# print alphabets
for k in range(2 * (size - i) - 1):
print(chr(alpha + k), end="")
print()
Output:
Enter the size: 5 ABCDEFGHI ABCDEFG ABCDE ABC A
12. Hollow Reverse Alphabet Pyramid Pattern in Python
ABCDEFGHI A B A B A B A
Here is the Python program for the hollow reverse alphabet pyramid.
# hollow reverse alphabet pyramid pattern
size = int(input("Enter the size: "))
alpha = 65
for i in range(size):
# print spaces
for j in range(i):
print(" ", end="")
# print alphabets
count = 0
for k in range(0, 2 * (size - i) - 1):
if k == 0 or k == 2 * (size - i) - 2:
print(chr(count+alpha), end="")
count += 1
else:
if i == 0:
print(chr(count+alpha), end="")
count += 1
else:
print(" ", end="")
print()
Output:
Enter the size: 5 ABCDEFGHI A B A B A B A