Wednesday, June 3, 2026

4.4 GRAPHICS IN PYTHON USING TURTLE



4.4 GRAPHICS IN PYTHON USING TURTLE

Introduction to Turtle

Turtle is a Python module used to create drawings, shapes, patterns and designs on the screen. It works by moving a virtual pen called a turtle using simple commands.

Turtle helps beginners learn programming through visual output. Instead of printing only text, it can draw shapes, patterns, and pictures.

 

Features of turtle

Feature

Description

Easy Learning

Helps learn programming through drawings and graphics.

Fun Learning

Makes coding interesting by creating shapes and designs.

Creativity

Allows users to create different drawings and patterns.

Easy Error Detection

Mistakes can be identified easily by looking at the drawing.

Learn Shapes

Helps understand shapes, angles, and positions.

Better Thinking

Improves logical thinking by following drawing steps.

 

 

Basic Structure of a Turtle Program

 

import turtle

Meaning: Imports the Turtle module so Turtle commands can be used.

Simple Explanation: Python cannot draw by itself. This command loads the Turtle library.

Without it: Turtle programs will not work.

 

import turtle as t

Meaning: Imports the Turtle module and gives it a short name (t).

Simple Explanation: Instead of writing turtle every time, we can write only t.

 

screen = turtle.Screen( )

Meaning: Creates a drawing window where the turtle can move and draw.

Simple Explanation: It opens a blank screen or canvas for drawing.

Used for:

  • Changing background color
  • Setting window size
  • Changing window title
  • Detecting keyboard and mouse actions

 

pen = turtle.Turtle( )
Meaning:
Creates a turtle object and stores it in the variable pen.

Simple Explanation: This creates the drawing turtle that will move and draw on the screen.

 

turtle.done( )

Meaning:
Keeps the Turtle graphics window open until it is closed manually.

Simple Explanation:
Stops the program from closing immediately after drawing.

 

Without done(), the window may close immediately after the drawing finishes. Humans do enjoy spending ten minutes drawing a square only to watch the window vanish instantly. 🐢

 

 

turtle.mainloop( )

Meaning:
Keeps the Turtle window open and waits for user actions.

Simple Explanation:
It allows the program to keep running until the user closes the window.

Uses

  • Keeps the window open
  • Waits for keyboard or mouse events
  • Prevents the program from ending immediately

 

🐢 Important Turtle Commands

1. Turtle Motion Commands

Turtle motion commands are used to move the turtle on the screen. The turtle can move forward, backward, left, and right while drawing.

Command

Definition

Syntax

Example

forward() / fd()

Moves the turtle forward by a specified distance.

forward(distance)

pen.forward(100)

backward() / bk()

Moves the turtle backward by a specified distance.

backward(distance)

pen.backward(50)

right() / rt()

Turns the turtle clockwise by a specified angle.

right(angle)

pen.right(90)

left() / lt()

Turns the turtle anticlockwise by a specified angle.

left(angle)

pen.left(90)


2. Shape Command

Command

Definition

Syntax

Example

circle()

Draws a circle with a specified radius.

circle(radius)

pen.circle(50)

 

3. Color and Style Commands

These commands are used to change the appearance of drawings.

Command

Definition

Syntax

Example

color()

Changes the pen color.

color(color_name)

pen.color("red")

fillcolor()

Sets the fill color of a shape.

fillcolor(color_name)

pen.fillcolor("yellow")

pensize()

Changes the thickness of the drawing line.

pensize(size)

pen.pensize(5)

speed()

Controls the drawing speed of the turtle.

speed(value)

pen.speed(0)

 

Speed Values

0 – Fastest , 1 – Slowest , 3 – Slow , 5 – Medium , 10 - Fast

 

4. Pen Control Commands

These commands control whether the turtle draws while moving.

Command

Definition

Syntax

Example

penup()

Lifts the pen so movement does not draw lines.

penup()

pen.penup()

pendown()

Places the pen down so movement draws lines.

pendown()

pen.pendown()

Example

pen.penup()
pen.forward(100)

pen.pendown()
pen.forward(100)

The first movement draws nothing. The second movement draws a line.

 

5. Position Commands

These commands control the turtle's position on the screen.

Command

Definition

Syntax

Example

goto()

Moves the turtle to a specific coordinate (x, y).

goto(x,y)

pen.goto(100,50)

home()

Returns the turtle to the starting position (0,0).

home()

pen.home()

 

Direction

Coordinate Value

Right

Positive X (+X)

Left

Negative X (-X)

Up

Positive Y (+Y)

Down

Negative Y (-Y)

📌 The center of the Turtle screen is (0,0).

 

6. Fill Commands

These commands are used to fill shapes with color.

Command

Definition

Syntax

Example

begin_fill()

Starts filling a shape with color.

begin_fill()

pen.begin_fill()

end_fill()

Ends filling and fills the shape.

end_fill()

pen.end_fill()

 

7. Text Command

Command

Definition

Syntax

Example

write()

Displays text on the screen.

write(text)

pen.write("Hello Python")

Example

pen.write("Welcome to Turtle Graphics")

 

8. Screen Commands

Command

Definition

Syntax

Example

clear()

Removes all drawings from the screen.

clear()

pen.clear()

reset()

Clears the screen and returns the turtle to the starting position.

reset()

pen.reset()

hideturtle()

Hides the turtle cursor.

hideturtle()

pen.hideturtle()

showturtle()

Displays the turtle cursor.

showturtle()

pen.showturtle()

bgcolor()

Changes the screen background color.

screen.bgcolor(color)

screen.bgcolor("lightblue")

turtle.done()

Keeps the Turtle window open.

turtle.done()

turtle.done()

 

9. Shape Command

pen.shape("turtle")

Definition : The shape() function is used to change the appearance of the turtle cursor.

Syntax

pen.shape("turtle")

Common Shapes

Shape

Example

"arrow"

Default arrow cursor

"turtle"

Turtle shape

"circle"

Circle shape

"square"

Square shape

"triangle"

Triangle shape

"classic"

Classic turtle cursor

 
















No comments:

Post a Comment