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
.
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