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.

2 Dimensional Plane Arrangement

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.

Rectangular Pattern Arrangement for Height of 4 and Length of 6

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

123456
123456
123456
123456

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.

Hollow Rectangular Pattern Arrangement for Height of 4 and Length of 6

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()
Photo by Jeremy Bishop on Unsplash

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.

Half Pyramid Pattern Arrangement for Size of 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

1
12
123
1234
12345
123456

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.

Hollow Half Pyramid Pattern Arrangement for Size of 6

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.

Inverted Half Pyramid Pattern Arrangement for Size of 6
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.

Pyramid Pattern Arrangement for Size of 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=4
base=size*2-1-1
for 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.

Inverted Pyramid Pattern Arrangement for Size of 4

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

size=4
base=size*2-1
for 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.

Diamond Pattern Arrangement for Size of 5
size=4
base=size*2-1
for 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

 * 
***
*****
*******
*****
***
*
Photo by Khánh Trung Lê on Unsplash

Hollow Diamond Pattern

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

Hollow Diamond Pattern Arrangement for Size of 5
size=5
n=size*2-1
temp=n//2+1
for 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.

Photo by Kelly Sikkema on Unsplash
Heart Pattern Arrangement for size of 5
size=5
for 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

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