My Bookmarks

  • No bookmark found

Most Active Members

Printing Heart Pattern in Python Using List Comprehension3 min read

Visualising the Pattern

The pattern we will be making can be visualized using a grid(or a 2D-Matrix).

Here, a grid with 7 rows and 6 columns is formed. The index of each row is mentioned at the top whereas the index of each column is mentioned at the left.

0123456
0****
1***
2**
3**
4**
5*

There exists multiple ways to make this pattern. But, we will make this pattern by breaking down it into 4 parts:-

The 1st part and 2nd part consist of zeroth and 1st column respectively.

The 3rd part represent the area from row 0 to row 3 and from column 1 onwards.

The 4th part represent the area from row 3 to row 6 and from column 1 onwards.

Note:-If you want, you can remove row 2.This will also result in a similar heart pattern.

Writing Code for different parts of Pattern

In Row 0,the ‘*’ is printed in column 1,column 2,column 4 and column 5.
These are not the multiples of 3.

So, the code for the 1st part will be:-

if(row==0 and col%3!=0):  #Here col represents column
    print('*')

The condition for Row 1 is opposite of the Row 0. In it, only column 0,3 and 6 are filled with ‘*’.

So, the code for the 2nd part will be:-

if row ==1 and col%3==0:
    print('*')

In the third part the marked position are:-
Row 2 and Column 0,
Row 3 and Column 1,
Row 4 and Column 2 and
Row 5 and Column 3.

It can be observed that the ‘*’ is available at those positions in part 3 where the difference of row and column is 2.

So, the code for the 3rd part will be:-

if row-col==2:
    print('*')

In the fourth part the marked position are:-
Row 2 and Column 6,
Row 3 and Column 5,
Row 4 and Column 4 and
Row 5 and Column 3.
Note:-Here Row 5 and column 3 is available in both parts, i.e., part 3 and part 4.

The ‘*’ is printed where the sum of row no. and column no. is equal to 8.

The code for the 4th part will be:-

if row+col==8:
    print('*')

**Where star is not printed, a space is given through else condition.

So, the whole code for this would be:-

Implementation in Python:-

for row in range(6):#To iterate over 6 rows available
    for col in range(7):#Here we have 7 columns
        if row==0 and col%3!=0 or row==1 and col%3==0 or row-col==2 or row+col==8: #The condition discussed above.
#Here we join all 4 conditions using or
            print('*',end="")#Here end=" " is used to avoid python from going to new line.By giving a space after each character we will create original heart pattern.
        else:
            print(" ",end=" ")#To give empty space
    print()#After each over we will print a new line

Output:-

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

Using List Comprehension:-

We can make a whole 2d list using list comprehension.
List comprehension is a way to define or create a list and in a single line only.

The code for this list would be:-

heart=[["*" if row==0 and col%3!=0 or row==1 and col%3==0 or row-col==2 or row+col==8 else " " for col in range(7)] for row in range(6)]

The list created above can be printed by using one-liner for printing a 2d matrix. The code for this is:-

print('\n'.join(' '.join(*zip(*row)) for row in heart))
#It will print the same pattern as given above.

If we want, we can also join both one-liners to create a single one-liner.
This can be done by giving the one-liner for 2d matrix in place of variable ‘heart’.

One-Liner for Printing Heart Pattern

print('\n'.join(' '.join(*zip(*row)) for row in ([["*" if row==0 and col%3!=0 or row==1 and col%3==0 or row-col==2 or row+col==8 else " " for col in range(7)] for row in range(6)])))

If we want we can replace the ‘*’ character with the Unicode character for heart symbol, i.e., \u2665.

And our final output will be:-

  ♥  ♥   ♥  ♥  
♥      ♥      ♥
♥            ♥
  ♥         ♥  
    ♥     ♥    
       ♥      

If you want you can change the above code to make some new pattern, like to fill the space with the heart symbol or something like that.

Write your suggestions in Comments.

Bookmark(0)

Add a Comment

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