Turtle Programming in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The Turtle library in Python helps the user to design a virtual canvas that can be used to draw pictures and interactive shapes. The Turtle was developed in 1967 as a part of the Logo programming language, which was a popular language among kids for drawing attractive shapes.

Python Turtle library benefits children and experienced programmers as it allows for designing unique shapes, attractive graphics, and various designs. We can also use this library to design mini-games and embed animations. In the upcoming section, we will be learning about various functionalities of the turtle library in Python.

In this article, we will learn about the working of the Python Turtle, some common methods, and plotting various designs using turtle in Python with the help of examples.

Methods of Turtle Programming in Python

Some majorly used turtle library methods are:

MethodParameterDescription
Turtle()Noneinitializes and returns a new turtle object
forward()amountmoves the turtle forward by the specified value
backward()amountmoves the turtle backward by the specified value
right()angleturns the turtle clockwise
left()angleturns the turtle counterclockwise
penup()Nonepicks up the turtle pen
pendown()Noneputs down the turtle pen
up()Nonepicks up the turtle pen
down()Noneputs down the turtle pen
color()Color namechanges the color of the turtle pen
fillcolor()Color namechanges the color of the turtle which is used to fill a shape
heading()Nonereturns the current heading
position()Nonereturns the current position
goto()x, ymoves the turtle to given position (x,y)
begin_fill()Noneremembers the starting point for a filled shape
end_fill()Nonecloses the polygon and fills with the current fill color
dot()Noneleaves the dot at the current position
stamp()Noneleaves an impression of a turtle shape at the current location
shape()shapenameshould be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

How to Plot using Python Turtle?

To implement the Turtle library in our program, we have first to import this library using the following statement:

Turtle comes pre-installed along with the Python package, and there is no need to install it explicitly. We can explain the life of a turtle program in the following four steps:

  1. importing the turtle module
  2. initializing our turtle to control the drawing
  3. start drawing using multiple drawing methods
  4. at last run turtle.done() to end the program

After we have imported the turtle library into our program, we have to create a window/ drawing board to implement all the drawing functionalities over this window.

Now we are done with creating the window and have also customized our window. We can now move our turtle using forward(value) or various other methods. After our shape is completed, we can end the program by writing turtle.done().

Drawing Basic Shapes using Turtle Python

Let us now learn how to implement basic shapes using the Turtle library:

  • Square

    Output

    SQUARE USING TURTLE

  • Star

    Output

    STAR USING TURTLE

  • Hexagon

    Output

    HEXAGON USING TURTLE

  • Circle

    Output

    CIRCLE USING TURTLE

  • Rectangle

    Output

    RECTANGLE USING TURTLE

Some Extraordinary Turtle Python Programs

After basic shapes, let us also explore some amazing programs using the Turtle library in Python:

  • Spiral Square inside out and outside in

    Output

    SPIRAL SQUARE

  • User Input Pattern

    This program is used to draw shapes based on the number of sides the user enters in a uniform pattern:

    Output

    USER INPUT PATTERN

  • Spiral Helix Pattern

    Output

    SPIRAL HELIX

  • Rainbow Benzene

    Output

    RAINBOW BENZENE

  • Trees using Turtle Programming

    Using the Turtle library in Python, we can try to draw Y tree (fractal tree) using Turtle. The approach is as follows:

    • Initially, we will draw ‘Y’ shape for the tree's base(level 1). Then both the branches of the ‘Y’ serve as the base of the other two ‘Y’s(level 2).
    • The above step will be repeated recursively, and the size of the Y will decrease as the level increases.
    • Colouring of the tree will be done based on level: darkest at the base level to lightest at the top.

    Output

    TREE USING TURTLE

Conclusion

  • Turtle is a Python library that helps the user to design a virtual canvas that we can use to draw pictures and interactive shapes.
  • Turtle program in Python provides many functions and programs to be programmed in Python.
  • In this article, we learned how to use the Turtle library in Python to design attractive shapes, unique graphics, animations, etc.