དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development
Python Coding
January 2024
Class IX ICT Curriculum
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
TURTLE MODULE
Key Concept # 4
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Concept Overview
Coding Concept | Sub-Concepts |
Python Turtle Module |
|
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Objectives
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Importance of Turtle Module
Easy Start
Helps beginners understand coding basics like 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.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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:
Using the import turtle statement - this imports the entire turtle module and allows you to use all of its functions and classes.
1
Using the from turtle import * statement - this imports all of the functions and classes from the turtle module into the current namespace, so you don't have to use the turtle prefix when calling them.
2
Direct import
Import Everything
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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 * .
import turtle
Tk=turtle.Turtle()
for x in range(4)
Tk.forward(200)
Tk.right(90)
Tk.done()
1
from turtle import *
for x in range(4)
forward(200)
right(90)
done()
2
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Turtle Methods
Turtle Methods
Colour control
Text method
Window setting
Pen control
Basic motions
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
WINDOW SETTING
ACTIVITIES
1
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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 |
3 | shape("turtle") | To change the turtle graphics object to one of the built-in shapes( "turtle", "arrow", "circle", or "square") |
4 | bgcolor("color") | 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 |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo - Windows Setting
Write a Python program to change the turtle window’s settings as follows:
A sample window is provided alongside.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo - Solution
1 2 3 4 5 6 7 8 | #import the turtle module from turtle import * # Set the background color of the window to yellow bgcolor("yellow") #Set the window size to 300x250 pixels setup(350,250) # Set the title of the Turtle window title("Colorful Turtle Canvas") |
Code
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Windows Setting
Write a Python program to create a turtle windows with the following settings:
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Solution
1 2 3 4 5 6 7 8 9 10 | #import the turtle module from turtle import * # Create a Turtle object with the shape "square" shape("square") #Change the window size height=400 and width=350 setup(height=400,width=350) # Set the title of the Turtle window title("This is a title to the window!") # Set the background color of the window to pink bgcolor("pink") |
Code
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
BASIC MOTIONS
ACTIVITIES
2
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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. |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Example 1 - Turning Left/Right
Write a Python program using the turtle module to turn the turtle left and right.
1 2 3 4 5 6 7 | #turtle program to turn the turtle left and then right from turtle import * title(“Turning left and right”) left(90) forward(100) right(90) forward(100 |
Code
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo 1 - Drawing a line
Write a Python program using the turtle module to move 100
pixels forward and then move 100 pixels backward.
1 2 3 4 5 | #turtle program to draw the moving forward and backward from turtle import * title("Moving forward and backward") forward(100) backward(100) |
Code
Output
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Drawing a Circle
The circle method in Python turtle can be used to draw a circle or an arc.
The part of the circle in degrees as an arc
Syntax →
circle(radius, extent=None, steps=None)
Radius of the circle
Divide the shape in the equal number of given steps.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Drawing a Circle
Code
Write a Python program using the turtle module to draw two circles as shown below.
1 2 3 4 5 6 | #Turtle program to draw the circles from turtle import * title(“Drawing circles”) circle(50) forward(100) circle(50) |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo 2 - Drawing an Arc
Code
Output
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
|
1 2 3 4 | from turtle import * pensize(4) circle(100, extent=180) hideturtle() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Code
Activity 2 - Drawing a Square
Output →
Write a Python program using the turtle module to draw a square with side lengths of 200 pixels.
1 2 3 4 5 6 7 8 9 10 | #drawing the square from turtle import * forward(200) right(90) forward(200) right(90) forward(200) right(90) forward(200) right(90) |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Code
Activity 3 - Drawing an Ice Cream
Output →
Write a Python program using the turtle module to draw an ice cream as shown in the example given below.
1 2 3 4 5 6 7 8 9 10 | from turtle import * pensize(4) left(90) circle(100,180) pensize(10) goto(0,0) right(45) goto(-100,-200) goto(-200,0) done() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
PEN CONTROL
ACTIVITIES
3
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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 |
1 | pendown() | To set the turtle's pen down, so that it will draw a line as it moves. |
2 | penup() | To lift the turtle's pen up, so that it will not draw a line as it moves. |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Turtle Pen Control (cont.)
SI# | Command | Purpose |
3 | pensize(width) | To set the width of the turtle's pen. The width is specified in pixels. |
4 | pencolor(color) | To set the color of the turtle's pen. * |
5 | speed(speed) | To control the speed of the turtle. where speed is an integer or a string. ** |
6 | goto(x, y) setpos(x,y) setposition(x,y) | Move the turtle to the specified x, y coordinates on the screen. |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Identifying the Methods
Watch the following video or observe the image and list down the methods used in the program.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Solution
bgcolor(“blanched almond “)
pencolor(“blue”)
pencolor(“red”)
pensize(3)
circle(100)
moves the turtle to a new position
penup()
pendown()
forward(100)
→
pensize(6)
forward(100)
left(90)
The methods used in the program are as the following:
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo 1 - Draw a House
Write a Python program to draw a simple house as shown below.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo 1 - Solution
Code
1 2 3 4 5 6 7 8 9 10 | from turtle import * pensize(5) penup() goto(-100,0) pendown() forward(200) left(90) forward(150) left(90) forward(200) |
11 12 13 14 15 16 17 18 19 20 | left(90) forward(150) left(90) penup() goto(-20,0) pendown() left(90) forward(80) right(90) forward(40) |
21 22 23 24 25 26 27 28 29 30 | right(90) forward(80) left(90) penup() goto(-130,150) pendown() forward(260) goto(0,200) goto(-130,150) penup() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 2 - Solution
Code
1 2 3 4 5 6 7 8 9 | from turtle import * pensize(3) shape("turtle") penup() goto(-200,0) pendown() left(90) forward(200) right(90) |
10 11 12 13 14 15 16 17 18 | forward(300) right(146) forward(360) left(180) forward(360) right(124) forward(200) right(90) forward(300) |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 3 - Drawing Olympic Ring
Write a Python program using turtle module to draw an olympic logo as shown below.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 3 - Solution
Code
1 2 3 4 5 6 7 8 9 | from turtle import * pensize(5) color("blue") penup() goto(-110, -25) pendown() circle(45) color("black") penup() |
10 11 12 13 14 15 16 17 18 | goto(0, -25) pendown() circle(45) color("red") penup() goto(110, -25) pendown() circle(45) color("yellow") |
192021222324 25 2627 | penup() goto(-55, -75) pendown() circle(45) color("green") penup() goto(55, -75) pendown() circle(45) |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
COLOUR FILL
ACTIVITIES
4
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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 |
1 | begin_fill() | Tells turtle to fill in any closed shapes that are drawn |
2 | end_fill() | Tells turtle to stop filling in closed shapes that are drawn |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo - Colouring a Circle
Code
Output
Write a Python program using the turtle module to draw a circle filled with purple colour.
1 2 3 4 5 6 | from turtle import * shape("turtle") begin_fill() color("purple") circle(50) end_fill() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Solution
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from turtle import * pensize(5) shape("turtle") penup() goto(-150,0) pendown() color("pink") begin_fill() circle(40) end_fill() penup() forward(100) pendown() color("blue") circle(40) |
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | penup() forward(100) pendown() color("purple", "orange") begin_fill() circle(40) end_fill() penup() forward(100) pendown() color("purple", "orange") pensize(9) begin_fill() circle(40) end_fill() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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?
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 2 - Solution
Code
1 2 3 4 5 6 7 8 | from turtle import * pensize(3) shape("turtle") penup() goto(-200,0) pendown() fillcolor(“yellow”) begin_fill() |
11 12 13 14 15 16 17 18 | left(90) forward(200) right(90) forward(300) right(146) forward(360) end_fill() fillcolor(”orange”) |
21 22 23 24 25 26 27 28 | begin_fill() left(180) forward(360) right(124) forward(200) right(90) forward(300) end_fill() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 3 - Check Your Understanding
Write the answers for the following questions in your notebook.:
Ans: Marks the beginning of the colour 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.
a) Red b) Green c) Black
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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()
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 4 - Solution
Code
1 2 3 4 5 6 7 8 9 10 11 12 | from turtle import * speed(0) pensize(5) color("red") goto(-100,0) begin_fill() forward(160) circle(20,90) forward(100) circle(20,90) forward(160) circle(20,90) |
13 14 15 16 17 18 19 20 21 22 23 24 | forward(100) circle(20,90) end_fill() penup() goto(20,70) pendown() color("white") begin_fill() left(90) circle(40,360,3) end_fill() hideturtle() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
TEXT METHODS
ACTIVITIES
5
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Text Methods
Example
write(“Hello World!”,align="center",font=("Arial",16,"normal"))
Method
Alignment of the text
String to write
Font
write() method in Python turtle module is used to write a text on the turtle canvas.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo - Labelling a Circle
Write a Python program using the turtle module to insert the text ‘Circle’ as shown below.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Demo - Solution
Code
1 2 3 4 5 6 7 8 | from turtle import * pencolor("red") circle(100) penup() goto(0,100) pendown() color("blue") write("Circle", align="center", font=("Georgia",16,"bold")) |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Colouring the House
Write a Python program to draw and colour a house as shown in
the image given below.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Solution
Code
1 2 3 4 5 | from turtle import * pensize(5) penup() goto(-100,0) pendown() |
6 7 8 9 10 11 12 13 14 15 16 17 | fillcolor("pink") begin_fill() forward(200) left(90) forward(150) left(90) forward(200) left(90) forward(150) left(90) end_fill() penup() |
Set the color of the wall, fill and draw the wall.
Import the turtle module, set the pensize, set the position of the turtle
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Solution (cont.)
Set the position to draw the door, select the color to fill the door, then draw and fill the door
18 19 20 21 22 23 24 25 26 27 28 29 30 | goto(-20,0) pendown() fillcolor("brown") begin_fill() left(90) forward(80) right(90) forward(40) right(90) forward(80) end_fill() left(90) penup() |
Set the position to draw the door, select the color to fill the door, then draw and fill the door
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 1 - Solution (cont.)
Set the position to draw the roof, select the color to fill the roof, then draw and fill the roof.
Add text ‘A house’ using write() method.
31 32 33 34 35 36 37 38 39 40 41 42 43 | goto(-130,150) pendown() fillcolor("green") begin_fill() forward(260) goto(0,200) goto(-130,150) penup() end_fill() penup() goto(0,-40) color("blue") write("A house!",align="center",font=("Georgia",22)) |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 2 - Drawing a Smiley Face
Write a Python program using the turtle module to draw a smiley face as shown below.
1 2 3 4 5 6 7 8 9 10 11 | from turtle import * # Set up the turtle speed(2) # Draw the face penup() fillcolor("yellow") goto(0, -100) pendown() begin_fill() circle(100) end_fill() |
Code
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 2 - Solution (cont.)
Output →
12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # Draw the eyes penup() fillcolor("black") goto(-30, 20) pendown() begin_fill() circle(15) end_fill() penup() goto(30, 20) pendown() begin_fill() circle(15) end_fill() |
26 27 28 29 30 31 32 33 | # Draw the smile penup() goto(-50, -40) pendown() right(60) circle(60, 120) # Hide the turtle hideturtle() |
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 3 - Check your understanding
Write the answers for the following questions in your notebook.
a) raisepen() b) penup()
c) pen_up() d) lift_pen()
a) set_speed("fastest") b) speed("fastest")
c) max_speed() d) turbo_mode()
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
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)
6. The forward(distance) method can be used to go backward by using a distance. (True/False)
color
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 4 - Drawing Sunset
Observe the given art carefully and recreate it in Python program using the turtle module.
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Activity 4 Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from turtle import* title("Sunset") shape("turtle") bgcolor("cyan") begin_fill() color("yellow") left(90) circle(75, 180) end_fill() home() begin_fill() color("blue") forward(200) forward(200) |
15 16 17 18 19 20 21 22 23 24 25 26 27 28 | right(90) forward(200) right(90) forward(400) right(90) forward(200) right(90) forward(200) end_fill() home() penup() goto(0,130) pendown() write("Sunset",font=("Impact",30)) |
Code
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
Key Points
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD
བཀྲིན་ཆེ།
THANK YOU
January 2024
PYTHON
Key stage IV
Class IX
© ICT Curriculum, MoESD