Python Coding
TURTLE MODULE
CONCEPT OVERVIEW
Coding Concept Sub-Concepts
● Importing the turtle module
● Turtle window setting - size(), title(), shape(),
bgcolor(), showturtle(), hideturtle()
● Turtle basic motions methods- forward(),
Python Turtle backward(), left(), right(), circle()
Module ● Turtle pen control methods - pencolor(),
pensize(), pendown(), penup(), speed(), goto(),
setpos(), setposition()
● Colour control - begin_fill(), end_fill()
● Text method - write()
OBJECTIVES
● Define Python turtle with examples.
● Import the turtle module in Python.
● Manipulate turtle window using setup(), bgcolor(), title() and shape() methods.
● Use motion methods in turtle programs to control the movement and create basic
shapes.
● Add colours to shapes and drawings using different colour methods.
● Insert text in turtle drawings using text() method.
● Create basic graphics or drawings using the turtle module.
INTRODUCTION TO TURTLE MODULE
Turtle is a Python library that is used to create graphics.
The onscreen pen that you use for drawing is called the turtle.
The turtle can draw lines, and shapes and also change colours,
sizes and shapes.
The turtle can also be used to create complex shapes and images
using loops and functions.
IMPORTANCE OF TURTLE MODULE
Helps beginners understand coding basics like
Easy Start loops and variables.
See Results You can see what your code does instantly.
Be Creative Make cool drawings and simple games.
Solve Problems Practice thinking logically and finding solutions.
Learn Coordinates Understand how x and y coordinates work.
Try GUIs Get ready for making interactive programs.
IMPORTING TURTLE MODULE
Before we can use turtle, we need to import it. The following are commonly used
methods to import the turtle module in Python:
1 2
Import
Direct import
Everything
Using the import turtle Using the from turtle import *
statement - this imports the statement - this imports all of the
entire turtle module and allows functions and classes from the turtle
you to use all of its functions module into the current namespace,
and classes. so you don't have to use the turtle
prefix when calling them.
EXAMPLES OF IMPORTING TURTLE MODULE
To draw a rectangle we can write the following codes in Python Turtle. In the first
example we use import turtle, and in the second case we use from turtle import
* .
1 2
import turtle from turtle import *
Tk=[Link]() for x in range(4)
for x in range(4) forward(200)
[Link](200) right(90)
[Link](90) done()
[Link]()
TURTLE METHODS
● In Turtle graphics, a virtual turtle moves around the screen, and
programmers can control its movements and drawings using specific
methods.
● The turtle can also be configured to change its colour, shape, and size
using various methods.
Turtle Methods
Window Basic Colour
Pen control Text method
setting motions control
ACTIVITIES
1 1. Understanding the
Different Window Settings
Methods
WINDOW SETTING
2. Demo - Window Setting
3. Activity 1 - Window Setting
TURTLE WINDOW
SETTINGS
Turtles live in a grid world guided
by an x-axis and y-axis. Turtle’s
home is at the very center of the
canvas, at coordinate point (0,0).
In the canva shown alongside the
x-axis goes from -200 to +200 and
the y-axis also goes from -200 to
200.
TURTLE WINDOW SETTINGS
We can apply the following methods to set turtle window.
Sl Methods Purpose
#
1 setup(x, y) To set the size of the canva
2 title(" ") To set the windows title
To change the turtle graphics object to one of the
3 shape("turtle") built-in shapes( "turtle", "arrow", "circle", or
"square")
bgcolor("color
4 To change the background color
")
5 showturtle() To make the turtle cursor visible on screen
6 hideturtle() To make the turtle cursor invisible on screen
DEMO - WINDOWS SETTING
Write a Python program to change the
turtle window’s settings as follows:
● Set the window size to 300x250 pixels.
● Create a program that changes the
background colour to yellow.
● Give the window title "Colorful Turtle
Canvas."
A sample window is provided alongside.
DEMO - SOLUTION
Code
#import the turtle module
1
from turtle import *
2
# Set the background color of the window to yellow
3
bgcolor("yellow")
4
#Set the window size to 300x250 pixels
5
setup(350,250)
6
# Set the title of the Turtle window
7
8
title("Colorful Turtle Canvas")
ACTIVITY 1 - WINDOWS SETTING
Write a Python program to create a turtle
windows with the following settings:
1. Change the shape of the turtle to
“square”.
2. Change the window size height=400 and
width=350
3. Change the title to “This is a title to the
window!”.
4. Change the background colour to “pink”.
ACTIVITY 1 - SOLUTION
Code
1 #import the turtle module
2 from turtle import *
3 # Create a Turtle object with the shape "square"
4 shape("square")
5 #Change the window size height=400 and width=350
6 setup(height=400,width=350)
7 # Set the title of the Turtle window
8 title("This is a title to the window!")
9 # Set the background color of the window to pink
10 bgcolor("pink")
ACTIVITIES
1. Understanding Turtle Motions
and Motion Methods
2
2. Demo 1 - Drawing a Line
3. Activity 1 - Drawing a Circle
BASIC MOTIONS
4. Demo 2 - Drawing an Arc
5. Activity 2- Drawing a Square
6. Activity 3 - Drawing an Ice
Cream
TURTLE BASIC MOTIONS
To draw something on the turtle canvas, we need to move the turtle. Following
methods are used to apply motion to the turtle.
Sl# Methods Purpose
1 forward(distance)
To move the turtle forward a specified
distance.
2 backward(distance) To move the turtle backward a specified
distance.
3 right(angle) To turn the turtle 90 degrees to the left.
4 left(angle) To turn the turtle 90 degrees to the right.
5 circle(radius) To draw a circle.
EXAMPLE 1 - TURNING LEFT/RIGHT
Write a Python program using the turtle module to turn the turtle left and right.
Code
1 #turtle program to turn the turtle left and then right
2 from turtle import *
3 title(“Turning left and right”)
4 left(90)
5 forward(100)
6 right(90)
7 forward(100
DEMO 1 - DRAWING A LINE
Write a Python program using the turtle module to move 100
pixels forward and then move 100 pixels backward.
Code
1 #turtle program to draw the moving forward and backward
2 from turtle import *
3 title("Moving forward and backward")
4 forward(100) Output
5 backward(100)
DRAWING A CIRCLE
The circle method in Python turtle can be used to draw a circle or an
arc.
Syntax → circle(radius, extent=None, steps=None)
Radius of the
circle Divide the shape in
The part of the circle the equal number of
in degrees as an arc given steps.
ACTIVITY 1 - DRAWING A CIRCLE
Write a Python program using the turtle module to draw two
circles as shown below.
CODE
1 #Turtle program to draw the circles
2 from turtle import *
3 title(“Drawing circles”)
4 circle(50)
5 forward(100)
6 circle(50)
DEMO 2 - DRAWING AN ARC
Write a Python program using the turtle module to draw a semi
circle
with a radius of 50 pixel. Hint: use extent parameter in circle
CODE OUTPUT
1 from turtle import *
2 pensize(4)
3 circle(100, extent=180)
4 hideturtle()
CODE
ACTIVITY 2 - DRAWING A SQUARE
1 #drawing the square
Write a Python program using the turtle 2 from turtle import *
module to draw a square with side lengths of 3 forward(200)
200 pixels. 4 right(90)
5 forward(200)
6 right(90)
7 forward(200)
8 right(90)
9 forward(200)
OUTPUT → 10 right(90)
ACTIVITY 3 - DRAWING AN ICE CREAM
CODE
Write a Python program using the turtle
module to draw an ice cream as shown in 1 from turtle import *
the example given below. 2 pensize(4)
3 left(90)
4 circle(100,180)
5 pensize(10)
6 goto(0,0)
OUTPUT 7 right(45)
→ 8 goto(-100,-200)
9 goto(-200,0)
10 done()
ACTIVITIES
1. Understanding the use of Pen
Controls and Pen Control
3 Methods
2. Activity 1 - Identifying the
Methods
PEN CONTROL 3. Demo 1 - Draw a House
4. Activity 2 - Drawing Flag
Boundary
5. Activity 3 - Drawing Olympic
Ring
TURTLE PEN CONTROL
Turtle Pen Control methods provide control over the drawings on the turtle canvas.
These methods allow you to customize the appearance and behaviour of the pen used
by the turtle to draw on the canvas.
Sl# Methods Purpose
To set the turtle's pen down, so that it will draw a
1 pendown()
line as it moves.
To lift the turtle's pen up, so that it will not draw
2 penup()
a line as it moves.
TURTLE PEN CONTROL (CONT.)
SI# Command Purpose
To set the width of the turtle's pen. The
3 pensize(width)
width is specified in pixels.
4 pencolor(color) To set the color of the turtle's pen. *
To control the speed of the turtle. where
5 speed(speed)
speed is an integer or a string. **
goto(x, y) Move the turtle to the specified x, y
6 setpos(x,y) coordinates on the screen.
setposition(x,y)
ACTIVITY 1 - IDENTIFYING THE METHODS
Watch the following video or observe the image and list down the methods
used in the program.
ACTIVITY 1 - SOLUTION
The methods used in the program are as the
following:
bgcolor(“blanched almond
“)
pencolor(“red”)
pencolor(“blue”
pensize(6)
) pensize(3)
forward(100)
circle(100)
left(90)
penup()
moves the turtle
forward(100 →
to a new
)pendown()
position
DEMO 1 - DRAW A HOUSE
Write a Python program to draw a simple house as shown below.
DEMO 1 - SOLUTION
CODE
1 from turtle import * 11 left(90) 21 right(90)
2 pensize(5) 12 forward(150) 22 forward(80)
3 penup() 13 left(90) 23 left(90)
4 goto(-100,0) 14 penup() 24 penup()
5 pendown() 15 goto(-20,0) 25 goto(-130,150)
6 forward(200) 16 pendown() 26 pendown()
7 left(90) 17 left(90) 27 forward(260)
8 forward(150) 18 forward(80) 28 goto(0,200)
9 left(90) 19 right(90) 29 goto(-130,150)
10 forward(200) 20 forward(40) 30 penup()
ACTIVITY 2 - DRAWING FLAG BOUNDARY
Write a Python program to draw the boundary of the Bhutan Flag as
shown below. You will fill the colour after learning the colour fill methods.
ACTIVITY 2 - SOLUTION
CODE
1 from turtle import * 10 forward(300)
2 pensize(3) 11 right(146)
3 shape("turtle") 12 forward(360)
4 penup() 13 left(180)
5 goto(-200,0) 14 forward(360)
6 pendown() 15 right(124)
7 left(90) 16 forward(200)
8 forward(200) 17 right(90)
9 right(90) 18 forward(300)
ACTIVITY 3 - DRAWING OLYMPIC RING
Write a Python program using turtle module to draw an olympic logo as shown below.
ACTIVITY 3 - SOLUTION
CODE
1 from turtle import * 10 goto(0, -25) 19 penup()
2 pensize(5) 11 pendown() 20 goto(-55, -75)
3 color("blue") 12 circle(45) 21 pendown()
4 penup() 13 color("red") 22 circle(45)
5 goto(-110, -25) 14 penup() 23 color("green")
6 pendown() 15 goto(110, -25) 24 penup()
7 circle(45) 16 pendown() 25 goto(55, -75)
8 color("black") 17 circle(45) 26 pendown()
9 penup() 18 color("yellow") 27 circle(45)
ACTIVITIES
1. Understanding Color Control
Methods
4 2. Demo - Colouring a Circle
3. Activity 1 - Coloring a Circle
4. Activity 2 - Colouring Bhutan
COLOUR FILL
Flag
5. Activity 3 - Check Your
Understanding
6. Activity 4 - Drawing a YouTube
logo
PYTHON COLOUR FILLS
In Python turtle module, the colour fill methods are used to control the filling of shapes
drawn by the turtle. When you draw a closed shape, such as a polygon or a circle, you
can use colour fill methods to specify the colour that fills the interior of the shape.
Sl# Methods Purpose
Tells turtle to fill in any closed shapes that
1 begin_fill()
are drawn
Tells turtle to stop filling in closed shapes
2 end_fill()
that are drawn
DEMO - COLOURING A CIRCLE
Write a Python program using the turtle module to draw a circle filled with
purple colour.
OUTPUT
CODE
1 from turtle import *
2 shape("turtle")
3 begin_fill()
4 color("purple")
5 circle(50)
6 end_fill()
ACTIVITY 1 - COLOURING CIRCLES
Write a Python program in your notebook using the turtle module to
create four different circles as shown below. Then check the drawings on your
computer.
ACTIVITY 1 - SOLUTION
1 from turtle import * 16 penup()
2 pensize(5) 17 forward(100)
3 shape("turtle") 18 pendown()
4 penup() 19 color("purple", "orange")
5 goto(-150,0) 20 begin_fill()
6 pendown() 21 circle(40)
7 color("pink") 22 end_fill()
8 begin_fill() 23 penup()
9 circle(40) 24 forward(100)
10 end_fill() 25 pendown()
CODE
11 penup() 26 color("purple", "orange")
12 forward(100) 27 pensize(9)
13 pendown() 28 begin_fill()
14 color("blue") 29 circle(40)
15 circle(40) 30 end_fill()
ACTIVITY 2 - COLOURING BHUTAN FLAG
Write a Python program using the turtle module to draw the National flag of
Bhutan as shown below. What does each colour signify? What has to be added
to complete the flag?
ACTIVITY 2 - SOLUTION
11 left(90)
12 forward(200)
CODE 13 right(90)
14 forward(300)
1 from turtle import * 21 begin_fill()
15 right(146)
2 pensize(3) 22 left(180)
16 forward(360)
3 shape("turtle") 23 forward(360)
17 end_fill()
4 penup() 24 right(124)
18 fillcolor(”orange”)
5 goto(-200,0) 25 forward(200)
6 pendown() 26 right(90)
7 fillcolor(“yellow”) 27 forward(300)
8 begin_fill() 28 end_fill()
ACTIVITY 3 - CHECK YOUR UNDERSTANDING
Write the answers for the following questions in your notebook.:
1. What does the begin_fill() method in Python turtle module signify?
Ans: Marks the beginning of the colour fill
2. In the turtle module, what happens if you call end_fill() without first
calling begin_fill()?
Ans: If you forget to call begin_fill() before using end_fill(), the
turtle assumes that there is no shape to fill, and as a result, the
colour filling process doesn't take place.
3. What is the default colour used for filling shapes if fillcolor() is not
explicitly set? (Choose the correct answer from the option given)
a) Red b) Green c) Black
ACTIVITY 4 - DRAWING A YOUTUBE LOGO
Write a Python program using
the turtle module to draw a
YouTube logo as shown
alongside.
Hint: use the methods such as circle(), color(), begin_fill(), end_fill()
ACTIVITY 4 - SOLUTION
1 from turtle import * 13 forward(100)
2 speed(0) 14 circle(20,90)
3 pensize(5) 15 end_fill()
4 color("red") 16 penup()
5 goto(-100,0) 17 goto(20,70)
6 begin_fill() 18 pendown()
7 forward(160) 19 color("white")
8 circle(20,90) 20 begin_fill()
CODE
9 forward(100) 21 left(90)
10 circle(20,90) 22 circle(40,360,3)
11 forward(160) 23 end_fill()
12 circle(20,90) 24 hideturtle()
ACTIVITIES
1. Understanding Text Control
Methods
2. Demo - Labelling a Circle
5
3. Activity 1 - Colouring the
House
TEXT METHODS 4. Activity 2 - Drawing a Smiley
face
5. Activity 3 - Check Your
Understanding
6. Activity 4 - Drawing Sunset
TEXT METHODS
write() method in Python turtle module is used to write a text on the
turtle canvas.
EXAMPLE
Metho Alignment of the
d text
write(“Hello World!”,align="center",font=("Arial",16,"normal"))
String to Font
write
DEMO - LABELLING A CIRCLE
Write a Python program using the turtle module to insert the text ‘Circle’
as shown below.
DEMO - SOLUTION
CODE
1 from turtle import *
2 pencolor("red")
3 circle(100)
4 penup()
5 goto(0,100)
6 pendown()
7 color("blue")
8 write("Circle", align="center", font=("Georgia",16,"bold"))
ACTIVITY 1 - COLOURING THE HOUSE
Write a Python program to draw and colour a house as shown in
the image given below.
● Colour the roof green
● Colour the wall pink
● Colour the door brown
● Add a text “A house!” below the house
● Use the font “Georgia” and font size 22 for the text
ACTIVITY 1 - SOLUTION
6 fillcolor("pink")
CODE 7 begin_fill()
1 from turtle import * 8 forward(200)
2 pensize(5) 9 left(90)
3 penup() 10 forward(150) Set the
4 goto(-100,0) 11 left(90) color of
5 pendown() 12 forward(200) the wall,
13 left(90) fill and
14 forward(150) draw the
15 left(90)
wall.
16 end_fill()
Import the turtle module, 17 penup()
set the pensize, set the
position of the turtle
ACTIVITY 1 - SOLUTION (CONT.)
18 goto(-20,0)
19 pendown()
20 fillcolor("brown")
21 begin_fill()
Set the position to 22 left(90)
draw the door, select 23 forward(80)
the color to fill the 24 right(90)
door, then draw and 25 forward(40)
26 right(90)
fill the door
27 forward(80)
28 end_fill()
29 left(90)
30 penup()
ACTIVITY 1 - SOLUTION (CONT.)
31 goto(-130,150)
32 pendown()
33 fillcolor("green")
34 begin_fill()
35 forward(260) Set the position to
36 goto(0,200) draw the roof, select
37 goto(-130,150) the color to fill the
38 penup() roof, then draw and fill
39 end_fill() the roof.
40 penup() Add text ‘A house’
41 goto(0,-40) using write() method.
42 color("blue")
43 write("A house!",align="center",font=("Georgia",22))
ACTIVITY 2 - DRAWING A SMILEY FACE
Code
Write a Python program using the 1 from turtle import *
turtle module to draw a smiley 2 # Set up the turtle
face as shown below. 3 speed(2)
4 # Draw the face
5 penup()
6 fillcolor("yellow")
7 goto(0, -100)
8 pendown()
9 begin_fill()
10 circle(100)
11 end_fill()
ACTIVITY 2 - SOLUTION (CONT.)
12 # Draw the eyes 26 # Draw the smile
13 penup() 27 penup()
14 fillcolor("black") 28 goto(-50, -40)
15 goto(-30, 20) 29 pendown()
16 pendown() 30 right(60)
17 begin_fill() 31 circle(60, 120)
18 circle(15) 32 # Hide the turtle
19 end_fill() 33 hideturtle()
20 penup()
21 goto(30, 20)
22 pendown()
23 begin_fill()
Output →
24 circle(15)
25 end_fill()
ACTIVITY 3 - CHECK YOUR UNDERSTANDING
Write the answers for the following questions in your notebook.
1. How would you lift the pen in the turtle module to allow the turtle to move without
drawing?
a) raisepen() b) penup()
c) pen_up() d) lift_pen()
2. How can you set the turtle's speed to the maximum in the turtle module?
a) set_speed("fastest") b) speed("fastest")
c) max_speed() d) turbo_mode()
3. The size (width, height) method sets the title of the turtle graphics window.
(True/False)
ACTIVITY 3 - CHECK YOUR UNDERSTANDING(CONT.)
4. Which method sets the pen color in the Turtle module?
a) set_pen_color("color") b) pensize(width)
c) pencolor("color") d) set_color("color")
5. The method that sets the background color of the turtle graphics window is
bgcolor("_____"). (Fill in the blanks)
color
6. The forward(distance) method can be used to go backward by using a distance.
(True/False)
ACTIVITY 4 - DRAWING SUNSET
Observe the given art carefully and
recreate it in Python program using
the turtle module.
ACTIVITY 4 SOLUTION
Code 15 right(90)
1 from turtle import* 16 forward(200)
2 title("Sunset") 17 right(90)
3 shape("turtle") 18 forward(400)
4 bgcolor("cyan") 19 right(90)
5 begin_fill() 20 forward(200)
6 color("yellow") 21 right(90)
7 left(90) 22 forward(200)
8 circle(75, 180) 23 end_fill()
9 end_fill() 24 home()
10 home() 25 penup()
11 begin_fill() 26 goto(0,130)
12 color("blue") 27 pendown()
13 forward(200) 28 write("Sunset",font=("Impact",30))
14 forward(200)
Key
Points
● Turtle is a built-in module for drawing graphics in Python.
● There are two ways to import turtle: import turtle and from turtle import
*
● We can customize turtle window using setup(x, y), title(" "),
shape("turtle"), bgcolor("color"), showturtle(), hideturtle() methods.
● We can manage drawing on turtle with forward(), backward(), left(), and
right(), penup(), pendown(), and customize pen attributes.
● The write( ) method is used to insert text on the turtle canvas.
● The immediate visual feedback in turtle can make programming more
engaging and intuitive, especially for younger learners or those new to
THANK YOU