Understanding Python Turtle Methods With Examples

Introduction

The turtle module is a part of the standard Python installation which provides a drawing board so that we can draw all over it using turtle methods. It is a popular way of introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert, and Cynthia Solomon in 1967.

Python’s turtle module lets us control a small image shaped like a turtle, just like a video game character. We need to give precise instructions to direct the turtle around the screen. Because the turtle leaves a trail wherever it goes, we can use it to write a program that draws different shapes.

Python Turtle Methods

Let us begin to understand turtle methods with the following examples.

import turtle module

import turtle as tt

For convenience, I have imported the turtle module as ‘tt’ throughout the examples.

 

Example 1: Moving turtle

# importing turtle module
import turtle as tt 

tt.forward(100)

Above we use the forward function to move the turtle 100px forward while leaving a trail behind it. When we run the program we will see something like this output.

Output

python-turtle-moving-forward

 

We will be noticing that the window screen automatically fades out as it drew a 100px arrow sign. To run the program until we exit we have to update the above code as.

Updated code

# importing turtle module
import turtle as tt

tt.forward(100)

# to keep screen holding
tt.getscreen()._root.mainloop()

 

Example 2: Creating a square shape

import turtle as tt

# change the shape of the default
# arrow to turtle 
shape('turtle')

for i in range(4):
    tt.forward(100)
    # change turtle direction
    # at angle 90 clockwise
    tt.right(90)

tt.getscreen()._root.mainloop()

In the above example, we have used the loop in the range of 4 to move the turtle 4 times in four different directions at 90º clockwise. As we already know forward functions move the turtle, we have to use the right function to change directions.

right(): turns the turtle clockwise

left():  turns the turtle counterclockwise

shape(): changes the shape of the default arrow. Initially, there are the following polygon shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”

Output

python-turtle-square

 

 

Example 3: Creating a circle of squares

# import turtle
import turtle as tt


def make_circle(sidelength):
    """
    Draw circle from 30 
    consecutive squares each
    bent at 10° in clockwise 
    directions.
    """
    for i in range(30):
        for j in range(4):
            tt.forward(sidelength)
            tt.right(90)
        tt.right(10)

# calling function
make_circle(100)

tt.getscreen()._root.mainloop()

Output

python-turtle-circle-of-squares

 

 

Example 4: Creating a red flower

Before beginning with the code, let us review some functions.

setheading(to_angle): This function sets the orientation of the turtle pointer to to_angle. Shortly we can call this function seth(to_angel) like seth(15), seth(50).

color(color_name): Changes the color of the turtle’s pen

circle(radiusextent=Nonesteps=None): Draw a circle with a given radius. Other parameters extend and steps we can use from modification of circle.

# import turtle module
import turtle as tt

tt.shape('turtle')
# Changes turtle’s pen to 
# color 'red'
tt.color("red")

for angle in range(0, 360, 15):
    tt.seth(angle)
    tt.circle(50)

tt.getscreen()._root.mainloop()

Output

python-turtle-red-flower

 

 

Example 5: Creating star

import turtle as tt 

for i in range(5):
    tt.forward(100) 
    tt.right(144) 

tt.getscreen()._root.mainloop()

Output

 

 

python-turtle-shape-star

Leave a Comment