Hollow Diamond Pattern in Python using for loop of Stars
Hollow diamond pattern in python using for loop of stars. In this article, you will learn how to print the hollow diamond pattern in python using for loop of the stars.
You should have knowledge of the following topics in python programming to understand this program:
- Python
defkeyword - Python
passkeyword - Python
forloop statement - Python
Ifcondition statement - Python
range()function - Python
endkeyword - Python
print()function
Source Code
# Hollow Diamond Pattern in Python using for loop of Stars
def hollowDiamond(size):
pass
i, j, diff = 0, 0, 0
diff = int (size / 2)
print ("\n-----The hollow diamond pattern is-----\n")
# This will print the first half diamond
for i in range (1, diff + 1):
print (end="\t")
for j in range (1, (diff - i) + 1):
print (end=" ")
if i == 1:
print (end="*")
else:
print (end="*")
for j in range (1, (2 *i - 3) + 1):
print (end=" ")
print (end="*")
print (end="\n")
# This will print the last half diamond
for i in range (diff + 1, size):
print (end="\t")
for j in range (1, (i - diff) + 1):
print (end=" ")
if i == size - 1:
print (end="*")
else:
print (end="*")
for j in range (1, (2 *(size - i) - 3) + 1):
print (end=" ")
print (end="*")
print (end="\n")
print ("-----Enter the size of the hollow diamond, it should be even-----")
x = int (input ())
# x - the size of the hollow diamond
# Size of the hollow diamond should be even number
if x % 2 == 1:
x = x + 1
# This will print the hollow diamond pattern
hollowDiamond(x)
Output
-----Enter the size of the hollow diamond, it should be even-----
15
-----The hollow diamond pattern is-----
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Explanation
In this given program, we have taken input 15 size of the pattern from the user then this value passed into hollowDiamond() the function then returned the hollow diamond pattern.