Table of contents
1.
Introduction
2.
Full Pyramid Patterns in Python
2.1.
Full Pyramid Patterns in Python using Loop
2.2.
Full Pyramid Patterns in Python using Recursion
2.3.
Pyramid Patterns in Python with Alphabet
2.4.
Pyramid Patterns in Python with Numbers
2.5.
Inverted Full Pyramid Patterns in Python
2.6.
Hollow Pyramid Patterns in Python
3.
Half Pyramid Patterns in Python
3.1.
Half Pyramid Patterns in Python using Loop
3.2.
Half Pyramid Patterns in Python using Recursion
3.3.
Pyramid Patterns in Python with Numbers
3.4.
Half Pyramid Patterns in Python with Alphabets
3.5.
Inverted Pyramid Patterns in Python
3.6.
Hollow Inverted Half Pyramid in Python
4.
Frequently Asked Questions
4.1.
How can I create different pyramid patterns in Python?
4.2.
Can I use numbers or alphabets in pyramid patterns instead of stars?
4.3.
What is the difference between a full and half pyramid?
5.
Conclusion
Last Updated: Dec 30, 2024
Easy

Pyramid Program in Python

Author Gaurav Gandhi
0 upvote

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.

Pyramid Program in 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)
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


Output:

   *
   * *
  *   *
 *     *
*********


Explanation:

The first and last stars are printed in each row, and the space between them increases as we move down.

Half Pyramid Patterns in Python

A half pyramid has a smaller number of stars and is printed only on one side.

def half_pyramid(n):
    for i in range(1, n + 1):
        print("*" * i)

# Calling the function
half_pyramid(5)
You can also try this code with Online Python Compiler
Run Code


Output:

*
**
***
****
*****


Explanation:

In each row, the number of stars increases, but there is no need to manage spaces since the pyramid is "half."

Half Pyramid Patterns in Python using Loop

This example shows how to print a half pyramid pattern using a loop.

def half_pyramid_loop(n):
    for i in range(1, n + 1):
        for j in range(i):
            print("*", end="")
        print()
# Calling the function
half_pyramid_loop(5)
You can also try this code with Online Python Compiler
Run Code


Output:

*
**
***
****
*****


Explanation:

Each row contains an increasing number of stars, controlled by two loops.

Half Pyramid Patterns in Python using Recursion

Let’s print a half pyramid pattern using recursion.

def half_pyramid_recursion(n, i=1):
    if i > n:
        return
    print("*" * i)
    half_pyramid_recursion(n, i + 1)

# Calling the function
half_pyramid_recursion(5)
You can also try this code with Online Python Compiler
Run Code


Output:

*
**
***
****
*****

 

Explanation:

The function calls itself, printing a line of stars at each level until the base case is reached.

Pyramid Patterns in Python with Numbers

def half_pyramid_numbers(n):
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(j, end="")
        print()
# Calling the function
half_pyramid_numbers(5)
You can also try this code with Online Python Compiler
Run Code


Output:

1
12
123
1234
12345


Explanation:

Numbers increase in each row, starting from 1.

Half Pyramid Patterns in Python with Alphabets

This pattern uses alphabets in a half pyramid shape.

def half_pyramid_alphabets(n):
    alphabet = 65  # ASCII value of 'A'
    for i in range(1, n + 1):
        for j in range(i):
            print(chr(alphabet), end="")
            alphabet += 1
            if alphabet > 90:
                alphabet = 65
        print()

# Calling the function
half_pyramid_alphabets(5)
You can also try this code with Online Python Compiler
Run Code


Output:

A
AB
ABC
ABCD
ABCDE


Explanation:

Alphabets are printed sequentially, wrapping back to 'A' after 'Z'.

Inverted Pyramid Patterns in Python

An inverted pyramid starts wide and becomes narrower as you go down.

def inverted_pyramid_numbers(n):
    for i in range(n, 0, -1):
        for j in range(1, i + 1):
            print(j, end="")
        print()
# Calling the function
inverted_pyramid_numbers(5)
You can also try this code with Online Python Compiler
Run Code


Output:

12345
1234
123
12
1


Explanation:

The number of printed digits decreases with each row.

Hollow Inverted Half Pyramid in Python

Finally, let's look at a hollow inverted half pyramid.

def hollow_inverted_half_pyramid(n):
    for i in range(n, 0, -1):
        for j in range(1, i + 1):
            if j == 1 or j == i or i == n:
                print("*", end="")
            else:
                print(" ", end="")
        print()


# Calling the function
hollow_inverted_half_pyramid(5)
You can also try this code with Online Python Compiler
Run Code


Output:

*****
*   *
*  *
* *
*


Explanation:

The stars are printed at the start and end of each row, and spaces fill the middle.

Frequently Asked Questions

How can I create different pyramid patterns in Python?

You can create pyramid patterns using loops or recursion. Loops help print the patterns iteratively, while recursion calls the function repeatedly to build the pattern.

Can I use numbers or alphabets in pyramid patterns instead of stars?

Yes, you can easily modify the patterns to print numbers or alphabets by adjusting the print statements accordingly.

What is the difference between a full and half pyramid?

A full pyramid has a symmetrical shape with spaces and stars, while a half pyramid is printed only on one side without the spaces.

Conclusion

In this article, we have discussed different pyramid patterns in Python, including full, half, inverted, and hollow pyramids. We have looked at how to create these patterns using both loops and recursion, and also how to modify them to use numbers or alphabets instead of stars.

You can also check out our other blogs on Code360.

Live masterclass