4.4
GRAPHICS IN PYTHON USING TURTLE
Introduction to
Turtle
Turtle is a Python library used to create graphics,
patterns, and drawings by moving a virtual pen (called a turtle) on the screen
using basic programming commands. Turtle is a module in Python that allows us
to bring shapes, figures, and designs to life on a screen.
It is widely used by beginners and students to learn
programming concepts through visual output.
i. Easy Learning
Turtle helps in
learning coding by showing drawings instead of only text.
ii. Fun Learning
Shapes are drawn
using simple commands, making coding more interesting.
iii. Creativity
Drawings and
designs can be created using code.
iv. Easy to Find
Mistakes
Drawings help in
easily identifying errors in programs.
v. Learn Shapes
Helps in
understanding shapes, angles, and positions.
vi. Better
Thinking
Steps are followed
to draw, which improves logical thinking and organization.
Basic Structure of
a Turtle Program
import turtle
Imports the Turtle
module so turtle graphics commands can be used.
Python cannot draw
by itself. This line loads the Turtle library.
Without this line,
Turtle commands will not work.
import turtle as t
# imports the
turtle module and assigns it a short name (alias) t for easier use in the
program.
screen =
turtle.Screen( )
Creates a drawing
window (screen) where the turtle can move and draw.
Think of it as
opening a blank canvas or paper.
A white window
appears.
It is needed when
you want control over the screen.
You use it when
you want to:
Change background
color
Set window size
Change title
Detect key presses
/ mouse clicks
pen =
turtle.Turtle( )
#
creates a turtle object and assigns it to the variable pen, which is used to
draw graphics on the screen.
turtle.done( )
# keeps the turtle
graphics window open until it is closed manually by the user.
Think of it like:
❌
Without done() → Movie starts and ends in 0.1 seconds
✅
With done() → Movie plays and stays until you exit
📌 turtle.mainloop()
It keeps the
Turtle graphics window open and starts an event loop that listens for user
actions (like closing the window).
Keeps window open
Waits for user
interaction
Prevents program
from ending immediately
Important Turtle
Commands
Turtle Motion
Turtle motion
refers to the movement of the turtle on the screen. The turtle can move forward
and backward in the direction it is facing.
Movement Commands
1. forward( ) -
[fd] à Moves the turtle
forward by a specified distance.
Syntax: forward(distance)
Example: pen.forward(100)
2. backward( ) -
[bk] à Moves the turtle
backward by a specified distance.
Syntax: backward(distance)
Example: pen.backward(50)
3. right( ) - [rt] à
Turns the turtle clockwise by a specified angle.
Syntax: right(angle)
Example: pen.right(90)
4. left( ) - [lt] à Turns the turtle anticlockwise by
a specified angle.
Syntax: left(angle)
Example: pen.left(90)
Shape Commands
5. circle( ) à Draws a circle of a given radius.
Syntax: circle(radius)
Example: pen.circle(50)
Color and Style
Commands
6. color( ) à Changes the pen color.
Syntax: color(color_name)
Example: pen.color("red")
7. fillcolor( ) à
Sets the fill color of a shape.
Syntax: fillcolor(color_name)
Example: pen.fillcolor("yellow")
8. pensize( ) à Sets the thickness of the drawing
line.
Syntax: pensize(size)
Example: pen.pensize(5)
9. speed( ) à Controls the drawing speed of the
turtle.
Syntax: speed(value)
Example: pen.speed(0)
Note:
0 = Fastest
1 = Slowest
3 = Slow
5 = medium
10 = Fast
Pen Control
Commands
10. penup( ) à Lifts the pen so movement does not
draw.
Syntax: penup()
Example: pen.penup()
11. pendown( ) à Places the pen down to resume
drawing.
Syntax: pendown( )
Example: pen.pendown( )
Position Commands
12. goto( ) à Moves the turtle to a specific
coordinate (x, y).
Syntax: goto(x, y)
Example: pen.goto(100, 50) # moves the
turtle to x = 100 and y = 50.
13. home( ) à Moves the turtle to the starting
position (0,0).
Syntax: home()
Example: pen.home()
Coordinate System
in Turtle
The center of the
Turtle screen is (0, 0).
Positive x → Right
Negative x → Left
Positive y → Up
Negative y → Down
Fill Commands
14. begin_fill( ) à Starts filling a shape with color.
Syntax: begin_fill()
Example: pen.begin_fill()
15. end_fill( ) à Ends filling a shape with color.
Syntax: end_fill()
Example: pen.end_fill()
Text Command
16. write( ) à Displays text on the screen.
Syntax: write(text)
Example: pen.write("Hello Python")
Screen Commands
17. clear( ) à Removes all drawings from the
screen.
Syntax: clear()
Example: pen.clear()
18. reset( ) à Clears the screen and returns the
turtle to its starting position.
Syntax: reset()
Example: pen.reset()
19. hideturtle( ) à Hides the turtle cursor.
Syntax: hideturtle()
Example: pen.hideturtle()
20. showturtle( ) à Displays the turtle cursor.
Syntax: showturtle()
Example: pen.showturtle()
21. bgcolor( ) à Changes the background color of
the screen.
Syntax: screen.bgcolor(color_name)
Example: screen.bgcolor("lightblue")
22. turtle.done( )
à Keeps the Turtle
graphics window open.
Syntax: turtle.done()
Example: turtle.done()
📌 pen.shape("turtle")
It changes the
shape of the turtle cursor (the drawing pen) on the screen.
Default shape:
small arrow ➜
Why use it?
Makes drawing
easier to understand visually
More fun for
beginners
Helps you actually
see the turtle moving (not just a boring arrow pretending to be a programmer)
It is a Turtle
graphics function used to change the shape of the turtle cursor to a turtle
shape so that the movement is more visible and attractive




























