Introduction
Pyramid programs are a popular concept in Python programming, often used to practice loops and improve logic-building skills. These programs create patterns in the shape of a pyramid using numbers, stars, or other characters. Writing pyramid programs helps beginners understand how nested loops work and enhances their problem-solving abilities. They are simple yet effective exercises for learning the basics of Python.

In this article, we will discuss different types of pyramid patterns, including full pyramids, inverted pyramids, half pyramids, and hollow pyramids. We will also look at examples using both numbers and alphabets. By the end of this article, you'll learn how to create different pyramid patterns using loops and recursion in Python.
Full Pyramid Patterns in Python
A full pyramid pattern is a symmetrical pattern of stars (or numbers or letters) where the top is the smallest and it gradually gets wider as you move down the rows. It’s a very common pattern used to learn about loops and indentation.
Full Pyramid Patterns in Python using Loop
Here's how you can print a full pyramid pattern using a loop in Python:
def full_pyramid(n):
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print stars
for k in range(2 * i - 1):
print("*", end="")
print() # Move to the next line
# Calling the function
full_pyramid(5)
Output:
*
***
*****
*******
*********
Explanation:
The first for loop controls the number of rows. The second loop prints spaces to center the stars. The third loop prints the stars (*), with the number of stars increasing as we go down the pyramid.
Full Pyramid Patterns in Python using Recursion
Now, let’s implement the same full pyramid pattern using recursion.
def full_pyramid_recursion(n, i=1):
if i > n:
return
# Print leading spaces
print(" " * (n - i), end="")
# Print stars
print("*" * (2 * i - 1))
full_pyramid_recursion(n, i + 1)
# Calling the function
full_pyramid_recursion(5)
Output:
*
***
*****
*******
*********
Explanation:
This function calls itself to print each row of the pyramid. The base case is when i exceeds n, at which point the recursion stops.
Pyramid Patterns in Python with Alphabet
Instead of stars, we can print alphabets in the pyramid pattern. Here’s how to do that:
def pyramid_with_alphabets(n):
alphabet = 65 # ASCII value of 'A'
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print characters
for k in range(2 * i - 1):
print(chr(alphabet), end="")
alphabet += 1
if alphabet > 90: # Wrap back to 'A' if we exceed 'Z'
alphabet = 65
print()
# Calling the function
pyramid_with_alphabets(5)
Output:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
Explanation:
We print the alphabet sequentially. After 'Z', it wraps back to 'A'.
Pyramid Patterns in Python with Numbers
Printing numbers instead of stars creates a different effect. Below is the code for printing a pyramid with numbers:
def pyramid_with_numbers(n):
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print numbers
for k in range(1, 2 * i):
print(k, end="")
print()
# Calling the function
pyramid_with_numbers(5)
Output:
1
123
12345
1234567
123456789
Explanation:
Numbers are printed in increasing order as we move across the pyramid.
Inverted Full Pyramid Patterns in Python
In an inverted pyramid, the widest part is at the top, and it gradually gets narrower as you move down.
def inverted_full_pyramid(n):
for i in range(n, 0, -1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print stars
for k in range(2 * i - 1):
print("*", end="")
print()
# Calling the function
inverted_full_pyramid(5)
Output:
*********
*******
*****
***
*
Explanation:
The first loop decreases the number of stars as we move down the pyramid. Spaces are printed before the stars, creating the inverted shape.
Hollow Pyramid Patterns in Python
A hollow pyramid has stars on the outer edges and spaces inside. Let's see how to create a hollow pyramid pattern.
def hollow_pyramid(n):
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print stars
for k in range(1, 2 * i):
if k == 1 or k == 2 * i - 1 or i == n:
print("*", end="")
else:
print(" ", end="")
print()
# Calling the function
hollow_pyramid(5)
Output:
*
* *
* *
* *
*********
Explanation:
The first and last stars are printed in each row, and the space between them increases as we move down.




