How to Print a Heart Pattern Using Python?


Introduction

In this article, we are going to focus on how to print a heart pattern using Python. We will look into the syntax of the function that is going to be used in the code. We will also learn about the method definition and the arguments that the method will take and their purpose.

As we all know python is an open-source versatile programming language that provides a huge number of modules and functionalities to accomplish our task. with Python's simplicity and readability, we can transform our computer screen into a canvas with just a few lines of code.

Learning how to print heart pattern not only enhance our programming skills but also unlock a gateway of endless possibilities.

This skill will help us in real-life applications such as in making visualisations and graphics. It will also help in enhancing our algorithmic thinking and problem-solving skills.

In this article, we will learn about the syntax of the function used and the approach to the problem. So, let us dive and discover how to print heart pattern using Python programming language.

Syntax

range(start, stop, step)

The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number.

Start − It is optional and takes an integer number specifying which position to start.

Stop − It is required and takes an integer specifying which position to stop.

Step − It is optional and takes an integer number specifying the incrementation, the default is 1.

Approach

This program takes an integer as the input value from the user which determines the size of the heart pattern. It divides the heart into two parts the upper and the lower parts based on the conditions it selects whether to print asterisks '*' or space ' '.

Here is the breakdown of the following code −

  • At first, the input is taken from the user to determine the size of the heart.

  • The user's input is then stored in the variable named "n".

  • The columns are determined by adding 1 to the size i.e. "n" and this value is stored in the variable "m".

  • The code uses two loops one for the upper part of the heart and one for the lower part.

  • In the upper part the loop iterates over the range of 0 to (n//2 -1) representing the row. The second loop iterates over the range of 0 to m, which represents the column.

  • Condition to print star in the upper line is checked when "i" is equal to (n//2 -2) and the value of j is either "0" or "m-1".

  • The condition for printing stars in the left upper part is checked if it satisfies the conditions i.e. "j" should be less than (m//2) and fulfilling either of these conditions

(i + j) equals (n // 2 - 3) and 'j' is less than or equal to (m // 4).
(j - i) equals (m // 2 - n // 2 + 3) and 'j' is greater than (m // 4).
  • For printing stars in the upper right part are checked if it satisfies the conditions i.e., "j" should be greater than (m//2) and fulfill either of these conditions

(i + j) equals (n // 2 - 3 + m // 2) and 'j' is less than (3 * m // 4).
(j - i) equals (m // 2 - n // 2 + 3 + m // 2) and 'j' is greater than or equal to (3 * m //	4)
  • If none of the conditions are being satisfied then the program will enter into the else part and print space " ".

  • To print the lower part the loop iterates over the range from (n//2-1) to 'n'.

  • If (i-j) equals (n//2-1) or (i+j) equals (n-1 +m//2) asterisks '*' will be printed else space  ' ' will be printed.

  • After printing the heart pattern the program terminates.

This code takes user input for the size and prints the heart pattern on the console based on the provided size.

Example

# Define the size of the heart pattern (n should be even)
def print_heart(size):
   n=size
   m=size+1
# Loop for the upper part of the heart
for i in range(n // 2 - 1):
   for j in range(m):
 
      # Condition for printing stars in the upper line
      if i == n // 2 - 2 and (j == 0 or j == m - 1):
      print("*", end=" ")
 
      # Condition for printing stars in the left upper part
      elif j <= m // 2 and ((i + j == n // 2 - 3 and j <= m // 4) \ or (j - i == m // 2 - n // 2 + 3 and j > m // 4)):
      print("*", end=" ")
 
      # Condition for printing stars in the right upper part
      elif j > m // 2 and ((i + j == n // 2 - 3 + m // 2 and j < 3 * m // 4) \ or (j - i == m // 2 - n // 2 + 3 + m //2 and j >= 3 * m // 4)):
      print("*", end=" ")
 
      # Condition for printing spaces
      else:
         print(" ", end=" ")
   print()
 
# Loop for the lower part of the heart
for i in range(n // 2 - 1, n):
   for j in range(m):
 
      # Condition for printing stars
      if (i - j == n // 2 - 1) or (i + j == n - 1 + m // 2):
         print('*', end=" ")
 
      # Condition for printing spaces
      else:
         print(' ', end=" ")
 
   print()
 
size = int(input("Enter the size of the heart pattern: "))
 
# Printing the heart pattern
print_heart(size)

Output

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

Conclusion

The code above is written using Python programming language which accepts the user's input (which is an integer value) and prints the heart pattern on the console screen.

The program uses loops and conditions to decide when to print asterisks '*' and when to print space ‘ ‘. The program divides the heart into upper and lower parts to form a complete heart pattern. We can also modify the size of the heart depending on the user's input.

Pattern printing can help us with data visualizations, and developing graphic user interfaces it is also helpful in game development and creating an artistic design.

Updated on: 04-Oct-2023

956 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements