Programs for printing pyramid patterns in Python
Last Updated :
02 Sep, 2024
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)
Output1
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)
OutputA
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)
OutputHollow 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.
Similar Reads
Pattern Programs in C
Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements. In this art
15+ min read
C Program For Printing Right Half Pyramid Pattern
A half-right pyramid consists of rows with sequential stars, numbers or characters arranged in a triangular shape. The first row has one character, the second row has two, and so on. The characters are aligned to the left making it similar to the right-angle triangle. In this article, we will learn
5 min read
C Program to Print Pyramid Pattern
In C, a pyramid pattern consists of numbers, stars, or alphabets arranged in a triangular shape. In this article, we will learn how to print different shapes of pyramid patterns using C program. Following are the 6 common pyramid patterns: Right Half Pyramid PatternRight half pyramid pattern looks l
13 min read
C Program to Print Number Pattern
A number pattern involves printing numbers in a specific arrangement or shape, often in the form of a pyramid, triangle, or other geometric shapes. They are great for practicing loops and conditional statements. In this article, we will learn how to print different number patterns in C. Rhombus Numb
6 min read
C Program to Print Continuous Character Pattern
Here, we will see how to print continuous character patterns using a C program. Below are the examples: Input: rows = 5Output: A B C D E F G H I J K L M N O Input: rows = 3Output: A B C D E F There are 2 ways to print continuous character patterns in C: Using for loop.Using while loop. Let's discuss
5 min read
C Program To Print Character Pyramid Pattern
Pyramid patterns is a classic logical programming exercise where a triangular looking pattern is printed by treating the output screen as a matrix and printing a given character. In this article, we will explore how to print various alphabet pyramid patterns using C program. Half Pyramid PatternHalf
4 min read
C Program to Print Right Half Pyramid Pattern
The Right Half Pyramid Pattern is a triangular pattern consists of rows where each row contains an increasing number of characters. The number of characters starts from 1 and increases by 1 in each subsequent row. Characters are aligned to the left, resembling a right-angle triangle with its hypoten
2 min read
C Program To Print Hollow Pyramid Patterns
The Hollow Pyramid patterns are the variation of pyramid patterns where only the outer edges are filled with characters but the interior is left empty. In this article, we will learn how to print different hollow pyramid patterns. There can be 5 hollow pyramid patterns corresponding to each of the n
12 min read
C Program to Print Cross or X Pattern
The Cross or X Pattern is a pattern where characters or stars are printed diagonally from top-left to bottom-right and from top-right to bottom-left, forming an "X" shape. In this article, we will learn how to print this pattern using a C program. Program to Print Cross or X Pattern[GFGTABS] Star Cr
3 min read
Programs to print Interesting Patterns
Program to print the following pattern: Examples : Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program is divided into four parts. C/C++ Code // C++ program to print // the given pattern #include<io
15+ min read