The Wayback Machine - https://web.archive.org/web/20250211085524/https://www.geeksforgeeks.org/programs-printing-pyramid-patterns-python/
Open In App

Programs for printing pyramid patterns in Python

Last Updated : 02 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Exploring and creating pyramid patterns in Python is an excellent way to enhance your programming skills and gain a deeper understanding of loops and control structures. By modifying and experimenting with the provided examples, you can create a variety of captivating patterns using Python.

Exploring Pyramid Patterns in Python

Pyramid patterns are sequences of characters or numbers arranged in a way that resembles a pyramid, with each level having one more element than the level above. These patterns are often used for aesthetic purposes and in educational contexts to enhance programming skills.

Full Pyramid Patterns in Python

A full pyramid pattern is a series of lines that form a pyramid-like structure. Each line contains a specific number of characters, and the number of characters on each line increases symmetrically as we move down the pyramid.

Example 1: Full Pyramid Patterns in Python using Loop

Python
# Function to print full pyramid pattern
def full_pyramid(n):
    for i in range(1, n + 1):
        # Print leading spaces
        for j in range(n - i):
            print(" ", end="")
        
        # Print asterisks for the current row
        for k in range(1, 2*i):
            print("*", end="")
        print()
   
full_pyramid(5)

Output
    *
   ***
  *****
 *******
*********

Example 2: Full Pyramid Patterns in Python Recursion

Python
def print_space(space):
    if space > 0:
        print(" ", end="")
        print_space(space - 1)

def print_star(star):
    if star > 0:
        print("*", end="")
        print_star(star - 1)

def print_pyramid(n, current_row=1):
    if current_row > n:
        return

    spaces = n - current_row
    stars = 2 * current_row - 1

    # Print spaces
    print_space(spaces)

    # Print stars
    print_star(stars)

    # Move to the next line for the next row
    print()

    # Print the pyramid for the next row
    print_pyramid(n, current_row + 1)

# Set the number of rows for the pyramid
n = 5

# Print the pyramid pattern
print_pyramid(n)

Output
    *
   ***
  *****
 *******
*********

Example 3: Pyramid Patterns in Python with Alphabet

Python
n = 5
alph = 65
for i in range(0, n):
    print(" " * (n-i), end=" ")
    for j in range(0, i+1):
        print(chr(alph), end=" ")
        alph += 1
    alph = 65
    print()

Output
      A 
     A B 
    A B C 
   A B C D 
  A B C D E 

Example 4: Pyramid Patterns in Python with Number

Python
def print_number_pyramid(rows):
    for i in range(1, rows + 1):
        # Print spaces
        for j in range(rows - i):
            print(" ", end="")
        # Print numbers
        for j in range(2 * i - 1):
            print(j + 1, end="")
        # Move to the next line after each row
        print()

# Example usage
num_rows = 5
print_number_pyramid(num_rows)

Output
    1
   123
  12345
 1234567
123456789

Example 5: Inverted Full Pyramid Patterns in Python

Python
# Function to print inverted full pyramid pattern
def inverted_full_pyramid(n):
    # Outer loop for the number of rows
    for i in range(n, 0, -1):
        # Inner loop for leading spaces in each row
        for j in range(n - i):
            print(" ", end="")
        # Inner loop for printing asterisks in each row
        for k in range(2*i - 1):
            print("*", end="")
        # Move to the next line after each row
        print("")

# Set the value of n (number of rows)
n = 5

# Call the function to print the inverted full pyramid
inverted_full_pyramid(n)

Output
*********
 *******
  *****
   ***
    *

Example 6: Hollow Pyramid Patterns in Python

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

# Set the number of rows for the pyramid
rows = 5

# Call the function with the specified number of rows
hollow_pyramid(rows)

Output
    *    
   * *   
  *   *  
 *     * 
*********

Half Pyramid Patterns in Python

In this example, the half pyramid starts with one asterisk in the first row and adds one more asterisk in each subsequent row. The print(“\r”) statement is used to move to the next line after printing each row. You can customize the value of n to create a half pyramid with a different number of rows.

Example 1: Half Pyramid Patterns in Python using Loop

Python
# Function to print a half pyramid pattern
def half_pyramid(n):
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print("* ", end="")
        print("")

# Example: Print a half pyramid with 5 rows
n = 5
half_pyramid(n)

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Example 2: Half Pyramid Patterns in Python using Recursion

Python
def print_half_pyramid(n):
    if n > 0:
        # Call the function recursively with a smaller value of n
        print_half_pyramid(n - 1)
        
        # Print '*' characters for the current row
        print('*' * n)

# Get the number of rows from the user
rows = int(input("Enter the number of rows for the half pyramid: "))

# Call the function to print the half pyramid pattern
print_half_pyramid(rows)

