Python Classroom

Turtle Graphics


Turtle graphics was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966 to teach students to code.
You can use the CS50 Sandbox with the X Window option to use Turtle Graphics.
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.
By combining together these and similar commands, intricate shapes and pictures can be drawn. Here's an example:
Circles with different colors
The first step in using Python's turtle graphics system is to import the turtle module. Let's try using turtle in Python's interactive mode.
$ python
Python 3.7.1 (default, Feb 20 2019, 02:45:44)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle

Turtle Commands

Move the turtle forward by the specified distance, in the direction the turtle is headed.
Move the turtle backward by distance, opposite to the direction the turtle is headed.
Move turtle to the origin – coordinates (0,0) – and set its heading to its start-orientation.
Pull the pen up – no drawing when moving.
Pull the pen down – drawing when moving.
Set pencolor to colorstring, which is a Tk color specification string, such as "red", "yellow" etc.
Draw a circle with given radius.
Sets the turtle shape to turtle.
Undo (repeatedly) the last turtle action(s)
Erases all drawings that currently appear in the graphics window.

Show Turtle

The Python turtle is initially position in the center of the graphics window. To display the turtle in the window, you can use the turtle.showturtle() command, to hide the turtle, you can use the turtle.hideturtle() command.
>>> turtle.showturtle()
The Python turtle is initially positioned in the center of the graphics window. The arrowhead shows the direction that the turtle is currently facing. The default heading is 0 degree.
Result of the turtle.showturtle() command


The turtle.shapes command sets the turtle shape to one of these shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, and “classic”.
>>> turtle.shape("turtle")
Result of the turtle.shape("turtle") command

Forward and Backward

The turtle.forward(distance) or turtle.fw(distance) and turtle.backward(distance) or turtle.bk(distance) moves the turtle forward or backward by the specified distance, in the direction the turtle is headed.
>>> turtle.forward(100)
Result of the turtle.forward(100) command


You can turn the turtle to face a different direction by using either the turtle.right(angle) or turtle.left(angle) command.
>>> turtle.right(90)
Result of the turtle.right(90) command
These are the angles in a Cartesian or x-y plane.
Cartesian Plane

Pen Up and Down

The turtle.penup() command pulls the pen up so there will be no drawing when the pen moves. The turtle.pendown() command pulls the pen down.
>>> turtle.penup()
>>> turtle.pendown()

Drawing Circles

The turtle.circle(radius) command makes the turtle draw a circle with a radius you specify. For example.
>>> turtle.circle(100)
Result of the turtle.circle(100) command

Drawing Dots

The turtle.dot(size, color) command draws simple dots.
>>> turtle.dot(100, "red")
Result of the turtle.dot(100, "red") command

Changing the Pen Size

You can use the turtle.pensize(width) command to change the width of the turtle's pen, in pixels.
>>> turtle.pensize(5)

Changing the Drawing Color

You can use the turtle.pencolor(color) command to change the turtle's drawing color. The color argument is the name of a color, as a string.
>>> turtle.pencolor('red')

Changing the Background Color

You can use the turtle.bgcolor(color) to change the background color of the turtle's graphics window. The color argument is the name of a color, as a string.
>>> turtle.bgcolor('green')

Moving the Turtle to a Specific Location.

The pixel in the center of the graphics window is at the position (0,0)
You can use the turtle.goto(x, y) command to move the turtle from its current location to a specific position in the graphics window.
>>> turtle.goto(100,100)
If the turtle's pen is down, a line will be drawn as the turtle moves.
Coordinate Plane

Getting the Turtle's Current Position

You can use the turtle.pos() command to display the turtle's current position.
>>> turtle.pos()

Displaying Text in the Graphics Window

You can use the turtle.write(text) command to display text in the graphics window.
>>> turtle.write("Hello, World!")
Result of the turtle.write("Hello, World!") command

Resetting the Screen

  • The turtle.reset() command erases all drawings that currently appear in the graphics window, resets the drawing color to black and resets the turtle to its original position at the center of the screen.
  • The turtle.clear() command erases all drawings that currently appear in the graphics window.
  • The turtle.clearscreen() command erases all drawings that currently appear in the graphics window.
>>> turtle.reset()
>>> turtle.clear()
>>> turtle.clearscreen()


The turtle.undo() command undoes the previous command.
>>> turtle.undo()


Python 3's Turtle Graphics Reference Page