Drawings with Python Turtle

Berivan Küçükkart
Yetkin Yayın
Published in
6 min readJun 3, 2022

How to draw a triangle, square, rectangle, star or new moon with Python Turtle?

Python Turtle, in its shortest definition, is a python module that helps us draw pictures. The reason why this module is called Turtle is based on a metaphor. Imagine you have a turtle that understands your language. Imagine this turtle moving and leaving a trail as commanded. That’s the metaphor behind the Python Turtle.

In this writing, we will learn how to draw basic figures with Python Turtle Module.

In order to use the Python Turtle Module, we use import turtle code, it allows us to use the turtles library. Then with the wn = turtle.Screen() we creates a graphics window. Then, we should create a turtle named whatever we want. The process up to this part is fixed.

import turtle #allows us to use turtle librarywn = turtle.Screen() #creates a graphics windowjohn = turtle.Turtle() #you can name your turtle anything.

Drawing a Rectangle

When drawing simple shapes with Turtle, we use the .forward(), .backward(), .left() and .right() commands.

We specify the distance we want the turtle to travel for the .forward() and .backward() commands, and how many degrees we want the turtle to turn to the specified direction for the .left() and .right() commands.

Let’s try to draw a simple rectangle together.
Let the long sides of this rectangle be 150 px and the short sides 75 px.

import turtlewn = turtle.Screen()rect = turtle.Turtle()rect.forward(150)#go for 150 pxrect.left(90) #turn left at 90 degree anglerect.forward(75) #go for 75 pxrect.left(90) #turn left at 90 degree angle againrect.forward(150) #go for 150 px for the second long siderect.left(90) #turn left at 90 degree angle for the last timerect.forward(75) #go for 75 px for the second short side

The output of the code above is going to be:

drawing a rectangle

Drawing an Equilateral Triangle

import turtlewn = turtle.Screen()tri = turtle.Turtle()tri.forward(100)tri.left(120)tri.forward(100)tri.left(120)tri.forward(100)

Since an equilateral triangle has an interior angle of 60 degrees, we used the supplementary angle of 60 degrees, 120 degrees. Because our turtle turns 120 degrees and then moves creates an angle of 60 degrees inside.

The output of the code above is going to be:

drawing a triangle

Instead of writing long code blocks, we can use for loop to make it short.

import turtlewn = turtle.Screen()tri = turtle.Turtle()for _ in range(3):tri.forward(100)tri.left(120)

This loop will do the same thing for each three sides of the triangle.

Note that if you do not want to create an equilateral triangle, you should chaneg the angle values. You can also change the distance values to make your triangle bigger or smaller.

With the same logic, you can create a pentagon, hexagon, heptagon or even dodecagon. You just have to pay attention to the angles. Below you can find the codes you can use to draw pentagons and hexagons.

#to create a hexagon
import turtle
wn = turtle.Screen()hex = turtle.Turtle()for _ in range(6):hex.forward(100)hex.left(60)#to create a pentagon
import turtle
wn = turtle.Screen()pen = turtle.Turtle()for _ in range(5):pen.forward(100)pen.left(72)

Drawing a Star

Drawing a star is a little more difficult than the operations we have done so far. But once you understand the logic, it will come easy.

import turtlewn = turtle.Screen()star = turtle.Turtle()for _ in range(5)star.forward(100)star.left(144)

The output of the code above is going to be:

drawing a star

But there is something strange about this star. Did you say this star is upside down? It is easy to fix. Just change one code line. Instead of star.left(144), try star.right(144).

drawing a better star

Drawing a Circle

So far, we have always drawn angular shapes and understood how they are drawn. So how do you draw a circle? It is even easier than others because in order to draw a circle we use .circle() command. Let’s draw a circle named circle.

import turtle
wn = turtle.Screen()
circle = turtle.Turtle()
circle.circle(80)

How do we fill it? Let’s assume you are going to draw a full moon. You should add three more code lines.

import turtle
wn = turtle.Screen()
circle = turtle.Turtle()
circle.color("red") #to define the color
circle.begin_fill() #begin to fill
circle.circle(80)
circle.end_fill() #end to fill

The output is going to be like:

drawing a filled circle

We have unconsciously drawn the Japanese flag. [Namaste] This gave me an idea. Now that we have learned to draw stars and circles, we can try to draw the Turkish Flag.

Drawing a Turkish Flag

First, let’s take a look at what the Turkish flag looks like.

White crescent and star on red background. We will make the crescent shape with two intertwined circles. One of the circles will be white while the other one is red. Let’s start coding.

To create the red background, we will use .bgcolor(“red”) command right before we named the turtle. Then, we will create a star and two circles and we are done.

import turtlewn = turtle.Screen()wn.bgcolor(“red”) #to make background color redturkey = turtle.Turtle()turkey.color(“white”)turkey.begin_fill()for _ in range(5):turkey.forward(100)turkey.right(144)turkey.end_fill()

After this coding process, if we run our programme, we will get:

flag with star

At this point, the issue we need to pay attention to is to place the two circles to the left of the star. Otherwise, the shapes will overlap and we will get a meaningless output. So how do we do this? How can we position the shapes where we want on the screen?

With .penup() and .pendown() commands. Also between those two commands we should use .goto(,) command to define the location.

We will define a new turtle to create our first circle. Let’s call it “turkey2”.

turkey2 = turtle.Turtle()turkey2.penup()turkey2.goto(-50,-100)turkey2.pendown()turkey2.color(“white”)turkey2.begin_fill()turkey2.circle(80)turkey2.end_fill()

At this point our flag will look like this:

it gets better

At that point, I realized that I need to move the star shape a little to the right. I will add the final version of the program at the bottom. If you’re in a hurry, you can go straight to the bottom. Let’s assume we fixed the star’s location and continue.

For the second circle which will be red and in front of the white circle, so, it can look like a crescent, I will use the name: “turkey3”.

turkey3 = turtle.Turtle()turkey3.penup()turkey3.goto(-20,-90)turkey3.pendown()turkey3.color(“red”)turkey3.begin_fill()turkey3.circle(70) #make it smaller so it can fit inside the otherturkey3.end_fill()

With this code block, we drew the Turkish flag. The output is:

turkish flag

The whole code block for the turkish flag:

import turtle
wn = turtle.Screen()
wn.bgcolor("red")
turkey = turtle.Turtle()
turkey.penup()
turkey.goto(50,0)
turkey.pendown()
turkey.color("white")
turkey.begin_fill()
for _ in range(5):
turkey.forward(100)
turkey.right(144)
turkey.end_fill()
turkey2 = turtle.Turtle()
turkey2.penup()
turkey2.goto(-50,-100)
turkey2.pendown()
turkey2.color("white")
turkey2.begin_fill()
turkey2.circle(80)
turkey2.end_fill()
turkey3 = turtle.Turtle()
turkey3.penup()
turkey3.goto(-20,-90)
turkey3.pendown()
turkey3.color("red")
turkey3.begin_fill()
turkey3.circle(70)
turkey3.end_fill()

Thank you for reading this far. If you have any other crazy ideas you want me to draw with Python turtle, please let me know in the comments. Don’t forget to follow me to be informed about more.

--

--