Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Print a Heart Pattern Using Python?
Creating visual patterns with code is both educational and fun. In this article, we'll learn how to print a heart pattern using Python, which demonstrates loop control, conditional logic, and coordinate-based pattern generation.
Understanding the Range Function
The heart pattern uses nested loops with the range() function ?
range(start, stop, step)
Parameters:
- start Starting position (optional, default is 0)
- stop Ending position (required)
- step Increment value (optional, default is 1)
Heart Pattern Algorithm
The heart pattern is created by dividing it into two main parts:
- Upper part Forms the two rounded lobes
- Lower part Creates the pointed bottom
The algorithm uses mathematical conditions to determine when to print asterisks (*) or spaces based on row and column positions.
Simple Heart Pattern
Let's start with a basic heart pattern using a simpler approach ?
def simple_heart():
# Simple heart using predefined pattern
pattern = [
" ** ** ",
" **** **** ",
"***********",
" ********* ",
" ******* ",
" ***** ",
" *** ",
" * "
]
for line in pattern:
print(line)
simple_heart()
** **
**** ****
***********
*********
*******
*****
***
*
Mathematical Heart Pattern
Here's a more advanced version that calculates the heart shape mathematically ?
def print_heart(size):
n = size
m = size + 1
# Upper part of the heart
for i in range(n // 2 - 1):
for j in range(m):
# Top line condition
if i == n // 2 - 2 and (j == 0 or j == m - 1):
print("*", end=" ")
# 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=" ")
# 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=" ")
else:
print(" ", end=" ")
print()
# Lower part of the heart
for i in range(n // 2 - 1, n):
for j in range(m):
if (i - j == n // 2 - 1) or (i + j == n - 1 + m // 2):
print('*', end=" ")
else:
print(' ', end=" ")
print()
# Create heart pattern
size = 10
print(f"Heart pattern with size {size}:")
print_heart(size)
Heart pattern with size 10:
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
*
Filled Heart Pattern
For a filled heart pattern, we can use a different approach ?
def filled_heart():
# Create a filled heart pattern
heart_lines = [
" ?????? ?????? ",
" ???????????????????? ",
"????????????????????????",
"????????????????????????",
" ???????????????????? ",
" ???????????????? ",
" ???????????? ",
" ???????? ",
" ???? ",
" ?? "
]
for line in heart_lines:
print(line.center(30))
print("Filled Heart Pattern:")
filled_heart()
Filled Heart Pattern:
?????? ??????
????????????????????
????????????????????????
????????????????????????
????????????????????
????????????????
????????????
????????
????
??
Key Points
- Heart patterns use coordinate-based mathematical conditions
- The pattern is divided into upper (lobes) and lower (pointed) sections
- Nested loops control row and column positioning
- Conditional statements determine when to print characters or spaces
Conclusion
Creating heart patterns in Python combines mathematical thinking with creative programming. These exercises improve your understanding of loops, conditions, and coordinate systems while producing visually appealing results.
