Python Program to print the diamond shape


The looping features in python can be used to create many nicely formatted diagrams using various characters from the keyboard. One such shape is diamond shape which will involve multiple loops. This is because we have to print the character both vertically and horizontally. Also we have to take care of the shape gradually growing from top till middle and then gradually shrinking from middle till the bottom. For this reason, we will use two for loops each containing one more for loop inside it.

Below is the code for creating the diamond shape.

Example

def Shape_of_Diamond(shape):
a = 0
for m in range(1, shape + 1):

for n in range(1, (shape - m) + 1):
print(end=" ")

while a != (2 * m - 1):
print("@", end="")
a = a + 1
a = 0

print()

s = 1
c = 1
for m in range(1, shape):

for n in range(1, s + 1):
print(end=" ")
s = s + 1

while c <= (2 * (shape - m) - 1):
print("@", end="")
c = c + 1
c= 1
print()

shape = 8
Shape_of_Diamond(shape)

Running the above code gives us the following result:

           @
          @@@
         @@@@@
        @@@@@@@
       @@@@@@@@@  
      @@@@@@@@@@@
     @@@@@@@@@@@@@
    @@@@@@@@@@@@@@@
     @@@@@@@@@@@@
     @@@@@@@@@@@
      @@@@@@@@@
       @@@@@@@
       @@@@@
        @@@
         @

Updated on: 30-Dec-2019

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements