Back to Table of Contents

Skip to Reading Quiz

Skip to Exercises

Turtle Graphics (Basic Commands)

The first three readings in this course will teach you some fundamental elements of computer programming using something called Turtle Graphics. In these modules, you will be writing Python programs that make a turtle move around the screen.

Some of the elements of programming covered in these modules include:

This module covers five of the commands (or functions) you can use in a Turtle Graphics program. They are: forward, backward, left, right, and color.

If you'd like to follow along, open the following URL in a separate tab or window:

https://repl.it/languages/python_turtle

I would recommend making an account on REPL if you haven't already, so you can save your code and revisit it later.

What Is a Turtle Graphics Program?

A Turtle Graphics program, like any program, can be thought of as a sequence of instructions. For now, each instruction has some measurable result, and the instructions are executed strictly in order from the first instruction to the last instruction.

When you write a Turtle Graphics program, your instructions are given to a turtle object (which will show up as a little arrow on your screen). When executed together in order, your instructions will cause the turtle object to move around the screen and draw lines, like this:

A five-pointed star drawn with black lines. There is an arrow on the leftmost point facing right.

Whenever you start a new Turtle Graphics program, make sure to put the following line of code at the top of your program:

from turtle import *

Don't worry about what this means just yet. Just remember that for this class, you should always have it as the first line of your program.

Forward and Backward

The first two commands you are going to use are forward and backward. As you can imagine, they make your turtle object move forward and backward on the screen.

To invoke the forward command, add this line to your program:

forward(50)

Since you also need the import line mentioned before, your full program should look like this:

from turtle import *

forward(50)

Now, when you click the run button near the top of your screen, you should notice an arrow move across the right side of your screen. Once it is done moving, your screen should look something like this:

A full REPL window. On the left is the coding window, with the two-line program mentioned. The first line is from turtle import *. The second line (with a blank line before it) is forward(50). Above the program is a share button and a play button. Clicking the play button runs the program. On the right is the window containing the result, which is a right-facing arrow at the right end of a 50-pixel line segment.

Notice that the path taken by the arrow is traced with a black line. Later, we'll learn how to change the color of the lines drawn by the arrow.

The backward command is similar. Here is a program that only uses the backward command:

from turtle import *

backward(50)

As you probably predicted, this moves the arrow in the opposite direction:

A full REPL window. On the left is the coding window, with the two-line program mentioned. The first line is from turtle import *. The second line (with a blank line before it) is backward(50).  Above the program is a share button and a play button. Clicking the play button runs the program. On the right is the window containing the result, which is a right-facing arrow at the left end of a 50-pixel line segment.

You can write a program with multiple commands. For now, they will be executed in order from top to bottom. Here's a program that uses both the forward command and the backward command:

from turtle import *

forward(50)
backward(25)

A full REPL window. On the left is the coding window, with the three-line program mentioned. The first line is from turtle import *. The second line (with a blank line before it) is forward(50). The third line is backward(25). Above the program is a share button and a play button. Clicking the play button runs the program. On the right is the window containing the result, which is a right-facing arrow in the middle of a 50-pixel line segment. The point of the arrow is 25 pixels along the line segment.

Investigate

Notice the way the forward and backward commands are written. They are spelled in all lower case and immediately followed by a pair of parentheses surrounding a number. This is more or less how the commands have to be written. If you change one small thing - by making the first letter capitalized or forgetting the parentheses, for example - your program will not be understood.

Try changing small things about how the commands are written. Notice what happens when you do so. Does the program run to completion? Does any of it run?

If you write a command that Python does not understand, you can see what Python has to say about it by switching from the "result" tab to the "console" tab on the right side of your screen, like this:

Both sides of the REPL screen. On the left is the code with the share and run buttons, as usual. On the right is the console tab selected rather than the result tab. The console tab shows the following text: "NameError: name 'Backward' is not defined on line 4". The code tab has the following lines of code: from turtle import * on line 1, nothing on line 2, forward(50) on line 3, and Backward(50) with a capital B on line 4.

Left and Right

You can change the direction of the turtle using the left and right commands. These commands are similar to the forward and backward commands in how they are invoked. One important difference is that the number in parentheses represents a number of degrees rather than a number of pixels.

Watch what happens when you turn the turtle to the left by 90 degrees and then send the turtle forward 50 pixels:

from turtle import *

left(90)
forward(50)

A full REPL window. On the left is the coding window, with the three-line program mentioned. The first line is from turtle import *. The second line (with a blank line before it) is left(90). The third line is forward(50). Above the program is a share button and a play button. Clicking the play button runs the program. On the right is the window containing the result, which is an upwards-facing arrow at the top end of an upwards 50-pixel line segment.

Now, watch what happens if you turn the turtle to the right instead of to the left:

from turtle import *

right(90)
forward(50)

A full REPL window. On the left is the coding window, with the three-line program mentioned. The first line is from turtle import *. The second line (with a blank line before it) is right(90). The third line is forward(50). Above the program is a share button and a play button. Clicking the play button runs the program. On the right is the window containing the result, which is a downwards-facing arrow at the bottom end of a downwards 50-pixel line segment.

