In this post, you will learn different pattern programs in python such as left triangle pattern, right triangle pattern, diamond shape pattern, pyramid shape pattern, pascals shape pattern, inverted pyramid pattern, hourglass pattern, butterfly pattern, etc.

But before writing a program you should know about:

  1. for-loop &
  2. nested for-loop

So let us start writing python programs for all the patterns one by one.

Left triangle pattern

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

Source code

# left triangle pattern

length = int(input('Enter length of the pattern: '))

for i in range(length):
    for j in range(i+1):
        print('*',end='')
    print()  # for new line

In the above program, outer loop acts like rows, and the inner loop acts like a column.

NOTE: Most pattern programs are based on rows and columns. The first loop or outer loop is used for rows and the second or inner loop is used for the column.

Right triangle pattern

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

Source code

size = int(input('Enter length of the pattern: '))

for i in range(size):
    for j in range(2*(size-i)):  # first loop for empty space
        print(' ',end='')

    for j in range(i+1):   #second loop for printing *
        print('*',end=' ')

    print() # for new line

2*(size - i) formula helps you to print space in the right triangle pattern where i is the number of rows and size is the length of the pattern.

Pyramid shape pattern

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

Source code

size = int(input('Enter length of the pattern: '))

for i in range(size):
    for j in range(size,i+1,-1):
        print(' ',end='')

    for k in range(2*(i-1)+3):
        print('*',end='')

    print() # for new line

2*(i-1)+3 formula helps you to print * in the Pyramid shape pattern (you can also use the 2*(i-1)+1 formula to print * in the pyramid shape pattern).

Inverted Pyramid shape pattern

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

Source code

size = int(input('Enter length of the pattern: '))

for i in range(size-1):
    for j in range(i):
        print(' ',end='')

    for k in range((size-i)*2-3):
        print('*',end='')
    print()

(size-i)*2-3 formula helps you to print * in an Inverted Pyramid shape pattern where i is the number of rows.

Diamond shape pattern

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

Source code

size = int(input('Enter length of the pattern: '))

# upper pyramid
for i in range(size):
    for j in range(size,i+1,-1):
        print(' ',end='')

    for k in range(2*(i-1)+1):
        print('*',end='')

    print()

# lower pyramid
for i in range(size-1):
    for j in range(i):
        print(' ',end='')

    for k in range((size-i)*2-3):
        print('*',end='')

    print()

You can observe that diamond shape pattern made up of 2 section the upper section is the pyramid pattern and lower section is inverted pyramid pattern.

Pascals shape pattern

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

Source code

rows = int(input('Enter number of rows: '))

#upper section
for i in range(rows):
    for j in range(i,rows):
        print(' ',end='')
    
    for k in range(i+1):
        print('*',end='')

    print()

#lower section
for i in range(rows,0,-1):
    for j in range(i,rows+1):
        print(' ',end='')
    
    for k in range(i):
        print('*',end='')
    print()

You can observe that Pascal’s shape pattern is made up of 2 sections the upper section is a right triangle pattern and the lower section is an inverted right triangle pattern.

Hourglass pattern

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

Source code

my_rows = int(input('Enter number of rows: '))

# Upper section
for i in range(my_rows, 0, -1):
    for j in range(my_rows-i):
        print(" ", end="")

    for k in range(1, 2*i):
        print("*", end="")

    print()

#lower section
for i in range(2, my_rows+1):
    for j in range(my_rows-i):
        print(" ", end="")

    for k in range(1, 2*i):
        print("*", end="")

    print()

butterfly pattern

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

Source code

length = int(input('Enter number of rows: '))
# upper section
for i in range(length):
    for j in range(i):
        print('*',end='')
    
    for k in range(2*(length-i)):
        print(' ',end='')
    
    for l in range(i):
        print('*',end='')
    
    print()  # for new line

# lower section
for i in range(length):
    for j in range((length-1)-i+1):
        print('*',end='')
    
    for k in range(2*i):
        print(' ',end='')
    
    for l in range((length-1)-i+1):
        print('*',end='')
    
    print()  #for new line

    

there are two sections of the butterfly pattern the first section contains the first N rows and the second section contains the last N-1 rows in the butterfly pattern.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment