turtle¶
The first time I encountered something like this was during one of my ICT classes during secondary school - it was with MSWLogo. According to this Wikipedia page, MSWLogo is an interpreted programming language based on the Logo language (which in itself is an educational programming language), with a graphical user interface (GUI) front-end.

The direction functions involve moving this thing (often represented as an arrow head or triangle) we will call the turtle cursor around a blank canvas. One will be able to instruct how far the turtle cursor moves (which typically traces a line in its travelled path unless instructed otherwise), or turn it either to the left or right of wherever the turtle cursor is pointing at that moment. With it, you are able to draw various shapes and images, ranging from simple to even the most complex!
The turtle package in Python provides this same set of features, albeit using Python programming instead of Logo/MSWLogo.
As far as I am aware, the turtle package is merely for learning purposes and is not widely used in the industry for anything.
Some turtle Functions¶
The turtle package defines several functions used to draw shapes and images.
The imperative ones you should be aware of are as follows:
| Function | What it does |
|---|---|
forward(n) or fd(n) |
Moves the turtle cursor forward n units |
backward(n) or bk(n) |
Moves the turtle cursor backward n units |
left(n) |
Turns the turtle cursor left by n degrees |
right(n) |
Turns the turtle cursor right by n degrees |
pd() |
Pen down; movements from this point forth will be traced |
pu() |
Pen up; movements from this point forth will not be traced |
hideturtle() |
Hide the turtle cursor if visible |
showturtle() |
Show the turtle cursor if not visible |
Python has a dedicated documentation page for the turtle library, which you can access right here.
Some Basic Math¶
Before we get to start drawing anything partially novel with these functions, we need to run through some basic math/geometry concepts.. stay with me now.
I would opine that knowing this one simple geometry concept and some simple arithmetic will be enough to get started with drawing regular polygons with turtle.
I will try to dumb it down to make it not as overwhelming, but feel free to search up some terms used that do not make sense to you (yet).
For now, just understanding these concepts is enough - no deep twists.
Exterior Angles¶
For a refresher on this thing called "angles"...
Check out this video here:
..or feel free to consult your nearest schooling kid(s) and/or other various resources.
Let's look at this equilateral triangle (a regular polygon of 3 equal sides).

A regular polygon basically means each of the inner angles are of equal size. In the case of an equilateral triangle, each inner angle is \(60\degree\). These inner angles are what we call interior angles, but we won't be dwelling on them for now. What we want to focus on are what we call exterior angles. Exterior angles, by definition, are the angles formed if we extended each side's length beyond the confines of the polygon shape. The exterior angles of this triangle are marked in red as shown (in contrast, interior angles in blue):

The exterior angles are also similar in size since all the interior angles are equal. You can verify this by subtracting each of the interior angle values by \(180\degree\) (the angle value for a straight line). For our equilateral triangle, this means that each exterior angle value is \(180\degree - 60\degree = 120\degree\).
Here is the interesting part - there are three exterior angles in our equilateral triangle. Add them all up, and you will get \(360\degree\) in total.
Notice too that in a square/rectangle, where each exterior angle is \(90\degree\), the total of all four exterior angles is also \(360\degree\).
I would not call this a proper mathematical proof, but you can probably see the correlation between the exterior angles of any \(n\)-sided polygon and the total value (here, \(n\) is a natural number/positive integer, i.e., \(n\in \mathbb{N}\) or \(n\in \mathbb{Z}^{+}\)). It is also not too difficult to derive the exterior angle and interior angle of a regular polygon, the first of which is easier to get.
With this formula, you can now calculate the exterior angle for most regular polygons:
| Regular Polygon | \(n\) | Exterior Angle, \(\frac{360\degree}{n}\) |
|---|---|---|
| triangle | 3 | \(\frac{360\degree}{3} = 120\degree\) |
| square/rectangle | 4 | \(\frac{360\degree}{4} = 90\degree\) |
| pentagon | 5 | \(\frac{360\degree}{5} = 72\degree\) |
| hexagon | 6 | \(\frac{360\degree}{6} = 60\degree\) |
| octagon | 8 | \(\frac{360\degree}{8} = 45\degree\) |
| decagon | 10 | \(\frac{360\degree}{10} = 36\degree\) |
| \(\vdots\) |
I say most, because some \(n\)-sided polygons like those where $n ≥ 7 $ is prime/contains a prime factor $ k ≥ 7$ will have their totals only reach close but not equal to \(360\degree\). For our intents and purposes, though, this extra detail is not too important (though it does explain why the turtle cursor does not return to its original position after drawing these specific polygons).
Right, now that we have this concept covered, we can get straight to applying them into drawing our shapes with the turtle package in Python.. and do more than just some basic math.

