In this post, I am going to write a python script to print Diamond Star Pattern. I am writing this program in Python 3 version.
And, It’s a python console program to print a diamond star pattern. So let’s see.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
print ("Enter the limit") rows = int(input()) #top half k = 0 for i in range(1, rows + 1): # print spaces for j in range (1, (rows - i) + 1): print(end = " ") # let's print stars while k != (2 * i - 1): print("*", end = "") k = k + 1 k = 0 # add a line break print() #bottom half k = 2 m = 1 for i in range(1, rows): # print spaces for j in range (1, k): print(end = " ") k = k + 1 # print star while m <= (2 * (rows - i) - 1): print("*", end = "") m = m + 1 m = 1 #add a line break print() |
Read the inline comments to understand the program.
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter the limit 6 * *** ***** ******* ********* *********** ********* ******* ***** *** * |
Enjoy learning!
Can you please tell how to do the same using only 2 for loops in python?