TURTLE PROGRAMMING
IN PYTHON
TURTLE
• “Turtle” is a Python feature like a drawing board, which lets us command
a turtle to draw all over it! We can use functions like turtle.forward(…)
and turtle.right(…) which can move the turtle around. Commonly used
turtle methods are :
Method Parameter Description
Creates and returns a new
Turtle() None
turtle object
Moves the turtle forward
forward() amount
by the specified amount
Moves the turtle backward
backward() amount
by the specified amount
right() angle Turns the turtle clockwise
Turns the turtle
left() angle
counterclockwise
Method Parameter Description
penup() None Picks up the turtle’s Pen
pendown() None Puts down the turtle’s Pen
up() None Picks up the turtle’s Pen
down() None Puts down the turtle’s Pen
Changes the color of the
color() Color name
turtle’s pen
Method Parameter Description
Changes the color of the
fillcolor() Color name
turtle will use to fill a polygon
heading() None Returns the current heading
position() None Returns the current position
Move the turtle to position
goto() x, y
x,y
Remember the starting point
begin_fill() None
for a filled polygon
Method Parameter Description
Close the polygon and fill
end_fill() None
with the current fill color
Leave the dot at the
dot() None
current position
Leaves an impression of a
stamp() None turtle shape at the current
location
Should be ‘arrow’,
shape() shapename
‘classic’, ‘turtle’ or ‘circle’
PLOTTING USING TURTLE
• To make use of the turtle methods and functionalities, we need to import
turtle.”turtle” comes packed with the standard Python package and need
not be installed externally. The roadmap for executing a turtle program
follows 4 steps:
• Import the turtle module
• Create a turtle to control.
• Draw around using the turtle methods.
• Run turtle.done().
turtle.done()
from turtle import *
# or
import turtle
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
skk.forward(100)
turtle.done()
EX.1
# Python program to draw square
# using Turtle Programming
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()
EX.2
# Python program to draw star
# using Turtle Programming
import turtle
star = turtle.Turtle()
star.right(75)
star.forward(100)
for i in range(4):
star.right(144)
star.forward(100)
turtle.done()
EX.3
EX.4
# Python program to draw hexagon
# using Turtle Programming
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle.done()
EX.5
import turtle
# Initialize the turtle
t = turtle.Turtle()
# Set the turtle's speed
t.speed(1)
# Draw the parallelogram
for i in range(2):
t.forward(100)
t.left(60)
t.forward(50)
t.left(120)
EX.6