Write a Python Program to Print Half Diamond Star Pattern using for loop. The first for loop (for i in range(rows)) iterate from 0 to rows, and for j in range(0, i + 1) iterate from 0 to i value and print stars.
# Python Program to Print Half Diamond Star Pattern rows = int(input("Enter Half Diamond Pattern Rows = ")) print("Half Diamond Star Pattern") for i in range(rows): for j in range(0, i + 1): print('*', end = '') print() for i in range(1, rows): for j in range(i, rows): print('*', end = '') print()
This Python Program uses a while loop to print the Half Diamond Star Pattern.
# Python Program to Print Half Diamond Star Pattern rows = int(input("Enter Half Diamond Pattern Rows = ")) print("Half Diamond Pattern") i = 0 while(i <= rows): j = 0 while(j < i): print('*', end = '') j = j + 1 i = i + 1 print() i = 1 while(i < rows): j = i; while(j < rows): print('*', end = '') j = j + 1 i = i + 1 print()
Python Half Diamond star pattern using a while loop output
Enter Half Diamond Pattern Rows = 9
Half Diamond Pattern
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
In this Python example, we created a halfDiamondPattern function to Print the Half Diamond Pattern. It accepts the symbol to replace the star in the half diamond.
# Python Program to Print Half Diamond Star Pattern def halfDiamondPattern(rows, ch): for i in range(rows): for j in range(0, i + 1): print('%c' %ch, end = '') print() for i in range(1, rows): for j in range(i, rows): print('%c' %ch, end = '') print() rows = int(input("Enter Half Diamond Pattern Rows = ")) ch = input("Symbol to use in Half Diamond Pattern = " ) print("Half Diamond Pattern") halfDiamondPattern(rows, ch)
Enter Half Diamond Pattern Rows = 12
Symbol to use in Half Diamond Pattern = #
Half Diamond Pattern
#
##
###
####
#####
######
#######
########
#########
##########
###########
############
###########
##########
#########
########
#######
######
#####
####
###
##
#