Last Updated:

Turtle module — drawing graphics in Python

Turtle module

Turtle is a pre-installed Python library, similar to a virtual canvas on which we can draw pictures and attractive shapes. It provides an on-screen pen that we can use to draw.

The turtle library is designed primarily to introduce children to the world of programming. With the Help of the Turtle library, new programmers can get an idea of how we can program in Python in a fun and interactive way.

This library is useful for children and for an experienced programmer, because it allows you to create unique shapes, attractive pictures and various games. We can also develop mini-games and animations. In the next section, we will get acquainted with the various functionalities of the turtle library.

Before working with the turtle library, we need to make sure of the two most important things to program.

  1. Python environment — We need to be familiar with the Python desktop environment. We can use applications such as IDLE or JupiterNotebook. We can also use an interactive Python shell.
  2. Python version – We should have Python 3 on our system; if not, download it from the official Python website.

The turtle is built into the library, so we don't have to install it separately. We just need to import the library into our Python environment.

The Python turtle library consists of all the important techniques and functions we'll need to create our designs and images. Import the turtle library with the following command.

import turtle

Now we can access all the methods and functions. First, we need to create a special window in which we execute each drawing command. We can do this by initializing a variable for it.

s = turtle.getscreen()

python turtle graphics

This will look like the image above, and the little triangle in the middle of the screen is a turtle. If you don't see the screen on your computer system, use the code below.

Example:

importturtle
# Creating a turtle screen
s = turtle.getscreen()
# To stop the screen to display
turtle.mainloop()

Exit:

python turtle graphics

The screen is the same as the canvas, and the turtle acts like a pen. You can move the turtle to create the desired shape. The turtle has certain variable characteristics such as color, speed and size. It can be moved in a certain direction and move in that direction unless we tell it otherwise.

Programming with turtle

First, we need to learn how to move the turtle in all directions as we want. We can customize the pen like a turtle and its surroundings. Let's learn a couple of commands to perform a few specific tasks.

The turtle can be moved in four directions:

  • Forward
  • Back
  • To the left
  • Right

Turtle movement

A turtle can move back and forth in the direction it looks. Let's look at the following features:

forward or turtle.fd(distance) — It moves the turtle in a straight direction for a certain distance. It takes a single distance parameter, which can be an integer or a floating point.

Example:

importturtle
# Creating a turtle screen
t = turtle. Turtle()
# To stop the display of the screen
t.forward(100)
turtle.mainloop()

Exit:

python turtle graphics

back(back) or turtle.bk or turtle.backward(distance) — This method moves the turtle in the opposite direction where the turtle is headed. This does not change the direction of movement of the turtle.

Example:

importturtle
# Creating a turtle screen
t = turtle. Turtle()
# Move the turtle in the opposite direction
t.backward(100)
# To stop the screen to display
turtle.mainloop()

Exit:

python turtle graphics

right(angle) or turtle.rt(angle) — This method moves the turtle to the right by a unit of angle.

Example:

importturtle
# Creating a turtle screen
t = turtle. Turtle()
t.heading()
# Move the turtle in the opposite direction,
t.right(25)

t.heading()
# To stop the screen to display
turtle.mainloop()

Exit:

left (corner) or turtle.lt (angle) — This method turns the turtle to the left by a unit of angle. Let's look at the following example.

Example

importturtle
# Creating a turtle screen
t = turtle. Turtle()

t.heading()
# Move the turtle to the left
t.left(100)

t.heading()
# To stop the screen to display
turtle.mainloop()

Exit:

python turtle graphics

The screen is initially divided into four quadrants. The turtle located at the beginning of the program (0.0) is known as the House.

goto(x, y=None) or turtle.setpos(x, y=None) turtle.setposition(x, y=None) — This method is used to move the turtle to another area of the screen. It takes two coordinates, x and y. Consider the following example.

importturtle
# Creating a turtle screen
t = turtle. Turtle()
# Move the turtle with the coordinates
t.goto(100, 80)
# To stop the screen to display
turtle.mainloop()

Exit:

python turtle graphics