Hollow Diamond Pattern in Python
In this article, you will learn how to print the Hollow Diamond Pattern using the Python programming language. Such a type of logical question is generally asked in programming interviews or on a competitive exams. This can be helpful to improve your logical programming skills and is useful in game development and other logical application development.
The following Python program prints a Hollow Diamond Pattern made up of stars up to the specified n lines. In this Python program, we take the row number as input from the client. Here, the row demonstrates the number of lines that will be printed in one triangle shape of the Hollow Diamond design. If we have a row value of 7 as input, the total number of lines in the hollow diamond pattern will be 13.
Hollow Diamond Pattern Python Program using for loop and while loop
The given Python program uses nested for loops, while loops and an if-else statements to return the hollow diamond pattern.
# Hollow diamond pattern program
def hollowDiamondPattern(n) :
k = 0;
# Upper triangle shape
for i in range(1,n+1) :
# Print spaces
for j in range(1,n-i+1) :
print(" ",end="")
# Print *
while (k != (2 * i - 1)) :
if (k == 0 or k == 2 * i - 2) :
print("*",end="")
else :
print(" ",end="")
k = k + 1
k = 0
# move to next row
print(""),
n = n - 1
# Lower triangle shape
for m in range (n,0,-1) :
# Print spaces
for p in range(0,n-m+1) :
print(" ",end="")
# Print *
k = 0
while (k != (2 * m - 1)) :
if (k == 0 or k == 2 * m - 2) :
print("*",end="")
else :
print(" ",end="")
k = k + 1
print(""),
# Input number of rows
row = int(input('Enter number of row: '))
hollowDiamondPattern(row)
We will get the following output of the above code-
Hollow Diamond Pattern Python Program using only for loop
The given Python program uses nested for loops, and if-else statement to return the hollow diamond pattern.
# Hollow diamond pattern program
rows = int(input("Enter number of rows : "))
print("Hollow Diamond Pattern")
# Upper triangle shape
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
# print spaces
print(end = ' ')
for k in range(1, 2 * i):
if k == 1 or k == i * 2 - 1:
# print spaces
print('*', end = '')
else:
# move to next row
print(' ', end = '')
print()
# Lower triangle shape
for i in range(rows - 1, 0, -1):
for j in range(1, rows - i + 1):
# print spaces
print(' ', end = '')
for k in range(1, 2 * i):
if k == 1 or k == i * 2 - 1:
# print spaces
print('*', end = '')
else:
# move to next row
print(' ', end = '')
print()
We will get the following output of the above code-
Related Articles
Pascal triangle pattern in Python
Alphabet pattern programs in Python
Reverse pyramid pattern in Python
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
Bouncing ball game code in Python
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml