Professional Documents
Culture Documents
3.5 - OOP and Turtle
3.5 - OOP and Turtle
with Python
Using “turtle” graphics to introduce object
oriented
programming concepts in Python
What does a turtle have to do
with graphics?
What does a turtle have to do with graphics?
● A library of objects and methods that you can ● Object-oriented programming is a type of
use to draw programming language that focuses on
modeling programs around objects.
● Turtle is a class that contains all of the
variables and functions that describe a turtle ○ Characteristics(variables) and actions
● To use those functions and variables, we must (functions) in the same place
create a specific turtle (an instance)
● We can make as many turtles as we need to
● Classes are the special modules that contain
make our program work.
the code to describe these objects.
● We can create instances of these objects that
we can control and investigate
Create the
turtle object
from turtle import Turtle and give it a
tommy=Turtle() name/handle
tommy.forward(50)
tommy.right(90)
Controlling a Turtle tommy.forward(50)
tommy.right(90)
tommy.forward(50)
Each command has a name and a tommy.right(90)
lot of them have options or settings. tommy.forward(50)
tommy.right(90)
Iteration
}
tommy.forward(50) for i in range(4):
tommy.right(90) tommy.forward(50)
tommy.forward(50) tommy.right(90)
tommy.right(90)
tommy.forward(50) Do it 4 times
tommy.right(90)
tommy.forward(50)
tommy.right(90)
Furthering Iteration- Nested Loop
Outside
for i in range(10): loop
tommy.left(36)
for i in range(10): Inside loop
tommy.forward(50)
tommy.left(36)
Useful Commands
More info here on the official turtle graphics page
Including on how to change the background color
Basic Commands and the shape of the