4.2. Our First Turtle Program¶
Let’s try a couple of lines of Python code to create a new turtle and start drawing a simple figure like a rectangle. We will refer to our first turtle using the name alex, but remember that you can choose any name you wish as long as you follow the naming rules from the previous chapter.
The program as shown will only draw the first two sides of the rectangle. After line 4 you will have a straight line going from the center of the drawing canvas towards the right. After line 6, you will have a canvas with a turtle and a half drawn rectangle. Press the run button to try it and see.
import turtle # allows us to use the turtles library
wn = turtle.Screen() # creates a graphics window
alex = turtle.Turtle() # create a turtle named alex
alex.forward(150) # tell alex to move forward by 150 units
alex.left(90) # turn by 90 degrees
alex.forward(75) # complete the second side of a rectangle
(ch03_1)
Here are a couple of things you’ll need to understand about this program.
The first line tells Python to load a module named turtle
. That module
brings us two new types that we can use: the Turtle
type, and the
Screen
type. The dot notation turtle.Turtle
means “The Turtle type
that is defined within the turtle module”. (Remember that Python is case
sensitive, so the module name, turtle
, with a lowercase t, is different from the type
Turtle
because of the uppercase T.)
We then create and open what the turtle module calls a screen (we would prefer to call it a window, or in the case of this web version of Python simply a canvas), which we assign to the name wn. Every window contains a canvas, which is the area inside the window on which we can draw.
In line 3 we create a turtle. The name alex is made to refer to this turtle. These first three lines set us up so that we are ready to do some drawing.
In lines 4-6, we instruct the object alex to move and to turn. We do this by invoking or activating alex’s methods — these are the instructions that all turtles know how to respond to.
Complete the rectangle ...
Modify the program by adding the commands necessary to have alex complete the rectangle.
Check your understanding
Mixed up programs

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.
Drag from here
ella.left(90)
ella.forward(75)
import turtle
window = turtle.Screen()
ella = turtle.Turtle()
ella.right(90)
ella.forward(150)
Drop blocks here

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.
Drag from here
import turtle
maria = turtle.Turtle()
maria.right(45)
maria.forward(75)
window = turtle.Screen()
maria.left(90)
maria.forward(150)
Drop blocks here

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.
Drag from here
jamal = turtle.Turtle()
import turtle
jamal.left(180)
window = turtle.Screen()
jamal.forward(75)
Drop blocks here
An object can have various methods — things it can do — and it can also have attributes — (sometimes called properties). For example, each turtle has a color attribute. The method invocation alex.color(“red”) will make alex red and the line that it draws will be red too.
The color of the turtle, the width of its pen(tail), the position of the turtle within the window, which way it is facing, and so on are all part of its current state. Similarly, the window object has a background color which is part of its state.
Quite a number of methods exist that allow us to modify the turtle and window objects. In the example below, we show just show a couple and have only commented those lines that are different from the previous example. Note also that we have decided to call our turtle object tess.
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen") # set the window background color
tess = turtle.Turtle()
tess.color("blue") # make tess blue
tess.pensize(3) # set the width of her pen
tess.forward(50)
tess.left(120)
tess.forward(50)
wn.exitonclick() # wait for a user click on the canvas
(ch03_2)
The last line plays a very important role. The name wn refers to the window shown
above. When we invoke its exitonclick
method, the program pauses execution and waits for the user to click the mouse somewhere in the window.
When this click event occurs, the response is to close the turtle window and
exit (stop execution of) the Python program.
Each time we run this program, a new drawing window pops up, and will remain on the screen until we click on it.
Check your understanding
turtle-2-5: Consider the following code:
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(150)
alex.left(90)
alex.forward(75)
What does the line “import turtle” do?
turtle.Turtle()
to get a new Turtle object?
turtle-2-8: Which of the following code would produce the following image?

Mixed up programs

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.
Drag from here
jamal.left(90)
jamal.forward(75)
wn.exitonclick()
wn.bgcolor("blue")
jamal = turtle.Turtle()
jamal.right(90)
jamal.forward(150)
jamal.color("white")
jamal.pensize(10)
import turtle
wn = turtle.Screen()
Drop blocks here

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.
Drag from here
jamal.left(90)
jamal.forward(150)
import turtle
wn = turtle.Screen()
wn.bgcolor("green")
jamal = turtle.Turtle()
jamal.color("white")
jamal.pensize(10)
jamal.right(180)
jamal.forward(100)
wn.exitonclick()
jamal.left(90)
jamal.forward(50)
Drop blocks here