Introduction
Requirement
CSI 100 Turtle Graphics Lab
1 Introduction
Welcome to CSI100! This lab is a head-first introduction to programming. Here are the important things
to know:
• Don’t Panic! The most important thing that this lab is about is managing confusion. There will be
a lot of confusion in learning how to program. Try things and ask lots of questions!
• In this lab we are going to draw pictures with turtles. It’s a classic exercise for introducing new
programmers to code, and it can make really cool pictures!
• Work in pairs. We’ll do this a lot in this class. The two of you should share a laptop, and share your
ideas too. You will be able to upload deliverables to canvas as a group.
• There is just one deliverable for this lab, in section 4. Basically, you will draw a cool picture and
upload it to canvas.
2 Running your first Python program
In this section of the lab, we’ll download and run our first python program. Let’s start by getting the code.
Go to canvas and find the files for today’s lab. Save a copy of turtle lab.py to your Downloads folder.
Comment 1 Why save your file to the Downloads folder? For this lab, it is just somewhere easy to
find. In the future, feel free to use any location you prefer on your computer. Remember that the lab
laptops reset on every reboot, so they probably won’t be there anymore by next class.
Comment 2 The lab computers have both OS X and Windows installed. For our labs, we will work in
OS X. If your computer boots into Windows, switch operating systems by restarting the computer. Hold
down the ‘Option’ key when you see the grey loading screen. Let go when you see startup icons appear,
and choose ‘Macintosh HD’
The code that you downloaded, turtle lab.py, is just a text file. You can open it in any text editor,
like textEdit (mac) or Notepad (windows). Even better, the lab computers come with Sublime and Atom,
which are both programmer-oriented text editors. Most importantly, this means that they automatically
highlight code to make it easier to read.
Exercise 1 Go ahead and open turtle lab.py in a text editor. Some of the lines of code are special:
they are comments. Comments don’t contain any instructions for the computer; they just help other
programmers understand what is going on.
Figure out which lines are comments. How do you think you would add a new comment to the
program?
Now we want to run the program. This means telling python to read through turtle lab.py and execute
each instruction in order. To do this, we need to use the command line. You can open the command line by
clicking on the spotlight icon ( ) on the right side of the mac menu bar. Search for and open Terminal,
which is the mac command line.
Comment 3 We usually interact with computers through a mix of keyboard, mouse, and touchscreens.
When we want to give instructions precisely and formally, like in programming, the keyboard is often
most helpful. The command line is a text-based interface to the computer. We type commands, press
enter, and then the computer executes them.
1 of 4
CSI 100 Turtle Graphics Lab
The command line is like the Finder: it is always in a folder, and you can move it from one folder to
another. In order to run turtle lab.py, we need to navigate to the Downloads folder where we put the file.
The rest of this section will introduce a few command line commands to let you do this.
The command line always starts up in your home folder. If you type pwd (which stands for “print working
directory”), the command line will tell you what its courent directory is.
> pwd
/Users/wilsonkl
Comment 4 Your command line probably actually looks more like this:
WC-07792:~ student$ pwd
/Users/student
This is normal. The WC-07792:~ student$ part is called the command line prompt. Since it changes
so much from one computer to another, when programmers communicate to each other, they usually
just write > as a sort of standard prompt, even though no one’s prompt looks exactly like that.
You can also see a list of all of the files and folder that are in the current directory using ls (for “list”):
> ls
Applications Library anaconda
Desktop Movies Documents
Music Downloads Pictures
Dropbox Public Google Drive
Comment 5 Your output may look slightly different depending on what files and folders are in your
home directory. This is what mine shows.
Some of these folders should look familiar, like Desktop and Documents. We want to move to the
Downloads folder. To do that, we’ll use the cd command (for “change directory”).
> pwd
/Users/wilsonkl
> cd Downloads
> pwd
/Users/wilsonkl/Downloads
> ls
turtle_lab.py
Now that we are here in the Downloads folder where our code is, we can tell python to run it by typing
> python turtle_lab.py
Exercise 2 Follow the directions above to download a copy of turtle lab.py, open a command line,
navigate to the Downloads folder, and run the code.
Do you see an image pop up that looks like the following?
2 of 4
CSI 100 Turtle Graphics Lab
Comment 6 There are many other handy commands for moving around on the command line. In
particular, cd .. (two dots) moves up one directory, and cd (with nothing after it) takes you back to
your home directory.
3 Working with turtles
Imagine a small invisible turtle with a pen in its mouth. In turtle graphics, we draw pictures by giving
a turtle directions: “go forwards”, “turn left”, “turn right”. The instructions in turtle lab.py have the
turtle first go forwards 100 units, then turn left by 45 degrees, and then go forwards another 200 units.
t.forward(100)
t.left(45)
t.forward(200)
The three basic turtle commands are forward, left, and right. Using just combinations of these, you can
make the turtle draw some very complicated things.
Exercise 3 Which direction is the turtle initially facing, before it starts moving?
Exercise 4 Change the code in turtle lab.py to make the turtle draw a square. How would you do
an equilateral triangle, or a hexagon?
4 Your turn
Now it’s your turn to experiment! For some inspiration, here is a cool picture made with turtle graphics:
Figure 1: A turtle drawing made by repeatedly moving and turning.
Also, here are some other commands that you can give to your turtles:
3 of 4
CSI 100 Turtle Graphics Lab
penup() Tell the turtle to lift up it’s pen. It will still move as usual, but will not leave a mark.
pendown() Tell the turtle to put it’s pen down on the paper, so that it leaves a mark. The pen starts down
unless you lift it.
pencolor(’red’) Change the color of line that the turtle draws. Other colors work too, such as ’black’,
’blue’, ’magenta’, and many others.
pensize(10) Change the thickness of the line that the turtle draws. The default is 10, so pensize(12)
draws a slightly thicker line.
Repeating. There is a way to repeat the same commands many times without lots of copying and pasting.
Here is an example:
for i in range(10):
t.forward(20)
t.left(20)
This will cause turtle t to go forward 20 steps, and the turn left 20 degrees, and to repeat those actions a
total of 10 times. Repeating code like this is called looping.
Comment 7 This looping code has one line that is not indented, and then two lines that are. Inden-
tation is important in python, and we will talk about what it means in detail later in the course. For
now, know that indentation should be exactly four spaces, and that only indented lines get repeated by
this loop.
Exercise 5 (Turn in) Now it’s your turn! Draw a cool picture with your turtle. Take a screenshot
and upload it (as a group) to canvas.