[go: up one dir, main page]

0% found this document useful (0 votes)
19 views14 pages

02 Two

Python

Uploaded by

Shahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

02 Two

Python

Uploaded by

Shahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Intro to

working with
Objects in
Python

Understanding the basics of Objects


What is Turtle?

Turtle is a Python Module, Which allows us


to draw and create art
Why are we doing this Module?

Although this seems like something for


kids, Turtle Module is simply a hidden
gem for learning OOP.

This Module will give you a strong


understanding of how Objects work in
Programming and how we can use them
throughout out code
What is Turtle?

We have 9 objects, all


of which are Turtles
Every Turtle has its own properties, such as
Color or Speed.

They all have different actions that can do


different tasks

What are Methods &


Properties?
Methods and Properties:

A Method is a Function, specifically it is a function in a Class


A Property is a Variable, specifically it is a variable in a Class

Properties (Variables) Methods (Functions)

owl.color = "maroon" owl.fly()

parrot.speed = 5 parrot.eat()

canary.width = 2 canary.speak()

A Property & Method must be linked to an object to work


Methods and Properties:

A Method is a Function, specifically it is a function in a Class


A Property is a Variable, specifically it is a variable in a Class

Properties (Variables) Methods (Functions)

owl.color = "maroon" owl.fly()

parrot.speed = 5 parrot.eat()

canary.width = 2 canary.speak()

A Property & Method must be linked to an object to work


Setting up your code:
from turtle import * Final Output

t1 = Turtle()
t2 = Turtle()

t1.color("orange")
t2.color("purple")

t1.forward(50)
t1.left(90)

t2.left(90)
t2.forward(50)

done()
Setting up your code:
from turtle import * Importing everything from
the Turtle Module

t1 = Turtle()
Creating two Objects
t2 = Turtle()
Final Output
t1.color("orange")
Assigning the a Color
t2.color("purple") Property to each object

t1.forward(50) Linking two methods to


t1.left(90) object one to move

t2.left(90) Linking two methods to


t2.forward(50) object two to move

done() Allows the drawing to stay


on the screen once done
Methods and properties of the Turtle Module:
Property / Method What they do

.color("green") Assigns a Color to your object, the color is a string

.width(5) Assigns a width to the Line

Assigns a shape to the object (arrow, circle, square,


.shape("circle") triangle, turtle)

.speed(10) Assigns a Speed to the object

A method to move your object forward a number


.fd(100) of pixels

.left(90) or .right(45) A method to turn your object, a number of degrees


Methods and properties of the Turtle Module:
Property / Method What they do

.up() Lifts your object off the screen

.goto(x, y) Moves your object to a new location (x , y)

.down() Places your object back down once moved

.circle(10) Used to draw a circle (takes a radius)

Allows you to fill in your drawing with a solid


.begin_fill() or .end_fill() color

Used to keep the drawing displayed once


.done() complete
Use Turtle with the basics of Python:
from turtle import * You can use your previous knowledge
to expand with turtle, this includes:
t1 = Turtle()
Using For Loops to repeat
t1.color("orange")
Using Conditional Statements
t1.width(5)
Creating Functions
t1.begin_fill()
for i in range(5):
t1.forward(150)
t1.left(144)
t1.end_fill()

done()
Use Turtle with the basics of Python:
from turtle import * You can use your previous knowledge
to expand with turtle, this includes:
t1 = Turtle()
Using For Loops to repeat
t1.color("orange")
Using Conditional Statements
t1.width(5)
Creating Functions
t1.begin_fill()
for i in range(5):
t1.forward(150)
t1.left(144)
t1.end_fill()

done()
Additional Code Examples:
from turtle import * t = Turtle()
ask = input("Enter shape: ")
def star(t,width,size,color): while ask != "done":
t.color(color) if ask == 'star':
t.width(width) width = int(input("Enter width: "))
t.begin_fill() col = input("Enter a color: ")
for i in range(5): size = int(input("Enter a length: "))
t.forward(size) star(t,width,size,col)
t.left(144) elif ask == 'circle':
t.end_fill() radius = int(input("Enter a radius: "))
col = input("Enter a color: ")
def circle(t,radius,color): circle(t,radius,col)
t.color(color) else:
t.begin_fill() print("No shape entered")
t.circle(radius) ask = input("Enter shape: ")
t.end_fill() done()
Additional Code Examples:
from turtle import * t = Turtle()
ask = input("Enter shape: ")
def star(t,width,size,color): while ask != "done":
t.color(color) if ask == 'star':
t.width(width) width = int(input("Enter width: "))
t.begin_fill() col = input("Enter a color: ")
for i in range(5): size = int(input("Enter a length: "))
t.forward(size) star(t,width,size,col)
t.left(144) elif ask == 'circle':
t.end_fill() radius = int(input("Enter a radius: "))
col = input("Enter a color: ")
def circle(t,radius,color): circle(t,radius,col)
t.color(color) else:
t.begin_fill() print("No shape entered")
t.circle(radius) ask = input("Enter shape: ")
t.end_fill() done()
Code Challenges:

1. Draw a hollow pentagon, each side a different color

2. Draw 4 solid squares all different colors in different locations

3. Create 3 functions that'll draw something different when called


(similar to the previous example)

4. Draw 10 circles, each at a random location with a random


radius
(hint -> Use the Python random module, randint()

5. Create 3 objects, that'll draw 3 triangles in a column, all a


different color Turtle Graphics Module Documentation

You might also like