Output

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

Example 3: Pyramid Patterns in Python with Numbers

Python
# Function to demonstrate printing pattern of numbers
def numpat(n):
    
    # initialising starting number 
    num = 1

    # outer loop to handle number of rows
    for i in range(0, n):
    
        # re assigning num
        num = 1
    
        # inner loop to handle number of columns
            # values changing acc. to outer loop
        for j in range(0, i+1):
        
                # printing number
            print(num, end=" ")
        
            # incrementing number at each column
            num = num + 1
    
        # ending line after each row
        print("")

# Driver code
n = 5
numpat(n)

Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Example 4: Half Pyramid Patterns in Python in Alphabets

Python
# Function to demonstrate printing pattern of alphabets
def alphapat(n):
    
    # initializing value corresponding to 'A' ASCII value
    num = 65

    # outer loop to handle number of rows 5 in this case
    for i in range(0, n):
    
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
        
            # explicitly converting to char
            ch = chr(num)
        
            # printing char value 
            print(ch, end=" ")
    
        # incrementing number
        num = num + 1
    
        # ending line after each row
        print("\r")

# Driver Code
n = 5
alphapat(n)

Output
A 
B B 
C C C 
D D D D 
E E E E E 

Example 5: Inverted Pyramid Patterns in Python

Python
# Function to print inverted half pyramid pattern
def inverted_half_pyramid(n):
    for i in range(n, 0, -1):
        for j in range(1, i + 1):
            print("* ", end="")
        print("\r")

# Example: Inverted Half Pyramid with n = 5
n = 5
inverted_half_pyramid(n)

Output
* * * * * 
* * * * 
* * * 
* * 
* 

Example 6: Hollow Inverted Half Pyramid in Python

In this modified version, an additional check is added inside the second inner loop to determine whether to print a ” or a space. The condition if j == 0 or j == i – 1 or i == rows: ensures that ” is printed at the beginning, end, and for the last row, while spaces are printed in between. Adjust the value of num_rows to change the size of the hollow inverted half pyramid.

Python
def print_hollow_inverted_half_pyramid(rows):
    for i in range(rows, 0, -1):
        for j in range(rows - i):
            print(" ", end="")
        for j in range(i):
            if j == 0 or j == i - 1 or i == rows:
                print("*", end="")
            else:
                print(" ", end="")
        print()

# Example usage
num_rows = 5
print("Hollow Inverted Half Pyramid:")
print_hollow_inverted_half_pyramid(num_rows)

Output
Hollow Inverted Half Pyramid:
*****
 *  *
  * *
   **
    *

Programs for printing pyramid patterns in Python – FAQs

How to print diamond pattern using Python?

To print a diamond pattern in Python, you can use nested loops to control the number of spaces and stars on each line, both in the upper and lower halves of the diamond.

def print_diamond(n):
# Upper half including middle row
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
# Lower half
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))

# Example usage:
print_diamond(5)

This function print_diamond() takes an integer n as input and prints a diamond pattern with 2n-1 rows.

How to print a simple pattern in Python?

Printing a simple pattern like a triangle involves using a single loop to control the number of stars on each line.

def print_triangle(n):
for i in range(n):
print('*' * (i + 1))

# Example usage:
print_triangle(5)

This print_triangle() function prints a triangle pattern where each subsequent row has one additional star.

How to print hollow inverted pyramid star pattern in Python?

Printing a hollow inverted pyramid involves using conditionals within a loop to control where stars and spaces are placed on each line.

def print_hollow_inverted_pyramid(n):
for i in range(n, 0, -1):
if i == n or i == 1:
print('*' * i)
else:
print('*' + ' ' * (i - 2) + '*')

# Example usage:
print_hollow_inverted_pyramid(5)

This function print_hollow_inverted_pyramid() prints an inverted pyramid pattern where the top and bottom rows are full of stars, and the middle rows have a star at the beginning and end with spaces in between.

How to print a reverse pyramid?

To print a reverse pyramid, start with a full row of stars at the top and gradually reduce the number of stars per row as you move downwards.

def print_reverse_pyramid(n):
for i in range(n, 0, -1):
print(' ' * (n - i) + '*' * (2 * i - 1))

# Example usage:
print_reverse_pyramid(5)

This print_reverse_pyramid() function prints a reverse pyramid pattern where each subsequent row has fewer stars and more spaces.

Are there any built-in functions or libraries in Python for printing pyramid patterns?

Python does not have built-in functions specifically for printing pyramid patterns. However, you can achieve various patterns using basic control structures like loops and conditional statements. For more complex visualizations or graphical representations, libraries like numpy or matplotlib are typically used, but these are geared towards numerical data and plotting rather than simple text-based patterns.




Next Article

Similar Reads

three90RightbarBannerImg