Python Program to print the diamond shape

PythonServer Side ProgrammingProgramming

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:

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

raja
Published on 30-Dec-2019 10:41:31
Advertisements