INTRODUCTION TO
MATH WITH PYTHON TURTLE
NR Computer Learning Center PHS Tech
1835 W. Orangewood, Suite Club
200 Portola High
Orange, CA 92868 School
(714) 505-3475 1001 Cadence
www.nrclc.com Irvine, CA
OBJECTIVE
Angle
Circle
Variable
PYTHON
An easy-to-learn programming language
Code is quite easy to read
Python has features that allow you to put
together simple animations for creating your own
games.
https://trinket.io/python
PYTHON TURTLE FUNCTION FORWARD()
Forward(distance) - Move the turtle forward by the specified distance, in
the direction the turtle is headed.
Example
t.forward(100) # move 100 steps forward
ANGLE
ANGLE
Angles are a measure of turn and are measured in
degrees.
Right Angle – Equal to 90o
Straight Angle – equal to 180o
Straight line – equal to 360o
PYTHON TURTLE FUNCTION LEFT() &
RIGHT()
Left(angle) - Turn turtle left by angle units.
Right(angle) - Turn turtle right by angle
units.
Example
t.forward(100) # move 100 steps forward
t.left(90) # turn 90 degrees to the left
t.forward(100) # move 100 steps forward
t.right(90) # turn 90 degrees to the right
t.forward(100) # move 100 steps
forward
EXERCISE 1: USING 900 ANGLE
import turtle
t=
turtle.Turtle()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
EXERCISE 2: USING 650 ANGLE
import turtle
t=
turtle.Turtle()
t.forward(50)
t.left(60)
t.forward(50)
t.left(60)
t.forward(50)
t.left(60)
t.forward(50)
t.left(60)
t.forward(50)
t.left(60)
t.forward(50)
t.left(60)
EXERCISE3: USING 450 ANGLE
import turtle
t=
turtle.Turtle()
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
t.forward(50)
t.left(45)
CIRCLE
PYTHON FUNCTION: CIRCLE()
turtle.circle(radius, extent, steps)
radius - Draw a circle with given radius. The center is radius units left of the
turtle;
extent (optional)– an angle – determines which part of the circle is drawn. If
extent is not given, draw the entire circle. If extent is not a full circle, one
endpoint of the arc is the current pen position. Draw the arc in counterclockwise
direction if radius is positive, otherwise in clockwise direction. Finally the direction
of the turtle is changed by the amount of extent.
Steps (optional)- determines the number of steps to use.
Example
turtle.circle(50) # draw full circle
turtle.circle(120, 180) # draw a semicircle
EXERCISE 4: USING VARIABLE
import turtle
t=
turtle.Turtle()
t.circle(5)
t.circle(10)
t.circle(15)
t.circle(20)
t.circle(40)
t.circle(60)
VARIABLE
VARIABLE
A variable is a quantity that may change within
the context of a mathematical problem or
experiment.
Typically, we use a single letter to represent a
variable.
For example:
2 x + 5 y = 10
EXERCISE 5: USING VARIABLE
import turtle
t=
r=5 # radius =
turtle.Turtle()
t.circle(r) 5
t.circle(r*2)
t.circle(r*3)
r = 20 # radius =
t.circle(r) 20
t.circle(r*2)
t.circle(r*3)
SHAPE(), PENUP(), PENDOWN()
PYTHON FUNCTION SHAPE(), PENUP(),
PENDOWN()
Shape(shapename) – Shapename can be ‘arrow’,
‘classic’, ‘turtle’ or ‘circle’
Penup() and Pendown() commands are used to move
around the screen
Pencolor(color) – Sets the color of the pen.
Pensize(size) – Sets the thickness of the pen.
EXERCISE 6: PENUP() & PENDOWN()
import turtle
t =
turtle.Turtle()
t.shape("turtle")
t.stamp()
t.penup()
t.forward(30)
t.pendown()
t.stamp()
t.penup()
t.forward(30)
t.pendown()
t.stamp()
PROJECT
OLYMPICS
import turtle
t=
turtle.Turtle("turtle")
t.pensize(10)
t.penup()
t.setposition(-120, 60)
t.pendown()
t.color("blue")
t.circle(50)
t.penup()
t.setposition(-60, 0)
t.pendown()
t.color("yellow")
t.penup()
t.setposition(0, 60)
t.pendown()
t.color("black")
t.circle(50)
t.penup()
t.setposition(60, 0)
t.pendown()
t.color("green")
t.circle(50)
t.penup()
t.setposition(120,60)
t.pendown()
t.color("red")
t.circle(50)
CONDITIONAL STATEMENT
CONDITIONAL STATEMENT
Conditional statements are used to check conditions and change the behavior
of the program accordingly.
Syntax:
if EXPRESSION:
STATEMENTS
The colon (:) is significant and required. It separates the header of the
compound statement from the body.
The line after the colon must be indented (use four spaces for indenting).
All lines indented the same amount after the colon will be executed whenever
the EXPRESSION is true.
CONDITIONAL OPERATOR
Meaning Python Symbols
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Equal to ==
Not equal to !=
EXERCISE 7: EXAMPLE – IF .. ELIF .. ELSE..
import turtle
t = turtle.Turtle()
t.shape("turtle")
points = input(“Enter Points between 0 and 100: ")
Points = int (points)
if Points >= 90 and Points < 100:
t.left(90)
elif Points >= 80 and Points < 90: t.right(90)
else:
t.left(0) t.forward(100)
NR Computer Learning Center
1835 W. Orangewood, Suite
200
Orange, CA 92868
(714) 505-3475
www.nrclc.com