Skip to main content
    Learn More


      How to Code a Heart Using Python’s Turtle Library

      By The Fullstack Academy Team

      FSA Code a Heart in Python LI

      Love encompasses a wide range of emotions, from the enduring bonds of friendship to the tender intimacy of romantic relationships, and the fiery commitment to one's interests. As we immerse ourselves in the season of love, what better way to celebrate Valentine's Day than by embarking on a journey of exploration and self-discovery? Expressing affection through the language of code offers a unique opportunity to merge creativity with technology and find your passion.

      Dive into this coding exercise, and allow your heart to lead the way toward new discoveries and fulfillment. Through practice, you'll not only learn how to code a heart using the Turtle module in Python but also gain a glimpse into the fascinating world of coding. As demand for skilled coders continues to grow, participating in coding exercises and bootcamps can help equip you with the tools and knowledge needed to thrive in this dynamic and exciting field. So, seize the opportunity to indulge your curiosity, nurture your passions, and embrace the endless possibilities that coding has to offer.

      What is Python?

      JavaScript, Python, Ruby, Go, and C stand out as some of the most popular programming languages in the tech industry, each with its unique features and applications. However, among these options, Python shines as one of the most accessible languages for beginners due to its clear, intuitive syntax that closely resembles English.

      Python's popularity stems not only from its simplicity but also from its versatility and readability. People who are new to programming may wonder, “Is it difficult to learn Python?”. It presents its structure in a simple manner like natural language, making it easier for beginners to understand and write code effectively. This characteristic serves as a gateway for aspiring programmers, smoothing the learning curve and alleviating some of the initial challenges often associated with coding.

      What is Python mainly used for? Among its many uses, Python is often used to develop websites and software, automate tasks, perform data analysis, and create visualizations. Whether you're a novice seeking to learn your first programming language or an experienced developer looking for a versatile tool, Python's accessibility and user-friendly nature make it an invaluable asset in any programmer's toolkit.

      What is Python's Turtle Library?

      The Turtle library of Python allows users to draw and create simple graphics. As the "turtle" moves around the canvas, it leaves a trail of lines and shapes. Generally, turtle graphics are used to teach beginners programming because of their simplicity and visual feedback. Turtle also offers some advanced features for customization and image manipulation. Beyond education, it's used in game development and prototyping. While useful for certain tasks, Turtle isn't a general-purpose programming tool and has limitations for complex graphics or large-scale projects.

      How to Code a Heart Using Python's Turtle Library - Getting Started

      Before we unleash our artistic coding skills, make sure you have Python installed on your computer. Python is an open-source language freely available for download. While older versions included the Turtle library, recent versions require an additional installation step. Just open a terminal or command prompt and type pip install turtle to bring it on board. Keep in mind that other libraries like Pygame offer more advanced graphical capabilities if you're feeling adventurous later on.

      Step 1: Setup Your Shell

      After installing Python and Turtle, you are ready to go! Now, let's get started by opening your favorite text editor or Python IDLE. Open a blank Python interpreter window, also known as a “shell,” by following these steps:

      • Double-click the IDLE icon in your Python download folder on your computer to open the shell. In Python, the Integrated Development Environment (IDLE) is the standard development environment. Programmers who work with Python may use other development environments and code editors, but for simplicity this exercise uses IDLE.

      • With IDLE open, save your shell by selecting “File,” then “Save As…” from the top navigation window.

      • Name your Python shell file “heart” and save the file to your preferred location on your computer.

      When your Python application installation and shell file setup are complete, you can start building your heart on the virtual canvas.


      Step 2: Import Turtle Python Library

      To access Turtle in the Python library, simply type the following into the shell and press enter on your keyboard.

      import turtle

      Pressing enter submits the characters to the machine. In Python, “import” is a command that brings a module into your code.

      Step 3: Opening the Turtle Screen

      s = turtle.Screen()

      This will open the “Python Turtle Graphic” window canvas. The screen provides access to some extra helpful methods that you will use in future steps.


      Step 4: Change Your Canvas Background Color

      You can change the background color of your turtle virtual canvas to black by entering the following command in the shell:


      You’ll notice that this time when you called the method s.bgcolor, you included some information inside the parentheses. The information you give to a method when you call it is called an argument. Some methods take arguments, while others don’t. In this case, you passed the argument 'black' into the method to tell your machine what color the background of your canvas should be.

      After typing in the code, press enter and your turtle virtual canvas background color will change to match the color you indicated inside of the command.

      The value 'black' is a text string. In programming, a text string is a group of characters, usually inside quotation marks. We’ve suggested black for the background to make the color of your heart appear more vibrant. You can also experiment with other colors by running the same line of code again but replacing 'black' with a different text string. The s.bgcolor method will accept any valid turtle color name, which you can find using this colors resource.

      Step 5: Setup Your Pen

      Program the “pen" you'll be using to draft your heart with the following code:

      pen = turtle.Turtle()

      As always, press enter after each line of code to submit the command.

      With this code, you created the pen and assigned it to a variable. Then you set the speed at which the pen will draw and the color of the pen.


      You can input different numbers for the drawing speed. You can also change the color of the line the pen will produce.

      How to Code a Heart Using Python's Turtle Library - Drawing the Heart Shape

      To code a heart shape using Python's Turtle library, we'll use a series of turtle movements to trace out the outline of the heart. Here are the steps to code a heart shape in Python’s Turtle.

      Step 1: Set Your Fill Color

      First, you’ll set the fill color of your heart using the following code:


      Press enter to submit each command.


      While you won’t notice any visible change on the Turtle Graphics screen, the fill color for your heart will be set so that when you execute your drawing, the inside of the shape will be red.

      Step 2: Direct Your Pen

      Next up, you’ll give your pen instructions on how to code the heart shape using code that directs the movement of the pen. For the first half of the heart, use the following code to direct the pen:

      pen.forward(180), 200)

      After that, you’ll use this code to complete the heart outline:, 200)

      Step 3: Fill Your Heart

      Remember that red shade you coded earlier? Now's the perfect time to bring it to life and fill your heart with color!

      This code snippet acts like a magic wand, transforming the line you’ve drawn into a vibrant heart. See how it works by running the following code:


      Step 4: "Hide" the Cursor

      Next, you’ll “hide” the cursor on the Turtle Graphic screen using the following code and display your results:

      The finished code should be something like this:

      import turtle
      s = turtle.Screen()
      pen = turtle.Turtle()
      pen.forward(180), 200)
      pen.setheading(60), 200)

      The final image should look like this:


      Coding shapes like a heart using Python's Turtle library is a fun and engaging way to learn programming concepts. With just a few lines of code, we were able to create a simple yet visually appealing drawing. As you embark on this coding adventure, let your creativity bloom like a bouquet of Valentine's Day roses. Feel free to explore different colors and personalize the code to reflect the warmth of your heart. After all, it's the playful experimentation that makes programming as enchanting as a romantic rendezvous!

      If you've fallen head over heels for this exercise and are eager to dive deeper into the captivating world of coding, the online Fullstack Academy Software Engineering Immersive Bootcamp can be your guide. Our beginner-friendly coding bootcamps are JavaScript-based and provide both a foundational introduction to the programming language and education on advanced concepts. You’ll also learn other languages like HTML and CSS, as well as get hands-on experience in programming and build a portfolio.

      Want to work with Python more? Fullstack Academy’s Data Analytics Bootcamp teaches you the skills you need to become a data analyst, including Python, data cleaning, manipulation, analysis, and visualization.