# 10 Pattern Programs in Python to Make Some Fun

Original article was published on Artificial Intelligence on Medium ## The two dimensional plane

A two dimensional surface is the major thing on which each elements will be placed. Each location of the 2D plane can be described using indexes of two different direction. The following picture will clearly shows the arrangement of 2D plane.

In the above plane each cell can be addressed with help of two integers. The actual pattern program will create the 2 dimensional plane with some exception to printing the element. In most cases the pattern programs will have two for loops. On controls the columns and another controls the rows.

In this article will create the following pattern programs using python programming language.

💥 Rectangular Pattern

💥 Hollow Rectangle Pattern

💥 Half Pyramid Pattern

💥 Inverted Half Pyramid Pattern

💥 Pyramid Pattern

💥 Inverted Pyramid Pattern

💥 Diamond Pattern

💥 Hollow Diamond Pattern

💥 Valley Pattern

💥 Heart Pattern

## Rectangular Pattern

Rectangular pattern is the basic pattern in which the elements will be placed in all the places in the 2 D plane. We need two parameters to develop the rectangular program. One is height and another is length. The following diagram will show that the arrangement of elements in the pattern.

## Step 1:

Creating for loop for tracing the no of rows. Here the the no of rows is the height of the rectangle that is 4. So we have to create a range function which generates the continuous numbers from 0 to 4. (0,1,2,3).

`for x in range(4):`

## Step 2:

The next step is to create a for loop which works each time the first for loop gets repeated. This for loop must be nested within the first for loop. This one will loop for no of columns present in the rectangle. (0,1,2,3,4,5)

`for x in range(4): for y in range(6):`

## Step 3:

The final step of the pattern program is to putting conditions for the second for loop. The rectangular pattern doesn’t require any conditions because it contains the elements distributed every places.

`for x in range(4): for y in range(6): print("*",end="") print()`

After each successful running of column for loop we have to print a new line. So that we can use empty print function to make upcoming values to be printed on next line. The output of the above code is given below.

`************************`

## Working with numbers on Rectangular Pattern

Just replacing the asterisk with the value of y we will get the following result.

`for x in range(4): for y in range(6): print(y+1,end="") print()`

Output

`123456123456123456123456`

This will help you to understand the state of looping variables at each run. Spend some time on paper and learn how a loop behaves each time when it is running.

## Hollow Rectangular Pattern

Hollow rectangular patter is same as the rectangular patter but in this only the edge elements will be filled with some elements. Here we have to find a condition which can help the program to find which location is at edge. Let’s look at the following rectangular arrangement of the asterisk.

## Step 1:

Creating for loop for tracing the no of rows. Here the the no of rows is the height of the rectangle that is 4. So we have to create a range function which generates the continuous numbers from 0 to 4. (0,1,2,3).

`for x in range(4):`

## Step 2:

The next step is to create a for loop which works each time the first for loop gets repeated. This for loop must be nested within the first for loop. This one will loop for no of columns present in the rectangle. (0,1,2,3,4,5)

`for x in range(4): for y in range(6):`

## Step 3:

In this step we should put the condition to print the value at a particular location. Look at the each location at which contains the symbol in it. The location will satisfy at least any one of the following conditions.

1. The value of x will be 0 to 3.
2. The value of y will be 0 to 5.

The zeroth column and last column can be filled if y = 0 or y = 5. The zeroth row and last row can be filled column can be filled if x =0 or x = 3. Mathematically the condition can be written as`(x==0 or x==3) or (y==0 or y==5)` . If a location fails to satisfy the condition then we should print a `whitespace` in the co ordinate.

So that the program will be,

`for x in range(4): for y in range(6): if ((x==0 or x==3) or (y==0 or y==5)): print("*",end="") else: print(" ",end="") print()`

## Half Pyramid Pattern

Half pyramid is a pattern in which the no of column filled is gradually decreased from bottom to top and each column starts from the zeroth index. Below two dimensional arrangement will help you to understand the structure of half pyramid pattern of size 6.

The no of loops for rows and columns are equal and can be created as usual using nested for loops. The condition for printing the value should be found. Look at the stars in the above illustration. The no of stars a column having is equal to the index of row + 1. So that each time when the inner for loop runs it must run for a range of indexes from 0 to the row_value+1.

`for x in range(0,6): for y in range(0,x+1): print("*",end="") print()`

Output

`*********************`

To understand better we can print the row+1.

`for x in range(0,6): for y in range(0,x+1): print(y+1,end="") print()`

Output

`112123123412345123456`

## Hollow Half Pyramid Pattern

The half pyramid pattern with the edge element only filled is called hollow half pyramid pattern. This hollow half pyramid pattern also takes only one parameter that is base. The arrangement diagram for hollow half pyramid is given below.

The condition for hollow half pyramid is very simple. The star should be printed in a column if the column value is equal to 0 or row_value. Otherwise we should print `white space`. But look at the last line this will not satisfy the condition. So that we have to separate the code into two parts.

`for x in range(5): for y in range(5): if(y==0 or y==x): print("*",end="") else: print(" ",end="") print()for z in range(6): print("*",end="")`

Output

`* ** * * * * * *******`

## Inverted Half Pyramid Pattern

The reversed version of the half pyramid pattern is called inverted half pyramid pattern. In this half pyramid pattern the values are reduced from higher to lower using lower. The arrangement of inverted half pyramid pattern is given below.

`for x in range(6): for y in range(6,x,-1): print("*",end="") print()`

## Pyramid Pattern

Pyramid is an inversion of half pyramid in which each row is centralized in its arrangement. The following arrangement will show the structure of pyramid pattern of size 4.

The base value of this pyramid will be size*2 +1 which is 7 here. So each column value in the range from (size-2-x) to (base-(size-x)+1). Hence the code will be

`size=4base=size*2-1-1for x in range(size): for y in range(base): if (y>(size-2-x) and y<(base-(size-x)+1)): print("*",end="") else: print(" ",end="")  print()`

Output

` *  ***  ***** *******`

## Inverted Pyramid Pattern

The reversed version of pyramid pattern is called inverted pyramid pattern. The following arrangement will show the structure of the inverted pyramid pattern.

This pattern follows the condition from row value to base-row.

`size=4base=size*2-1for x in range(size): for y in range(base): if (y>=x and y<(base-x)): print("*",end="") else: print(" ",end="")  print()`

Output

`******* *****  ***  *`

## Diamond Pattern

Diamond pattern combines the normal pyramid and inverted pyramid patterns together. The following diagram describes the arrangement of diamond pattern.

`size=4base=size*2-1for x in range(size-1): for y in range(base): if (y>(size-2-x) and y<(base-(size-x)+1)): print("*",end="") else: print(" ",end="")  print()for x in range(size): for y in range(base): if (y>=x and y<(base-x)): print("*",end="") else: print(" ",end="")  print()`

Output

` *  ***  ***** ******* *****  ***  *`

## Hollow Diamond Pattern

The diamond pattern which contains the value at its edges only called hollow diamond pattern.

`size=5n=size*2-1temp=n//2+1for x in range(1,n+1): for y in range(1,n+1): if(y==temp or y==n-temp+1): print("*",end="") else: print(" ",end="") if(x<n/2): temp=temp-1 else: temp=temp+1 print()`

Output

` *  * *  * *  * * * * * *  * *  * *  *`

## Heart Pattern

Heart pattern can be divided into two parts. The upper part and lower part will have different codes.

`size=5for x in range(size//2,size+1,2): for y in range(1,size-x,2): print(" ",end="") for y in range(1,x+1): print(" ",end="") for y in range(1,size-x,1): print(" ",end="") for y in range(1,x+1): print("*",end="") print()for x in range(size,0,-1): for y in range(x,size): print(" ",end="") for y in range(1,(x*2)): print("*",end="") print()`

Output

` ** ******************* ******* ***** *** *`