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 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.

 

Uses of Turtle Module

i. Easy Visualization of Programming Concepts

Turtle helps students understand programming concepts such as loops, functions, variables, and conditions through graphical output.

ii. Interactive Learning

Users can control the turtle’s movement and create different shapes and patterns using simple Python commands, making learning more engaging.

iii. Enhances Creativity

Students can design drawings, patterns, and artistic graphics using simple code, which encourages creativity and problem-solving skills.

iv. Simplified Debugging

Since Turtle provides visual output, it is easier to identify and correct mistakes in programs. Python’s simple syntax also makes debugging easier than many other programming languages.

v. Learning Basic Geometry

Turtle helps students understand angles, coordinates, shapes, and measurements through practical drawing activities.

vi. Developing Logical Thinking

Creating drawings and patterns requires planning and sequencing commands, which helps improve logical and computational thinking.

 

Basic Structure of a Turtle Program

import turtle as t

# imports the turtle module and assigns it a short name (alias) t for easier use in the program.

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.

 

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
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()

 

🧊 shape( )

Changes the shape of the turtle cursor itself.

Example:
t.shape("turtle")

👉 The cursor becomes a turtle instead of an arrow.

Common shapes:

  • "arrow"
  • "turtle"
  • "circle"
  • "square"

 

These commands are sufficient to create most examination programs such as:
Square, Rectangle, Triangle, Circle, Star, House, Basic Patterns, Nepal Flag.


No comments:

Post a Comment