Changing Color

Let's get colorful! This is where you can really start to let out your inner artist.

To change the turtle's color, you can use the color command. The color command is similar to the commands we've learned so far in a couple ways:

The most important difference is that you do not put a number in the parentheses after the word color. Instead, you put a string - a unit of textual information surrounded by quotes. This string is the name of the color you would like the turtle to have at a particular moment in your program.

You can surround the color with single quotes, like this:

color('red')

You can also use double quotes, like this:

color("red")

In this class, we will be using single quotes by convention.

Here's an example program that uses the color command:

from turtle import *

color('red')
forward(50)

A full REPL window. On the left is the coding window, with the three-line program mentioned. The first line is from turtle import *. The second line (with a blank line before it) is color('red'). The third line is forward(50). Above the program is a share button and a play button. Clicking the play button runs the program. On the right is the window containing the result, which is a right-facing arrow at the right end of a red 50-pixel line segment.

Examples

Here are a couple example programs that put all the concepts in this module together.

from turtle import *

color('blue')
forward(50)
left(90)
color('green')
forward(50)
left(90)

A turtle graphics program. The first line is from turtle import *. The second line is blank. The third line is color('blue'). The fourth line is forward(50). The fifth line is left(90). The sixth line is color('green'). The seventh line is forward(50). The eighth line is left(90). The result on the right shows a blue line segment going to the right and a perpendicular green line segment going up, with an upward-facing blue arrow at the top of the upward line segment.

from turtle import *

color('red')
forward(20)
color('yellow')
forward(20)
color('green')
forward(20)
color('blue')
forward(20)
color('purple')
forward(20)

The first line of this program is from turtle import *. The second line is blank. The third line is color('red'). The fourth line is forward(20). The fifth line is color('yellow'). The sixth line is forward(20). The seventh line is color('green'). The eighth line is forward(20). The ninth line is color('blue'). The tenth line is forward(20). The eleventh line is color('purple'). The twelfth line is forward(20). The result on the right is a line segment with 20 pixels each of red, yellow, green, blue, and purple going left to right. The arrow is purple, faces to the right, and is at the right end of the line segment.

Reading Quiz

Back to Top

1. Which of the following is the correct way to move the arrow forward by 30 in a Turtle Graphics program?

a.
forward(30)
b.
30 forward
c.
forward 30
d.
30(forward)

2. What does the following program draw?

from turtle import *

forward(20)
color('red')
forward(20)
a.

It draws a line segment that is red.

b.

It draws two perpendicular lines, one of which is red and one of which is black.

c.

It draws a line segment that is half red and half black.

d.

It draws two perpendicular lines that are red.

Exercises

Back to Top

For each of these exercises, you should start by creating a new Turtle Graphics REPL session. You can do this by going to:

https://repl.it/languages/python_turtle

Exercise 0.0

This Turtle Graphics program attempts to draw a single black line of length 50. However, something isn't quite right about it. Copy the code into a new Turtle Graphics REPL session and see if you can fix it!

forward(50)

When the program is fixed, it should draw this:

A black line segment going 50 pixels to the right of the origin, with a black arrow facing to the right at the right end of it.

Exercise 0.1

This Turtle Graphics program attempts to draw a line segment that alternates black, green, black, green. Currently, it does not do this. Copy the code into a new Turtle Graphics REPL session and see if you can fix it!

from turtle import *

forward(20)
color('green')
forward(20)

forward(20)
color('green')
forward(20)

When the program is fixed, it should draw this:

Four line segments of length 20 in a row going to the right of the origin. The first is blue, the second green, the third blue, and the fourth green. The arrow is green and facing right at the end of the fourth line segment.

Exercise 0.2

Open a new Turtle Graphics REPL session and write a program that draws two distinct vertical line segments - one green segment of length 40 going downwards from the origin, and one yellow segment of length 40 going upwards from the origin.

If you draw a line segment of length 80 and cover half of it with another line segment, that does not count as a line segment of length 40! Similarly, if you draw multiple smaller line segments whose lengths add up to 40, that also does not count as a line segment of length 40.

When your program is finished, it should look like this:

A green line segment extending 40 pixels downwards from the origin and a yellow line segment extending 40 pixels upwards from the origin. The arrow is yellow and is facing up at the top of the yellow line segment.

Note that the arrow started at the middle of the two line segments. Your arrow's final location may be different depending on the order in which you draw the line segments.

Exercise 0.3

Open a new Turtle Graphics REPL session and write a program that draws the following:

Recall from the last exercise that you should not do the following:

Here is an example of what your result might look like:

A horizontal blue line segment of length 100 whose center is at the origin. An orange line segment of length 25 extending upwards from the origin. Two orange line segments extending 20 pixels to either side of the vertical orange line segment. They go exactly between the vertical orange segment and the blue horizontal segment (at angles of 135 degrees and 45 degrees to the left of the arrow's starting direction, which is to the right). The arrow is orange and is facing upwards with its tip at the origin.

Back to Top

Back to Table of Contents