EOP CS Component
Download
Report
Transcript EOP CS Component
Class 2
Introduction to turtle graphics
Today’s Topics
First steps
Turtle graphics
Python modules
Python help and documentation
for and range
def – creating a function
Computing is a social activity
If you’ve got a question, ask it (it’s part
of computing culture)
We’re working in pairs. It’s more fun,
more real and more productive.
Use the online help system and tutorial
Use the textbook
Ask a TA or tutor
Introduction to Turtle Graphics
Type ‘from turtle import *’ and press enter
This gives you access to the functions in Python’s turtle
module
A module is a collection of functions
‘*’ matches everything
Type:
pendown() and press enter
forward(100) and press enter
A new window pops up and a straight line will be
drawn
Why are the pendown() parens empty? What is the
‘100’ in the parens?
Introduction to Turtle Graphics
Some useful commands
forward(distance)
left(degrees)
right(degrees)
goto(xcoord, ycoord)
up() – (no drawing)
down() – (pen ‘down’ for drawing)
clear()
Look up the Turtle module in the Python docs
Python manuals > Global module index > Turtle (Tk)
Practice Drawing Shapes
Draw the following shapes using forward(),
left() or right()
Triangle, square or rectangle, pentagon,
dodecagon
Challenge problem 1: Draw a set of squares
rotated at different angles
Challenge problem 2: Draw a set of squares offset
from each other (hint: use ‘up’)
Write down the sequence of Python statements
you used before you exit (you’ll need them later)
Write your own function
Use def to create your own function
Put this function definition is a file
def square(size):
forward(size)
right(90)
forward(size)
right(90)
forward(size)
right(90)
forward(size)
right(90)
The for loop
Try this
>>> for i in 'cat':
print(i)
Now try this
>>> for i in range(3):
print('cat')
Use for in range() to improve your square
function