Creating Shapes¶
n-sided Regular Polygons¶
Let's begin with drawing that same triangle shown earlier.
Depending on what you choose to do, your triangle may be an upside-down version of the same thing.
The simplest method requires only 2 functions: forward()/fd(), and right()/rt() or left()/lt().
- Suppose we set the length of the triangle to be 100 units - enter
100as the only parameter in yourforward()orfd()function. - You will need to turn the
turtlecursor a couple of times to help draw the desired shape, either withright()orleft(). For its parameter, that's where the exterior angle comes in clutch - for a triangle, we shall put in120as the only parameter. - Suppose you decided to use
forward()andleft()only. Add the following on top of your code file:
Try to work this out on your own, then check the following solution to see if there was anything you could have improved.
| Code for Drawing An Equilateral Triangle with turtle | |
|---|---|
We can always change the parameter values as needed to draw different polygons.. but that's manual work that honestly can be made simpler.
| Code for Drawing a Regular Polygon with turtle | |
|---|---|
DO NOT name your file turtle.py!!
The reason lies with that import statement you require the turtle module that has already been prepared by Python.
If you create a new file called turtle.py in your project folder, running it will produce errors.
These errors would relate much to how the functions you learnt with turtle not working.
Five-pointed Star¶
Okay, the angle calculation for drawing five-pointed stars is not as straightforward, but it's still sort of basic.. I think it's not basic, oh gosh.
But the concepts involved is basic - still stuff one learns in upper primary or lower secondary math (middle school geometry for some others, if I got it right).

Let's see.. inside a five-pointed star drawn using turtle, we can see a regular pentagon formed like as shown below.

We know that the exterior angle from a pentagon is \(72\degree\). If we extended any one of the two lines in the angle past the vertex point (where it bends), we see that this exterior angle being formed. Do this with all vertices in the pentagon and you will form the same five-pointed star, with 5 equivalent (not equilateral) triangles formed outside as a result.
These equivalent triangles formed are examples of an isosceles triangle. Such triangles have 2 equal sides in length, and 2 equal angles. Here, the pentagon's exterior angle make up these two equal angles.
Now, in every triangle, all interior angles (the angles formed inside the shape) total up to \(180\degree\). Since we know two of these angles are \(72\degree\), the remaining angle is equal to \(180\degree - 72\degree - 72\degree = 36\degree\). We're close now.

\(36\degree\) isn't how far the turtle head should turn, though.
It's the other way, which means we need to find the complementary angle to \(36\degree\) (i.e., if we drew any of the two lines forming this angle past the vertex/bend point, it's that other angle we form).

The complementary angle \(180\degree - 36\degree = 144\degree\) is the angle we require the turtle head to turn 5 times to produce our 5-pointed star.
Oof, that's a little much for those who may not be good with math, but that's the best explanation I can come up with.
I'm pretty sure there may be another explanation for it, perhaps easier for some of you.
But I hope this works to help clarify how this works!
Using this geometric proof, the Python code to form a regular 5-point star is as follows:
| Drawing a Five-Point Star in Python using turtle | |
|---|---|