Explore

Pattern Programs

Print Diamond Pattern In Python

Print Diamond Pattern In Python

Want to know how to print Diamond pattern in python, then you are at the right place today in this tutorial. I will show you the python program to print Diamond pattern, so follow this tutorial till the end. 

Printing patterns in python is fun and very interesting. You must have created python programs to print letters and other shapes but today I will show you how to print a Diamond pattern in python.

I will show you how to print diamond pattern using for loop and while loop in python you can use any program you want, So first let’s see printing diamond pattern using for loop in python.

Python Program To Print Diamond Pattern Using For Loop


rows = int(input("Enter Diamond Pattern Rows: "))

k = 0

for i in range(1, rows + 1):
    for j in range(1, rows - i + 1):
        print(end = ' ')
    while k != 2 * i - 1:
        print('#', end = '')
        k = k + 1
    k = 0
    print()

k = 2
l = 1

for i in range(1, rows):
    for j in range(1, k):
        print(end = ' ')
    k = k + 1
    while l <= (2 * (rows - i) - 1):
        print('#', end = '')
        l = l + 1
    l = 1
    print()

Python Program To Print Diamond Pattern Using While Loop


rows = int(input("Enter Diamond Pattern Rows: "))

k = 0
i = 1

while(i < rows):
    for j in range(1, rows - i + 1):
        print(end = ' ')
    while k != 2 * i - 1:
        print('#', end = '')
        k = k + 1
    k = 0
    print()
    i = i + 1

k = 2
l = 1

i = 1

while(i < rows):
    for j in range(1, k):
        print(end = ' ')
    k = k + 1
    while l <= (2 * (rows - i) - 1):
        print('#', end = '')
        l = l + 1
    l = 1
    print()
    i = i + 1

Above are the python programs to print diamond patterns using for loop and while loop. Now to run this programs you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer.

To run this python program, follow the below steps:

  • Create a new folder for this python project.
  • Open it in a code editor of your choice.
  • Create a python file with an ending .py extension.
  • Copy the above code and paste it in your file.
  • Open a terminal or CMD at folder location
  • And type this command python filename.py (Name of your file).

After running this program you will see a diamond pattern printed like below output. The output would be same for both for and while loop programs.


Enter Diamond Pattern Rows: 5
    #
   ###
  #####
 #######
#########
 #######
  #####
   ###
    #

Don’t want to create all the files and folders then you can run this program now using this online python compiler it is fast and easy.

Summary

This was the python program to print diamond pattern. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.

Here are some more python drawing tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