The Python turtle library is a library that provides programmers with a virtual canvas to create shapes and images.
To draw the shapes and images, you use an onscreen pen called the turtle. The name of the library was derived from the name of this pen.
The Python turtle library is a useful learning tool because it helps programmers realize that Python is an interactive programming language. The turtle library lets your see how fun it is to learn Python!
Because of this, turtle is often used to introduce children to programming.
Although it is a straightforward library, it provides a versatile way of learning the Python concepts. It acts as a stepping stone for kids when starting their journey in Python programming.
So, is the Python turtle library only meant for kids?
It’s also a good library for adults who are beginners to Python programming.
With the turtle library, you can create different shapes and images in Python.
Let me show you how you can get started with the Python turtle library…
Getting started with turtle
Before going further, there are two important points for you to note:
The turtle library is shipped with Python.
So, if you’ve installed Python on your computer, you already have turtle.
This means that you won’t be required to install the library separately.
To write and run your code, you can use applications like IDLE or Jupyter Notebook.
However, if you are not familiar with them, you can use REPL.
Install the latest version of Python on your computer, Python 3.X.
Since the library is pre-installed in Python, you only need to import it.
You can use the import statement for this.
Let me show you how:
The above command will import the turtle library into your program.
You can then use it to create shapes and images.
turtle is a graphical library. This means you will need to create a separate window to carry out each drawing command.
This window is known as the screen.
The easiest way to create the screen is by initializing a variable for it.
In Python, variables help us store information that we need to access later.
A Python variable is said to have been initialized once it has been assigned an initial value.
The value of a variable is not constant. It can change many times during program execution.
Here’s how you can create your turtle screen and assign it to a variable called screen:
screen = turtle.getscreen()
To reiterate, we’ve created a variable and given it the name screen.
We have then called the .getscreen()Python function from the turtle library.
This is the function that generates the screen for us.
So, anytime you call the screen variable, you will be referring to the generated screen.
When you run the code, a separate will pop up as shown below:
The output of your code will be displayed on this screen.
Notice that there is a small black triangle at the center of the screen.
This is called the turtle.
The next thing we need to do is initialize a variable that you will use to refer to the turtle.
We will give this variable the name tt, which is a common practice when working in the turtle library.
The creating of this tt variable is shown below:
tt = turtle.Turtle()
You have now create both the screen and the turtle.
See the screen as a canvas and the turtle as a pen. You can code the turtle to move to any place on the screen.
The turtle has characteristics such as color, size, and, speed that you can change. The turtle points in a particular direction and will always move in that direction unless you instruct it otherwise.
Here are two things you should keep in mind about the turtle:
When it is up, it will not draw any line when it moves.
When it is down, it will draw a line when it moves.
We will see this in practice soon. Next, I will show you how to program using the turtle library. Let’s continue…
How to Program with turtle
Now, it’s time to get into real coding.
I want to show you how to perform various programming tasks using the turtle library.
First, I will be showing you how to…
Move the turtle
You can move the turtle in the following four directions:
To move the turtle forward or backward, you need to call the .forward() and .backward() functions respectively.
The direction to which the turtle moves depends on the direction it is facing.
To change the direction of the turtle, you can use the .left() and the .right() functions.
When calling these functions, you must specify the number of degrees by which to move the turtle.
Let’s see the turtle move…
To move the turtle 90 degrees to the right, run the following command:
The command will change the turtle to look as follows:
Next, let’s move the turtle forward by 120 units.
Simply run the following command:
The turtle should now look as follows:
Next, let’s see the turtle move 90 degrees to the left:
The turtle should now look as follows:
We can now move the turtle backward by 120 units:
The turtle should look as follows:
So, the turtle first turned right by 90 degrees, moved forward by 120 units, turned left by 90 degrees, then moved backwards by 120 units.
Also, you can use the commands in their short form.
This is shown below:
tt.rt() for tt.right()
tt.fd() for tt.forward()
tt.lt() for tt.left()
tt.bk() for tt.backward()
The use of degrees makes it possible for us to draw a line from the current position to any arbitrary position on the screen.
Consider the figure given below:
The above figure shows how the screen is divided into four quadrants.
At the beginning of your program, the turtle is located at the position marked as (0,0).
This position is also known as Home.
If you need to move the turtle to any area of the screen, you can use the .goto() function.
The function expects you to pass the coordinates to which you need to move the turtle.
The above command should move the turtle as follows:
The output shows that you have drawn a line from the point (0,0) to the point (110,110).
You can bring the turtle back home by running the following command:
You now know how to move the turtle.
In this section, I will be showing you how to draw shapes.
First, I will show you how to draw polygons because they are simple.
Polygons are made up of many straight lines connected at particular angles.