TURTLE GRAPHICS IN PYTHON
“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
Turtle() None Creates and returns a new turtle object
forward() amount Moves the turtle forward by the specified amount
backward(
amount Moves the turtle backward by the specified amount
)
right() angle Turns the turtle clockwise
left() angle Turns the turtle counterclockwise
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
color() Color name Changes the color of the turtle’s pen
fillcolor() Color name Changes the color of the turtle will use to fill a polygon
stamp() None Leaves an impression of a turtle shape at the current location
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:
1. Import the turtle module
2. Create a turtle to control.
3. Draw around using the turtle methods.
4. Run turtle.done().
Page 1 of 6
TURTLE GRAPHICS IN PYTHON
PROGRAMS
To execute the program : https://pythonsandbox.com/turtle
(1)
# Python program to draw square
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()
(2) # Python program to draw star
import turtle
star = turtle.Turtle()
star.right(75)
star.forward(100)
for i in range(4):
star.right(144)
star.forward(100)
turtle.done()
(3) # Python program to draw hexagon
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()
(4) #program - parallelogram
Page 2 of 6
TURTLE GRAPHICS IN PYTHON
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)
(5)
import turtle
t = turtle.Turtle()
t.speed(1)
for x in range(6):
t.circle(100)
t.left(60)
(6)
import turtle
# Set up the turtle screen and set the background color to white
screen = turtle.Screen()
screen.bgcolor("white")
# Create a new turtle and set its speed to the fastest possible
pen = turtle.Turtle()
pen.speed(1)
pen.fillcolor("magenta") # Set the fill color to red
pen.begin_fill()
pen.circle(100) # Draw the circle with a radius of 100 pixels
pen.end_fill()# End the fill and stop drawing
pen.hideturtle()
turtle.done()# Keep the turtle window open until it is manually closed
7)
import turtle
t = turtle.Turtle()
t.speed(0)
n = int(input("Enter Number of circles"))# Enter n=35
for x in range(n):
t.circle(100)
t.left(360/n)
Page 3 of 6
TURTLE GRAPHICS IN PYTHON
8) Spiral Helix Pattern
# Python program to draw # Spiral Helix Pattern
import turtle
t = turtle.Turtle()
t.speed(0)
for i in range(10):
t.circle(5*i)
t.circle(-5*i)
t.left(i)
9) SquareSpiral1.py
# SquareSpiral1.py - Draws a square spiral
import turtle
t = turtle.Turtle()
t.speed(0)
for x in range(100):
t.forward(x)
t.left(90)
10) SquareSpiral2.py
import turtle
t = turtle.Turtle()
t.speed(0)
for x in range(100):
t.forward(x)
t.left(91)
11) CircleSpiral1.py
import turtle
t = turtle.Turtle()
t.speed(0)
for x in range(100):
t.circle(x)
t.left(91)
12) CircleSpiral2.py
import turtle
Page 4 of 6
TURTLE GRAPHICS IN PYTHON
t = turtle.Turtle()
t.speed(0)
colors = ["red", "yellow", "blue", "green"]
for x in range(100):
t.pencolor(colors[x%4])
t.circle(x)
t.left(91)
Extended Learning
(1) SpiralMyName.py
# SpiralMyName.py - prints a colorful spiral of the user's name
import turtle # Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]
# Ask the user's name using turtle's textinput pop-up window
your_name = turtle.textinput("Enter your name", "What is your name?")
# Draw a spiral of the name on the screen, written 100 times
for x in range(100):
t.pencolor(colors[x%4]) # Rotate through the four colors
t.penup() # Don't draw the regular spiral lines
t.forward(x*4) # Just move the turtle on the screen
t.pendown() # Write the user's name, bigger each time
t.write(your_name, font = ("Arial", int( (x + 4) / 4), "bold") )
t.left(92) # Turn left, just as in our other spirals
PROJECT
Design a Program to create the following
Page 5 of 6
TURTLE GRAPHICS IN PYTHON
Page 6 of 6