Python Heart Shape Star Pattern Program

By | March 18, 2020

Python Heart Shape Star Pattern Program

This Python pattern printing program uses the logic of the following figure. We can see that how the stars are present in certain positions. We will create some conditions on the basis of star posittion in each row.

For example we consider the first row which is zero row. Now zero row has no stars (but space) on position 0,3 and 6. Hence we will create a condition if row==0 and col%3 !=0.

0%3, 3%3 and 6%3 all give 0 as remainder and so a space will be printed at position 0, 3 and 6. A star will be printed otherwise.

Similarly, other conditions are made for remaining row of the heart shape.

Python Heart Shape Star Pattern Program

Python Heart Shape Star Pattern Program

# Write a Python program
# using nested for loops to print the
# heart shape using stars pattern.

#   **   **
#  *   *   *
#  *       *
#   *     *
#    *   *
#      *


# 
# Perfect Python Programming Tutorials
# Author : www.EasyCodebook.com (c)



# Actual Program starts here 
# Python Program - Heart Shape Star Pattern Program No. 1

n = 6		
for row in range(0,n):
    for col in range(0,n+1):
        if(row==0 and col%3!=0) or (row==1 and col%3==0) or (row-col==2) or (row+col==8):
            print('*',end='')
        else:
            print(' ',end='')
              
    print()

Output

 ** ** 
*  *  *
*     *
 *   * 
  * *  
   *   

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *