4. Python Turtle Graphics

Our first turtle program

import turtle        # use the turtles library
wn = turtle.Screen() # create a graphics window
li = turtle.Turtle() # create a turtle named li
li.forward(150)      # move forward by 150 units
li.left(90)          # turn left by 90 degrees
li.forward(75)       # other side of a rectangle
In [1]:
from IPython.display import IFrame
IFrame('http://162.105.175.204:8000/runestone/static/thinkcspy/Workspace/index.html', width='100%', height=450)
Out[1]:

Exercise:

TurtleL4

Exercise:

TurtleL4

Exercise:

TurtleL4

Color & Pensize

import turtle

wn = turtle.Screen()
wn.bgcolor("blue")

alex = turtle.Turtle()
alex.color("white")
alex.pensize(10)
In [2]:
from IPython.display import IFrame
IFrame('http://162.105.175.204:8000/runestone/static/thinkcspy/Workspace/index.html', width='100%', height=450)
Out[2]:

Exercise

TurtleL4

Exercise

TurtleL4

Instances — A Herd of Turtles

import turtle
wn = turtle.Screen() 
wn.bgcolor("lightgreen")

tess = turtle.Turtle() 
tess.color("hotpink")
tess.pensize(5)
tess.forward(80)

alex = turtle.Turtle()
alex.backward(100)

wn.exitonclick()

Exercise

TurtleL4

The for Loop

Exercise

Draw a square

In [3]:
from IPython.display import IFrame
IFrame('http://162.105.175.204:8000/runestone/static/thinkcspy/Workspace/index.html', width='100%', height=450)
Out[3]:
import turtle

tess = turtle.Turtle()
tess.forward(80) 
tess.left(90)
tess.forward(80)
tess.left(90)
tess.forward(80)
tess.left(90)  
tess.forward(80)

for statement

In [4]:
for name in ["Joe", "Amy", "Brad", "Anlina"]:
    print("Hi", name)  # Note the indent
print("Hi", name)  # Note the indent
Hi Joe
Hi Amy
Hi Brad
Hi Anlina
Hi Anlina

Execution flow of for Loop

Iteration Helps

import turtle
alex = turtle.Turtle()

for i in [0, 1, 2, 3]:  # repeat four times
    alex.forward(80)    # indent
    alex.left(90)       # indent

Another version

import turtle
alex = turtle.Turtle()

for i in ["yellow", "red", "purple", "blue"]: 
    alex.forward(80)
    alex.left(90)

Change line color

import turtle
alex = turtle.Turtle()

for aColor in ["yellow", "red", "purple", "blue"]:
   alex.color(aColor)
   alex.forward(80)
   alex.left(90)

List

alist = ["yellow", "red", "purple", "blue"]

a_len = len(alist)

item1 = alist[0]
aitem3 = alist[3]
In [5]:
from IPython.display import IFrame
IFrame('http://162.105.175.204:8000/runestone/static/thinkcspy/Workspace/index.html', width='100%', height=350)
Out[5]:

Exercise

TurtleTriangle

In [6]:
from IPython.display import IFrame
IFrame('http://162.105.175.204:8000/runestone/static/thinkcspy/Workspace/index.html', width='100%', height=350)
Out[6]:

Exercise

1. In the following code, how many lines does this code print?
for number in [5, 4, 3, 2, 1, 0]:
    print("I have", number, "cookies.  I'm going to eat one.")
2. How does python know what lines are contained in the loop body?

(A) They are indented to the same degree from the loop header.
(B) There is always exactly one line in the loop body.
(C) The loop body ends with a semi-colon (;) which is not shown in the code above.
3. In the following code, what is the value of number the second time Python executes the loop?
for number in [5, 4, 3, 2, 1, 0]:
    print("I have", number, "cookies.  I'm going to eat one.")
4. Consider the following code:
for aColor in ["yellow", "red", "green", "blue"]:
   alex.forward(50)
   alex.left(90)
What does each iteration through the loop do?
(A) Draw a square using the same color for each side.
(B) Draw a square using a different color for each side.
(C) Draw one side of a square.

The range Function

for i in [0,1,2,3]:
    # # Executes the body with i = 0, then 1, then 2, then 3

for i in range(4):
    # Executes the body with i = 0, then 1, then 2, then 3
Computer scientists like to count from 0!
In [7]:
for x in range(4):
    print(x)
0
1
2
3
import turtle
alex = turtle.Turtle()

for i in range(4): 
    alex.forward(80)
    alex.left(90)
In [8]:
print(type(range(1)))
print(range(2))
print(list(range(2)))

# in python2.7, range(1) is a list
<class 'range'>
range(0, 2)
[0, 1]

help(range)

range(stop) -> range object
range(start, stop[, step]) -> range object

Return an object that produces a sequence of integers 
from start (inclusive) to stop (exclusive) by step.

range(start, stop, step)

In [7]:
print(list(range(4)))
print(list(range(1, 5)))
print(list(range(0, 19, 2)))
print(list(range(0, 20, 2)))
print(list(range(10, 0, -1)))
[0, 1, 2, 3]
[1, 2, 3, 4]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Exercise

1. In the command range(3, 10, 2), what does the second argument (10) specify?
(A) Range should generate a list that stops at 9 (including 9).
(B) Range should generate a list that starts at 10 (including 10).
(C) Range should generate a list starting at 3 that stops at 10 (including 10).
(D) Range should generate a list using every 10th number between the start and the stopping number.
2. What command correctly generates the list [2, 5, 8]?
(A) range(2, 5, 8)
(B) range(2, 8, 3)
(C) range(2, 10, 3)
(D) range(8, 1, -3)

More turtle Methods

A turtle’s pen can be picked up or put down

alex.up()
alex.forward(100)     # this moves alex, but no line is drawn
alex.down()

Every turtle can have its own shape

Shapes: arrow, blank, circle, classic, square, triangle, turtle

alex.shape("turtle")

Speed up or slow down

alex.speed(10)

Stamp

A turtle can “stamp” its footprint onto the canvas, and this will remain after the turtle has moved somewhere else.

Stamping works even when the pen is up.

import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
tess.shape("turtle")

tess.up()                     
for size in range(5, 60, 2):  
    tess.stamp()              
    tess.forward(size)        
    tess.right(24) 

wn.exitonclick()

Exercise 1

Write a program to draw something like this:

TurtleCircle

Exercise 2

Use for loops to make a turtle draw a regular octagon (eight sides)

For a regular convex n-gon, each interior angle has a measure of:

$ (n-2) \times \frac{180^\circ}{n}$

Exercise 3

Run the following code, observe output:

import turtle
t=turtle.Turtle()
for x in range(1,100,2):
    t.forward(x)
    t.left(90)

Exercise 4

Run the following code, observe output:

import turtle
t=turtle.Turtle()
for x in range(1,100,2):
    t.forward(x)
    t.left(91)
In [ ]: