Python Turtle: Say hello to the Turtle of the coding world!

Turtle Library Python

Hello fellow learner! Today you will be introduced to Python Turtle, the turtle of the coding world who is used to create fun drawings and shapes on your screen with just some simple steps.

Let’s Begin!

Introduction to the Python Turtle Library

Python Turtle helps users interact with the programming language better by drawing various things on a virtual canvas.

It makes use of a virtual pen as well known as a turtle.

One can draw and make different shapes and pictures with the help of the python turtle library. Mostly the turtle library is used to draw shapes, create designs, and make images. But it can also be helpful in creating mini-games and animations.

Getting Started with Python Turtle

You need to import the turtle library in order to use its methods and functionalities. It also comes with a Python standard kit and hence, does not require additional installation.

The next step involves creating the virtual canvas to draw the various objects on. We can name the canvas/screen according to our needs and interests and displaying it. The code below does the creation and displaying of the screen for the user.

#Importing Module
import turtle as tur

# Creating Screen
wind = tur.Screen()

# Displaying Screen
sc = tur.getscreen()
tur.mainloop()

The output of the code mentioned above results in the screen which is displayed below:

Initial Python Turtle Window
Initial Turtle Window

One can see the output of the code on this screen and the little black triangular shape in the middle of the screen is called the turtle which can be used to draw the required shapes and objects.

First, one needs to create the turtle and then use the position functionalities on the turtle. Turtle supports four basic movements namely forward, backward, left, and right. The forward and backward function needs the distance as a parameter on the other hand the left and right function need an angle of turning as a parameter. The code below helps to display the basic movement operations on a turtle.

import turtle

# Creating Screen
my_window = turtle.Screen()

# Creating turtle to draw
my_pen = turtle.Turtle()      

# Moving Forward
my_pen.forward(150)           
# Turning Right
my_pen.right(40)
# Moving Forward
my_pen.forward(150)
#Moving Left
my_pen.left(90)
#Moving Backward
my_pen.backward(30)

# Displaying Window
my_window.mainloop()

The output of the code is displayed in the picture below.

Basic Python Turtle Movement Visualization
Basic Turtle Movement Visualization

Creating Shapes using Python Turtle

Using the basic functionalities of the turtle movements one can create some basic shapes such as triangle, square, and rectangle. One can also create shapes like a star.

1. Creating Pentagon with Turtle

The code below displays a pentagon on the screen with the help of a Python Turtle. For a regular pentagon, all edges have equal length and all angles are equal to 72 degrees.

import turtle

# Creating Screen
my_window = turtle.Screen()

# Creating turtle to draw
my_pen = turtle.Turtle()      

#Creating a Pentagon
for i in range(5):
    my_pen.forward(150)
    my_pen.left(72)

# Displaying Window
my_window.mainloop()

The picture below shows the output of the code above resulting in a pentagon.

Making Pentagon Using Turtle
Making Pentagon Using Turtle

2. Creating a Star with Turtle

The code below displays a star on the screen with the help of a turtle. For a regular star shape, all edges have equal length and all angles are equal to 144 degrees.

import turtle

# Creating Screen
my_window = turtle.Screen()

# Creating turtle to draw
my_pen = turtle.Turtle()      

#Creating a Star Shape
for i in range(5):
    my_pen.forward(200)
    my_pen.right(144)

# Displaying Window
my_window.mainloop()

The picture below shows the output of the code above resulting in a star shape.

Making Star Using Turtle
Making Star Using Turtle

Changing Colors with Python Turtle

One can change the color of the screen, the turtle, and the lines drawn to make the shapes look nicer. The code below displays a rectangle and a star using different colors.

import turtle

# Creating Screen
my_window = turtle.Screen()
turtle.bgcolor('black')

# Creating turtle to draw
my_pen = turtle.Turtle()
my_pen.color('yellow')
my_pen.forward(150)
my_pen.color('green')
my_pen.left(90)
my_pen.forward(200)
my_pen.color('orange')
my_pen.left(90)
my_pen.forward(150)
my_pen.color('pink')
my_pen.left(90)
my_pen.forward(200)
my_pen.right(90)
my_pen.color('black')
my_pen.forward(100)

colors = ['red','magenta','yellow','orange','green']
for i in range(5):
    my_pen.color(colors[i])
    my_pen.forward(200)
    my_pen.right(144)

# Displaying Window
my_window.mainloop()

The picture below displays the output of the code.

Making Colorful Turtle Shapes
Making Colorful Turtle Shapes

Conclusion

Congratulations! You now know about Python turtle! This Turtle Library can also create very complex shapes and have a lot of colors.

Happy coding!

Hope you learned something!