Как сделать сердечко на питоне
Перейти к содержимому

Как сделать сердечко на питоне

  • автор:

Draw Heart with Python using Turtle

Ayushi Rawat

In this Blog article, we will learn how to Create a heart with Turtle, we will implement it in Python.

Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video tutorial for better Understanding:

What will be covered in this Blog:

What is Turtle?

Turtle is a pre-installed Python library. It that enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called as turtle.

The turtle has three attributes: a location, an orientation (or direction), and a pen.

Moving the Turtle Head

The turtle can move in four directions:

  • Forward
  • Backward
  • Left
  • Right

If you wish to know more about it, you can refer to Turtle Documentation. Use this link to navigate to the documentation.

Now that you are aware of Turtle basics, we can move forward to the coding section.

Time to code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

In order to access the Python library, you need to import it into your Python environment, use the following command to import turtle it in your python script.

Now let’s define some properties,

  • Let’s set the speed as 3 using speed method, that means the heart will not just appear on the screen, the drawing will have some animation.
  • If you wish to change the background color, you can use the bgcolor method, by default it is white.
  • You can adjust the pen’s thickness using pensize method, it will be slightly bold.

Now let’s define a function to define the curve, I am calling it as myfunc .

I will define a loop here for the first curve, we will set the direction right and move forward. Now let’s set the turtle color using the color method

  • the first parameter will be the border color, the pen color, which we are passing as black
  • the second parameter, red is the color used to fill inside our heart.

And once we have set the color, we can begin the fill using begin_fill method.

we call left and forward and finally call our function to get the first curve. It will look something like this.

Draw A Heart Using Python Turtle With Code

If you’re searching for a guide on drawing a heart using Python, you’ve come to the right place. This tutorial will show you how to draw a heart shape utilizing Python turtle. So follow this tutorial till the end.

To draw heart in python we will use turtle which is popular graphics library which can be used to draw shapes and designs in python.

So before moving forward you need to know the basic of python and turtle if you want to know how it actually draws a heart or if you don’t care about how it works just want to show it to your loved one then you are good to go.

Python Code To Draw A Heart

Above is the python program to draw a heart. Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer.

Now you have the code you can run it but if you get any errors like turtle module not found then you need to install the turtle module mostly it comes preinstalled with python but if it doesn’t then install it by using the below command.

Heart Drawing In Python

So now you have everything setup and you are ready to run the program, After running you will see it opened a new window and started drawing the heart and below is the final drawing you will get.

As you can see, we successfully drew a heart using python turtle. I hope you were able to run this program successfully. You can also use this online python compiler to run this program.

Want to propose your boyfriend/girlfriend using a python program then follow this tutorial: I love you python program. If you have proposed your boyfriend/girlfriend and the proposal breaks your heart then we also have a python program for it: Broken heart using python.

I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program. If you want more python guides and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day ��


report this ad

how to draw a heart with pylab

How to draw a heart with pylab? I searched with google for ways to draw the picture but i want know how to draw it with pylab. Can someone help? The picture should look like this:


3D Heart

3 Answers 3

Using the linked formula in the other solution:


You can see here, how can you plot a 3D hearth.

The author of the article have put together the implicit function plotting can be found here and the implicit function of the hearth, and got the code below:

I have changed the python to python3 in the first row. If you use Python 2 you need to set it back.

enter image description here

Hint: Take a look at example from Sage: 3D Love Heart:

sage: Heart 3D

    The Overflow Blog
Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.6.15.43494

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Name already in use

Work fast with our official CLI. Learn more about the CLI.

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats


Failed to load latest commit information.


Drawing a heart in Python

Hi fellow coders!

This project was created to have some fun with Python during my Sundays. In this project I drawn a heart in Python using turtle framework.

Before to dive into the description of the project I’ll leave here a brief description about turtle framework. If you’re not interested about it or you have some experience with this framework you can jump to the Project Overview section. Thanks ��

Table of contents

Accordingly to Python’s documentation :

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Indeed, thanks to Turtle Graphics and obviously to Turtle module, programmers can create beautiful shapes ny simply using its methods. It’s a very fascinating way to approach to Python and to computer programming in general.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

The turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5. It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it.

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

The object-oriented interface uses essentially two+two classes:

  1. The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application. The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible. All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.
  2. RawTurtle (alias: RawPen) defines Turtle objects which draw on a TurtleScreen. Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw. Derived from RawTurtle is the subclass Turtle (alias: Pen), which draws on “the” Screen instance which is automatically created, if not already present. All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-oriented interface.

The procedural interface provides functions which are derived from the methods of the classes Screen and Turtle. They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An (unnamed) turtle object is automatically created whenever any of the functions derived from a Turtle method is called.

To use multiple turtles on a screen one has to use the object-oriented interface.

Drawing using Turtle

To make use of the turtle methods and functionalities, we need to import turtle.”turtle” comes packed with the standard Python package and need not be installed externally. The roadmap for executing a turtle program follows 4 steps:

  1. Import the turtle module
  2. Create a turtle to control.
  3. Draw around using the turtle methods.
  4. Run turtle.done().

So as stated above, before we can use turtle, we need to import it. We import it as :

After importing the turtle library and making all the turtle functionalities available to us, we need to create a new drawing board (window) and a turtle. Let’s call the window as window and the turtle as my_turtle (because we have a lot of fantasy). So we code as:

Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the direction my_turtle is facing, we code:

We have moved my_turtle 100 pixels forward, Awesome! Now we complete the program with the done() function and We’re done!

So, we have created a program that draws a line 100 pixels long. We can draw various shapes and fill different colors using turtle methods. There’s plethora of functions and programs to be coded using the turtle library in python.

The project is very easy and very fun to do on your own. It allows you to use a lot of methods from Turtle framwork. There is just one functions that I wrote to effectivly draw the heart on the screen. For the rest, they are all within the framework.

In pratice: the project draw a heart on a black screen and coloring in pink the heart itself.

The reasoning behind the project is very basic. Since the heart is a simmetric shape I’ve decided to split the code in two parts each one divided by the draw() function. In this way I can do the same exact thing, that I’ve done on the left, on the right.

The code inside the project

After importing the turtle framework

I’ve started to give some informations about the speed of drawing, the background color and the size of the mark on the board

Then I created my loop to give instructions to the turtle pen about where to go and embedded all in function called draw()

In the end I set the color of the pen and the color of the shape

After all of this I’ve just put together all the turtle’s methods that suited best for my homework

Note: As you can notice the draw() function is written two times, this because the heart that I drawn is a symmetric shape and you can do the same thing on both side, the left side and the right side.

Thanks for reading. �� �� ��

If you want to follow me on my other socials I’ll appreciate it.

  • Medium
  • Linkedin
  • Instagram
  • Facebook


This project is intended to draw a heart in Python using the turtle framework

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *