Programs for printing pyramid patterns in Python
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
- Full Pyramid Patterns in Python using Loop
- Full Pyramid Patterns in Python Recursion
- Pyramid Patterns in Python with Alphabet
- Pyramid Patterns in Python with Number
- Inverted Full Pyramid Patterns in Python
- Hollow Pyramid Patterns in Python
- Half Pyramid Patterns in Python
- Half Pyramid Patterns in Python using Loop
- Half Pyramid Patterns in Python using Recursion
- Pyramid Patterns in Python with Numbers
- Pyramid Patterns in Python with Alphabet
- Inverted Pyramid Patterns in Python
- Hollow Inverted Half Pyramid in Python
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
Python3
def full_pyramid(n):
for i in range ( 1 , n + 1 ):
for j in range (n - i):
print ( " " , end = "")
for k in range ( 1 , 2 * i):
print ( "*" , end = "")
|
Output
*
***
*****
*******
*********
Example 2: Full Pyramid Patterns in Python Recursion
Python3
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_space(spaces)
print_star(stars)
print ()
print_pyramid(n, current_row + 1 )
n = 5
print_pyramid(n)
|
Output
*
* *
* * *
* * * *
* * * * *
Example 3: Pyramid Patterns in Python with Alphabet
Python3
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
Python3
def print_number_pyramid(rows):
for i in range ( 1 , rows + 1 ):
for j in range (rows - i):
print ( " " , end = "")
for j in range ( 2 * i - 1 ):
print (j + 1 , end = "")
print ()
num_rows = 5
print_number_pyramid(num_rows)
|
Example 5: Inverted Full Pyramid Patterns in Python
Python3
def inverted_full_pyramid(n):
for i in range (n, 0 , - 1 ):
for j in range (n - i):
print ( " " , end = "")
for k in range ( 2 * i - 1 ):
print ( "*" , end = "")
print ( "\r" )
n = 5
inverted_full_pyramid(n)
|
Output
*********
*******
*****
***
*
Example 6: Hollow Pyramid Patterns in Python
Python3
def hollow_pyramid(n):
for i in range ( 1 , n + 1 ):
for j in range ( 1 , 2 * n):
if j < = n - i or j > = n + i:
print ( " " , end = "")
else :
print ( "*" , end = "")
print ()
rows = 5
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
Python3
def half_pyramid(n):
for i in range ( 1 , n + 1 ):
for j in range ( 1 , i + 1 ):
print ( "* " , end = "")
print ( "\r" )
n = 5
half_pyramid(n)
|
Output
*
* *
* * *
* * * *
* * * * *
Example 2: Half Pyramid Patterns in Python using Recursion
Python3
def print_half_pyramid(n):
if n > 0 :
print_half_pyramid(n - 1 )
print ( '*' * n)
rows = int ( input ( "Enter the number of rows for the half pyramid: " ))
print_half_pyramid(rows)
|
Output
*
**
***
****
*****
Example 3: Pyramid Patterns in Python with Numbers
Python3
def numpat(n):
num = 1
for i in range ( 0 , n):
num = 1
for j in range ( 0 , i + 1 ):
print (num, end = " " )
num = num + 1
print ( "\r" )
n = 5
numpat(n)
|
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Example 4: Half Pyramid Patterns in Python in Alphabets
Python3
def alphapat(n):
num = 65
for i in range ( 0 , n):
for j in range ( 0 , i + 1 ):
ch = chr (num)
print (ch, end = " " )
num = num + 1
print ( "\r" )
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
Python3
def inverted_half_pyramid(n):
for i in range (n, 0 , - 1 ):
for j in range ( 1 , i + 1 ):
print ( "* " , end = "")
print ( "\r" )
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.
Python3
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 ()
num_rows = 5
print ( "Hollow Inverted Half Pyramid:" )
print_hollow_inverted_half_pyramid(num_rows)
|
Output
Hollow Inverted Half Pyramid:
*****
* *
* *
* *
**
Last Updated :
11 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...