- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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:
@ @@@ @@@@@ @@@@@@@ @@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@@ @@@@@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@ @@@@@@@ @@@@@ @@@ @
Advertisements