Turtle Programming in Python
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:
Method | Parameter | Description |
---|---|---|
Turtle() | None | initializes and returns a new turtle object |
forward() | amount | moves the turtle forward by the specified value |
backward() | amount | moves the turtle backward by the specified value |
right() | angle | turns the turtle clockwise |
left() | angle | turns the turtle counterclockwise |
penup() | None | picks up the turtle pen |
pendown() | None | puts down the turtle pen |
up() | None | picks up the turtle pen |
down() | None | puts down the turtle pen |
color() | Color name | changes the color of the turtle pen |
fillcolor() | Color name | changes the color of the turtle which is used to fill a shape |
heading() | None | returns the current heading |
position() | None | returns the current position |
goto() | x, y | moves the turtle to given position (x,y) |
begin_fill() | None | remembers the starting point for a filled shape |
end_fill() | None | closes the polygon and fills with the current fill color |
dot() | None | leaves the dot at the current position |
stamp() | None | leaves an impression of a turtle shape at the current location |
shape() | shapename | should 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:
- importing the turtle module
- initializing our turtle to control the drawing
- start drawing using multiple drawing methods
- 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
-
Star
Output
-
Hexagon
Output
-
Circle
Output
-
Rectangle
Output
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
-
User Input Pattern
This program is used to draw shapes based on the number of sides the user enters in a uniform pattern:
Output
-
Spiral Helix Pattern
Output
-
Rainbow Benzene
Output
-
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
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.