Pyramid Pattern In Python

Pyramid Pattern In Python | In this article, we will learn how to print the pyramid pattern in Python programming. Here we will discuss some of the examples that are given below:-

  1. Pyramid star pattern in python
  2. Inverted pyramid pattern in python
  3. Pyramid pattern in python using while loop
  4. Reverse pyramid pattern in python
  5. Alphabet pyramid pattern in python
  6. Half pyramid pattern in python
  7. Hollow pyramid star pattern in python
  8. Inverted hollow pyramid star pattern in python

We can print patterns in Python by simply using for loop. Here we will use nested for loop. The outer loop will handle the rows and the nested loop will handle the columns.

Pyramid Star Pattern in Python

Here, we are using for loop to print the pyramid pattern in python. Python program to print Pyramid Star Pattern is as below:-

def pattern(s):
   # number of spaces
   x = s - 1
   # Outer loop
   for i in range(s):
      # Inner loop
      for j in range(x):
         print(end=" ")

      # decrementing x after each loop
      x = x - 1
      for j in range(i+1):
         # printing stars
         print("* ", end="")
      print("\r")


# take inputs
s = int(input('Enter the number of rows: '))

# calling function
pattern(s)

Output:-

Enter the number of rows: 5
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Inverted Pyramid Pattern in Python

The program to make an inverted pyramid pattern is written below. Here we are using stars to make an inverted pyramid. You can use any numbers, alphabet or some special characters. It is up to your choice.

# Making Inverse Pyramid Pattern Using Stars

r = int(input('Enter number of rows required: '))
# outer loop
for i in range(r, 0, -1):
   # Inner loop
   for j in range(r - i):
      print(' ', end='')  # printing space and staying in same line

   for j in range(2 * i - 1):
      print('*', end='')  # printing * and staying in same line
   print()  # printing new line

Output:-

Enter number of rows required: 5
*********
 *******
  *****
   ***
    *

Pyramid Pattern in Python Using While Loop

Here we are using a while loop instead of using for loop for printing the pyramid pattern in Python programming.

# Program to print pyramid using while loop
p = int(input('Enter number of rows : '))

i = 1
# outer loop
while i <= p:
   j = 1
# Inner loop
   while j <= i:
      print("*", end=" ")
      j += 1
   print()
   i += 1

Output:-

Enter number of rows : 5
* 
* * 
* * * 
* * * * 
* * * * * 

Reverse Pyramid Pattern in Python

The program for printing reverse pyramid patterns in a python programming language is given below:-

# Take range input from the user
a = int(input("Enter the range: "))

# outer loop
for rows in range(a, 0, -1):
   # Inner loop
   for columns in range(0, rows):
      print("*", end=" ")
   print()

Output:-

Enter the range: 5
* * * * * 
* * * * 
* * * 
* * 
* 

Alphabet Pyramid Pattern in Python

Here to make an alphabet pyramid pattern, firstly we have to convert the ASCII value into the ASCII character. We use chr() function to convert ASCII values into ASCII characters.

# Program to print alphabet pyramid pattern
# outer loop
for i in range(65, 70):
   # inner loop
   for j in range(65, i+1):
      print(chr(j), end="")
   print()

Output:-

A
AB
ABC
ABCD
ABCDE

Half Pyramid Pattern in Python

The program for printing the half pyramid pattern is given below:

# Python program to print the half pyramid pattern using *
# take input from the user
row = int(input("Enter the number of rows: "))
# new line
print("")
# display the pattern
# outer loop
for i in range(1, row + 1):
   # print *
   # inner loop
   for j in range(1, i + 1):
      print("*", end="")
   # new line
   print("")

Output:-

Enter the number of rows: 5

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

Hollow Pyramid Star Pattern in Python

Firstly we will take input from the user and then use for loop in the program to print a hollow pyramid pattern in Python. The program for the hollow pyramid star pattern is given below:-

# program to print hollow star pyramid pattern
r = int(input('Enter number of rows : '))
for i in range(r):
   for j in range(r - i):
      print(' ', end='')  
   for j in range(2 * i + 1):
      if j == 0 or j == 2 * i or i == r - 1:
         print('*', end='')
      else:
         print(' ', end='')
   print()  # print new line

Output:-

Enter number of rows : 5
     *
    * *
   *   *
  *     *
 *********

Inverted Hollow Pyramid Star Pattern in Python

The program to print the inverted hollow pyramid star pattern in a python programming language is written down:-

# Program to Print Inverse Hollow Pyramid Star Pattern
r = int(input('Enter number of rows required: '))
for i in range(r, 0, -1):
   for j in range(r - i):
      print(' ', end='')  # printing space
   for j in range(2 * i - 1):
      if i == 0 or i == r or j == 2 * i - 2 or j == 0:
         print('*', end='')  # printing * and staying in same line
      else:
         print(' ', end='')  # printing space
   print()  # printing new line

Output:-

Enter number of rows required: 5
*********
 *     *
  *   *
   * *
    *

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *