How to Print 4 Different Diamond Pattern in Python

We think printing diamond pattern is a herculean task. In fact, it is a piece of cake. This article will help you in learning to print diamond pattern in python. This article deals with four different patterns of a diamond. Now let’s see the algorithm for those and the code for the same.

We will try printing using symbol ‘ * ‘. Use can also use any symbol.

Full Diamond Pattern In Python

Difficulty level: Easy

To print a full diamond pattern in python first we need to know the height of the diamond. This is basically the input from the user and is assumed to be ‘n’. Based on the value of n we will print the pattern in 2n rows.

Point to be noted: Always remember the value of n should be odd because it is not possible to create a diamond pattern with even numbers.

Algorithm

  • Getting the input from the user as ‘n’.
  • We need to find the modulo of the user input value (n) by 3.
  • Finding the number of stars and whitespaces.
  • Print the stars.

Algorithm is based on the below code.

Code

//pgm for diamond pattern
print("Give the value of n(odd):")
n=int(input())        //input from the user is stored in n
x=n//2+1
//here two loops are combined into one by 'zip'
//loop for printing the first half diamond              
for i,j in zip(range(1,x+1),range(1,n+1,2)):   
    print(' '*(x-i)+'*'*(j))
//loop for printing the second half diamond(reverse half)
for i,j in zip(range(1,x+1),range(n-2,-1,-2)):
    print(' '*(i)+'*'*(j))

Code Explanation

  1. The value of n is the height of the diamond and we store it in a variable ‘n’.
  2. We find n//3 and store it in x.
  3. The number of whitespaces is x+1 and the number of stars is n+1.
  4. We use two for loops. The reason behind this is diamond has an enlarging pattern at first and a shrinking pattern at the bottom. The first loop prints the first half of the diamond and the second half prints the inverse half.
  5. Run a nested for loop using variables i and j till the ranges x+1 and n+1 respectively.
  6. Print the stars till x-i and j values.
  7. The first half is printed.
  8. Again run a nested for loop using variables i and j till ranges x+1 and -1.
  9. Print the stars till x and j values.
  10. Inverse half is printed.

Output

First loop
Second loop
diamond pattern in python
Output with n=9

Run The code here

Half Diamond Pattern In Python

Difficulty level: Easy

Printing a half diamond is much easier than a full diamond. Here the height of the diamond has no restriction, unlike the full diamond pattern.

Algorithm

  • Finding the height of half diamond
  • Finding the stars
  • Printing the stars

Code

//program for half diamond
print("Give the value of n:")
    //user input
n=int(input());
//loop for printing the first half
for i in range(n): 
   //loop for checking the row number
    for j in range(0, i + 1): 
   //loop for checking the column 
            print("*", end = "") 
    print() 
 //loop for printing the second half 
for i in range(1, n): 
    for j in range(i, n): 
            print("*", end = "")
    print() 
  

Code Explanation

  1. First, we get the input from the user and store it in a variable ‘n’.
  2. Run a for loop till the range n. This loop keeps track of the rows.
  3. Again run a nested for loop till the range i+1. This loop keeps track of columns.
  4. Print the star and whitespace at the end.
  5. Print a new line outside the inner for loop. This is for printing the pattern in the next consecutive lines.
  6. The first half is printed.
  7. Again run two for loops with the range as n for both.
  8. Now, the inverse half of the diamond is printed.

Output

first loop output
second loop output
diamond pattern in python
Output with n=6

View at: Check code here

Hallow Diamond Pattern In Python

Difficulty level: Easy

Hallow half diamond is a half diamond pattern that has stars only on the border. It is empty/hollow inside. It is similar to a half diamond.

Algorithm

The approach to this problem is the same as that of the half diamond – splitting the diamond. The only difference is that when we print the star, we check for a certain condition and print.

Note : Read the code explanation part to understand how the condition works.

Code

//program for hallow half diamond
print("Give the height of diamond:")
n=int(input())
//loop for printing the first half
//loop for checking row 
for i in range(1,n+1):
//loop for checking column
    for j in range(1,i+1):
 //condition for printing stars on the border
        if i==j or j==1:
            print("*",end=" ")
        else:
            print(" ",end=" ")
    print()
//loop for printing the second half
for i in range(n-1,0,-1):
    for j in range(1,i+1):
        if i==j or j==1:
            print("*",end=" ")
        else:
            print(" ",end=" ")
    print()

Code Explanation

  1. The first step is, as usual, getting the input from the user.
  2. Store it in a variable ‘n’.
  3. Run a for loop till the range n+1. This keeps track of rows.
  4. Now run a nested for loop again till the range i+1. This keeps track of columns.
  5. Let’s check the condition for printing. Now when j==1 it prints a star at the top, If this is left we would miss a star at the top.
    • The condition i==j checks and prints the star only when both values match and this makes sure the star is printed at the borders only.
  6. If this condition is not met, we just print a blank space.
  7. Now we print a blank line to make the cursor come to the next line to print consecutively.
  8. The first half hallow diamond is printed.
  9. Again we run two nested for loops.
    • First for loop with starting value as n-1 and till the value -1.
    • Second for loop till the range i+1
  10. The condition is the same again(as seen above).
  11. The inverse half hallow pattern is printed.

Output

diamond pattern in python
Output with n=5

View at: Check code here

Hallow Diamond Pattern In Python

Difficulty level: Hard

Hallow diamond is a full diamond that is hollow inside. It is similar to a full diamond. Now we will see how to print this pattern.

Algorithm

We will use a different approach to this. Since diamond is a rhombus, we use the equation of rhombus to solve this problem. The equation is |X/a|+|Y/b|=1 . Here the input value acts as sides of the rhombus.

Points to be noted:

  • This pattern works only for even numbers
  • Read the code explanation section to know how the code works.

Code

//pgm to print hallow diamond
print("Give the diamond height:")
n=int(input())
//finding the distance of the rhombus
a=n//2
//loop fo the vertical side
for x in range(-a,a+1):
//loop for the horizontal side
    for y in range(-a,a+1):
//condition for the rombus 
        if(abs(x/a)+ abs(y/a) ==1):
            print("*", end='')
        else:
            print(" ",end='')
    print('')

Code Explanation

The approach is we divide the diamond vertically and horizontally and check for the equation condition and then print the rhombus/hallow diamond.

  1. We get the input from the user first. (preferably even)
  2. Since we need to find the distance between the extreme points of the rhombus we divide the number n by 2. From this, we get the distance and we call it ‘a’.
  3. Now we run a for loop from -a till a+1 (-a is the left extreme end of rhombus and it is always situated at negative co-ordinate of the x-axis and a+1 is the positive co-ordinate)
  4. Now we run a nested for loop from -a till a+1. This loop is for the horizontal part. (-a is in bottom negative one and a+1 is in top positive co-ordinate)
  5. We check for the condition |X/a| + |Y/b| ==1(this condition satisfies the rhombus properties).
    • As we have a modulo operator we use the function ‘abs’. This function gives us the absolute value.
    • If the condition is satisfied, we print “*”.
    • Else we print a blank space.
  6. Now the whole hallow diamond is printed.

Output

diamond pattern in python
Output with n=10

View at: Check Code here

Conclusion

Keep in mind that this is one of the approaches for dealing with these problems. There are several ways to print the diamond pattern in python. Each one has its own approach. Hope we helped you to understand the approaches in a simpler way. Try printing these patterns using number sequences for better coding knowledge. Keep practicing and happy coding!

Related articles:

Leave a Comment

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