Turtles
assign to alex “The Turtle type that is defined within the turtle module”
turtle starts off facing east when it is created (it is just part of the turtle library)
Objects can have methods (things that they can do), and properties (things they are):
move_forward - m
jump - m
color - p
age - p
Instances
Each instance has own properties, and methods.
For Loop
A basic building block of all programs is to be able to repeat some code over and over again. In computer science, we refer to this repetitive idea as iteration.
In Python, the for statement allows us to write programs that implement iteration.
name in this
for
statement is called the loop variable, it is being updated after each runlist of names
line 2 is the loop body !!! ALWAYS INDENTED
terminating condition - if the conditions to iterate again are over, it is called t.c
control flow aka. flow of execution of the program/loop is the name for python to know where in the loop we are at any given moment
sequential control flow == linear
Iteration simplifies programs
Benefits:
simplified program - easier to understand
found patterns of behaviour that can be reused
The range values, had nothing to do whith the loop number, anything could be inside:
would work the same, plus the change of the color.
range
function
range
functionTurtle specific methods
Every turtle can have its own shape. The ones available “out of the box” are arrow
, blank
, circle
, classic
, square
, triangle
, turtle
.
Method
Parameters
Description
Turtle
None
Creates and returns a new turtle object
forward
distance
Moves the turtle forward
backward
distance
Moves the turle backward
right
angle
Turns the turtle clockwise
left
angle
Turns the turtle counter clockwise
up
None
Picks up the turtles tail
down
None
Puts down the turtles tail
color
color name
Changes the color of the turtle’s tail
fillcolor
color name
Changes the color of the turtle will use to fill a polygon
heading
None
Returns the current heading
position
None
Returns the current position
goto
x,y
Move the turtle to position x,y
begin_fill
None
Remember the starting point for a filled polygon
end_fill
None
Close the polygon and fill with the current fill color
dot
None
Leave a dot at the current position
stamp
None
Leaves an impression of a turtle shape at the current location
shape
shapename
Should be ‘arrow’, ‘classic’, ‘turtle’, or ‘circle’
Modules
A module is a file containing Python definitions and statements intended for use in other Python programs. There are many Python modules that come with Python as part of the standard library.
Global module index to find things
The math
and random
modules
math
and random
modulesThe math
module contains the kinds of mathematical functions you would typically find on your calculator and some mathematical constants like pi and e. As we noted above, when we import math
, we create a reference to a module object that contains these elements.
The random module:
random.random()
random.randrange()
pseudo-random since they depend on seed value
Last updated
Was this helpful?