0% found this document useful (0 votes)
51 views

Drawing Polygons With The Turtle Module

The document discusses using Python's turtle module to draw shapes. It introduces basic programming concepts like loops, variables, and functions that are used with the turtle module. The turtle module allows you to control a turtle image on screen and draw shapes by providing instructions. The document provides examples of importing the turtle module and using functions like forward(), right(), and shape() to move the turtle and draw a square. It also introduces for loops to repeat code and draw shapes more efficiently.

Uploaded by

nobodyscribd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Drawing Polygons With The Turtle Module

The document discusses using Python's turtle module to draw shapes. It introduces basic programming concepts like loops, variables, and functions that are used with the turtle module. The turtle module allows you to control a turtle image on screen and draw shapes by providing instructions. The document provides examples of importing the turtle module and using functions like forward(), right(), and shape() to move the turtle and draw a square. It also introduces for loops to repeat code and draw shapes more efficiently.

Uploaded by

nobodyscribd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

dr AwI n G Po lyG o n s wIth

1
th E  tu rtlE M o du lE

Centuries ago a Westerner heard a Hindu say the Earth


rested on the back of a turtle. When asked what the turtle was
standing on, the Hindu explained, “It’s turtles all the way down.”

Before you can start using math to build


all the cool things you see in this book,
you’ll need to learn how to give instructions
to your computer using a programming language
called Python. In this chapter you’ll get familiar with
some basic programming concepts like loops, vari-
ables, and functions by using Python’s built-in turtle
tool to draw different shapes. As you’ll see, the turtle
module is a fun way to learn about Python’s basic fea-
tures and get a taste of what you’ll be able to create
with programming.
Python’s turtle Module
The Python turtle tool is based on the original “turtle” agent from the
Logo programming language, which was invented in the 1960s to make
computer programming more accessible to everyone. Logo’s graphical envi-
ronment made interacting with the computer visual and engaging. (Check
out Seymour Papert’s brilliant book Mindstorms for more great ideas for
learning math using Logo’s virtual turtles.) The creators of the Python
programming language liked the Logo turtles so much that they wrote
a module called turtle in Python to copy the Logo turtle functionality.
Python’s turtle module lets you control a small image shaped like a
turtle, just like a video game character. You need to give precise instruc-
tions to direct the turtle around the screen. Because the turtle leaves a
trail wherever it goes, we can use it to write a program that draws different
shapes.
Let’s begin by importing the turtle module!

Importing the turtle Module


Open a new Python file in IDLE and save it as myturtle.py in the Python
folder. You should see a blank page. To use turtles in Python, you have to
import the functions from the turtle module first.
A function is a set of reusable code for performing a specific action in a
program. There are many built-in functions you can use in Python, but you
can also write your own functions (you’ll learn how to write your own func-
tions later in this chapter).
A module in Python is a file that contains predefined functions and
statements that you can use in another program. For example, the turtle
module contains a lot of useful code that was automatically downloaded
when you installed Python.
Although functions can be imported from a module in many ways,
we’ll use a simple one here. In the myturtle.py file you just created, enter
the following at the top:

from turtle import *

The from command indicates that we’re importing something from


outside our file. We then give the name of the module we want to import
from, which is turtle in this case. We use the import keyword to get the use-
ful code we want from the turtle module. We use the asterisk (*) here as a
wildcard command that means “import everything from that module.” Make
sure to put a space between import and the asterisk.
Save the file and make sure it’s in the Python folder; otherwise, the
program will throw an error.

WARNING Do not save the file as turtle.py. This filename already exists and will cause a con-
flict with the import from the turtle module! Anything else will work: myturtle.py,
turtle2.py, mondayturtle.py, and so on.

4 Chapter 1
Moving your turtle
Now that you’ve imported the turtle module, you’re ready to enter instruc-
tions to move the turtle. We’ll use the forward() function (abbreviated as fd)
to move the turtle forward a certain number of steps while leaving a trail
behind it. Note that forward() is one of the functions we just imported from
the turtle module. Enter the following to make the turtle go forward:

forward(100)

Here, we use the forward() function with the number 100 inside paren-
theses to indicate how many steps the turtle should move. In this case, 100
is the argument we pass to the forward() function. All functions take one or
more arguments. Feel free to pass other numbers to this function. When
you press F5 to run the program, a new window should open with an arrow
in the center, as shown in Figure 1-1.

Figure 1-1: Running your first line of code!

As you can see, the turtle started in the middle of the screen and walked
forward 100 steps (it’s actually 100 pixels). Notice that the default shape is
an arrow, not a turtle, and the default direction the arrow is facing is to the
right. To change the arrow into a turtle, update your code so that it looks
like this:

myturtle.py from turtle import *


forward(100)
shape('turtle')

As you can probably tell, shape() is another function defined in the


turtle module. It lets you change the shape of the default arrow into other
shapes, like a circle, a square, or an arrow. Here, the shape() function takes

drawing Polygons with the turtle module 5


the string value 'turtle' as its argument, not a number. (You’ll learn more
about strings and different data types in the next chapter.) Save and run
the myturtle.py file again. You should see something like Figure 1-2.

Figure 1-2: Changing the arrow into a turtle!

Now your arrow should look like a tiny turtle!

Changing directions
The turtle can go only in the direction it’s facing. To change the turtle’s
direction, you must first make the turtle turn a specified number of
degrees using the right() or left() function and then go forward. Update
your myturtle.py program by adding the last two lines of code shown next:

myturtle.py from turtle import *


forward(100)
shape('turtle')
right(45)
forward(150)

Here, we’ll use the right() function (or rt() for short) to make the turtle
turn right 45 degrees before moving forward by 150 steps. When you run this
code, the output should look like Figure 1-3.

Figure 1-3: Changing turtle’s


direction

6 Chapter 1
As you can see, the turtle started in the middle of the screen, went for-
ward 100 steps, turned right 45 degrees, and then went forward another
150 steps. Notice that Python runs each line of code in order, from top to
bottom.

ExErCIsE 1-1: squArE dAnCE


Return to the myturtle.py program. Your first challenge is to modify the code in
the program using only the forward and right functions so that the turtle draws
a square.

repeating Code with loops


Every programming language has a way to automatically repeat commands
a given number of times. This is useful because it saves you from having
to type out the same code over and over and cluttering your program. It
also helps you avoid typos that can prevent your program from running
properly.

using the for loop


In Python we use the for loop to repeat code. We also use the range keyword
to specify the number of times we go through the loop. Open a new program
file in IDLE, save it as for_loop.py, and then enter the following:

for_loop.py for i in range(2):


print('hello')

Here, the range() function creates i, or an iterator, for each for loop. The
iterator is a value that increases each time it’s used. The number 2 in paren-
theses is the argument we pass to the function to control its behavior. This
is similar to the way we passed different values to the forward() and right()
functions in previous sections.
In this case, range(2) creates a sequence of two numbers, 0 and 1. For
each of these two numbers, the for command performs the action specified
after the colon, which is to print the word hello.
Be sure to indent all the lines of the code you want to repeat by pressing
TAB (one tab is four spaces). Indentation tells Python which lines are inside
the loop so for knows exactly what code to repeat. And don’t forget the colon
at the end; it tells the computer what’s coming up after it is in the loop. When
you run the program, you should see the following printed in the shell:

hello
hello

drawing Polygons with the turtle module 7

You might also like