What is Python Turtle used for?

Turtle is a Python feature that lets us draw.

turtle is a pre-installed Python library that allows users to create pictures and shapes with a provided, virtual canvas. The onscreen pen you use to draw is called the turtle.

Uses of turtle programming

  • Create mini-games and animations
  • Turtle graphics are a great way to introduce kids to coding. With short programs of just five to ten lines of code, kids can create and modify while learning.

Steps for executing a turtle program

1. Import the turtle module

Python Turtle library contains each method and function. To access the Python library, import it into your Python environment:

import turtle

2. Create a separate window

turtle is a graphical library, which means you’ll need to create a separate window (called the screen) to carry out each drawing command. You can create this screen by initializing a variable for it. To open the turtle screen, initialize a variable by:

x = turtle.getscreen()

You should see a separate window open up:

This is a screen where you can view the output of your code. The little black triangle in the middle of the screen is the turtle.

3. Create a turtle to control.

Now, you need to create a turtle initialized to a variable – you will use it throughout the program to refer to the turtle:

alex = turtle.Turtle()

creates a turtle named alex

4. Draw around using the turtle methods.

We can program the turtle to move around the screen with different method like left(), right(), forward(), backward(). The turtle has certain changeable characteristics, like size, color, and speed.

alex.right(90)
alex.forward(100)
alex.left(90)
alex.backward(100)

When we run these commands, the turtle will turn right by ninety degrees, go forward by a hundred units, turn left by ninety degrees, and move backward by a hundred units. You can see how this looks in the image below:

Free Resources