“Turtle” is a Python function that works by providing a drawing board and allows you to order a turtle to draw on it! The turtle needs to be moved around for a drawing to appear on the screen. This can be achieved by using functions like forward(), backward(), left() and right().
You can switch the turtle around with functions like turtle.forward(…) and turtle.left(…).
You must first import a turtle before you can use it. We suggest practicing with it in the interactive interpreter first, as working with files requires a little more effort. Simply type the following command into your terminal:
import turtle
Do you have the error message “No module called _tkinter” while working with Ubuntu? sudo apt-get install python3-tk installs the missing package.
sudo apt-get install python3-tk
The turtle.forward(…) function instructs the turtle to advance the specified distance. The number of degrees you want to rotate to the left is passed to turtle.left(…). On the other hand, you turtle.backward(…) if you want to move backwards by the distance value provided as a parameter. Also, use turtle.right(…) if you wish to rotate the turtle by the given number of degrees towards the right.
Do you want to start over?
To clear the drawing your turtle has made so far, use turtle.reset(). We’ll go over turtle in greater depth in the subsequent sections with examples so that you can master the concepts better.
The typical turtle is nothing more than a triangle. That’s not a good time! Instead of a turtle, let’s make it a turtle using the command turtle.shape() as shown below.
turtle.shape("turtle")
You may have noticed that the turtle window vanishes after the turtle completes its movement if you saved the commands to a file. This is due to Python exiting when the turtle has completed its movement. Python owns the turtle window, so it goes away as well. Place turtle.exitonclick() at the bottom of your file to avoid this. The window will now remain open until you click on it:
import turtle turtle.shape("turtle") turtle.forward(25) turtle.exitonclick()
Drawing a Square
You’ll probably need a right angle, which is 90 degrees, to make a square.
Drawing a square Anti-Clockwise
import turtle my_turtle =turtle.Turtle() my_turtle.shape('turtle') drawing_area =turtle.Screen() drawing_area.title("tuts@codeunderscored: Drawing a Square Anti-Clockwise") # my_turtle.penup() my_turtle.forward(200) my_turtle.left(90) my_turtle.forward(200) my_turtle.left(90) my_turtle.forward(200) my_turtle.left(90) my_turtle.forward(200) my_turtle.left(90) drawing_area.exitonclick()

Drawing a square Clockwise
import turtle my_turtle =turtle.Turtle() my_turtle.shape('turtle') drawing_area =turtle.Screen() drawing_area.title("tuts@codeunderscored: Drawing a Square Clockwise") my_turtle.left(90) my_turtle.forward(200) my_turtle.right(90) my_turtle.forward(200) my_turtle.right(90) my_turtle.forward(200) my_turtle.right(90) my_turtle.forward(200) my_turtle.right(90) drawing_area.exitonclick()

Optimized code to draw a square
import turtle my_turtle =turtle.Turtle() # create instance of the turtle my_turtle.shape('turtle') # set the shape of the turtle to that of the turtle drawing_area =turtle.Screen() # create the screen drawing_area.title("tuts@codeunderscored: Optimizing drawing a square ") my_turtle.pendown() # lift the pen up so when the turtle moves, it will not leave a trace behind my_turtle.left(90) # for i in range(4): my_turtle.forward(200) my_turtle.right(90) drawing_area.exitonclick()
Keep in mind
Before and after drawing the square, note how the turtle begins and finishes in the same position, facing the same way. This is a helpful convention to observe since it makes drawing different shapes later on much simpler.
Added value
If you want to be more imaginative, use the turtle.width(…) and turtle.color(…) functions to change the form. How do you make use of these features? You must first learn the signature of a feature before you can use it. For example what to put between the parentheses and what those things mean.
In the Python shell, type help(turtle.color) to find out more. Python can bring it into a pager, which allows you to scroll up and down in case there is a lot of text. To exit the pager, press the q key.
Is this an error ?

When trying to get support, you get a NameError: the name ‘turtle’ isn’t specified. In Python, you must import names before you can refer to them, so you’ll need to import turtle before using help(turtle.color) in a new Python interactive shell as shown below.

You should tell the turtle to delete its drawing board with the turtle if you make a mistake.
Use the reset() directive or undo the most recent move using the command undo().
You can change the color with turtle.color, as you might have read in the aid. The color string passed as a parameter includes the colors “red,” “green,” and “violet,” among others. For instance turtle.color(colorstring) where the colorstring can be red, green etc. A comprehensive list can be found in the color manual available in the help feature.
Be sure to run turtle.colormode(255) first if you want to set an RGB value. You might, for example, use turtle.color(75,0,130) to set the color as indigo.
Demonstration of how to draw a square with four different colors
window = turtle.Screen() window.bgcolor("lightgrey") window. title("tuts@codeunderscored:~ Demonstrate how to draw a square with 4 colors") my_turtle = turtle.Turtle() my_turtle.pendown() for current_color in ["red", "green", "purple", "blue"]: my_turtle.pensize(10) my_turtle.color(current_color) my_turtle.forward(200) my_turtle.left(90) window.exitonclick()
Example 1
""" Example 2 """ import turtle def drawSquares(my_turtle, length_of_side, count_squares, distance_apart): """ :param my_turtle: instance of the turtle :param length_of_side: initial length of the square e.g 200 before drawing the inner squares :param count_squares: determines how many squares exist e.g 5 means the count of squares is 5 :param distance_apart: distance from once square to the next :return: """ for n in range(count_squares): for _ in range(4): my_turtle.forward(length_of_side) my_turtle.left(90) my_turtle.penup() x, y = my_turtle.position() my_turtle.goto(x + distance_apart / 2, y + distance_apart / 2) my_turtle.pendown() length_of_side -= distance_apart if __name__=='__main__': window = turtle.Screen() # Setting up the attributes and thee window window.bgcolor("lightgreen") window.title("tuts@codeunderscored: ~ Example 1: How to draw 4 Squares in Python") new_turtle = turtle.Turtle() new_turtle.penup() new_turtle.goto(60, 60) new_turtle.pendown() drawSquares(new_turtle, 200, 4, 10) turtle.done()

Example 2
""" Example 2 """ def drawSquares(my_turtle, side_length, no_of_squares, distance_apart): """ :param my_turtle: instance of the turtle :param side_length: initial length of the square e.g 200 before drawing the inner squares :param no_of_squares: determines how many squares exist e.g 5 means the count of squares is 5 :param distance_apart: distance from once square to the next :return: """ new_turtle = my_turtle.clone() # turtle cloning to avoid restoring changes new_turtle.shape("square") # turtle shape modification for stamping new_turtle.fillcolor(turtle.bgcolor()) # modify turtle fill color for stamping for _ in range(no_of_squares): new_turtle.turtlesize(side_length / 20) # the magic number 20 is the stamp’s default size new_turtle.stamp() side_length -= distance_apart if __name__=='__main__': window = turtle.Screen() # Setting up the window and its attributes window.bgcolor("grey") window.title("tuts@codeunderscored: ~ Example 2: How to draw 4 Squares in Python") new_turtle = turtle.Turtle() new_turtle.penup() new_turtle.goto(10, 10) new_turtle.pendown() drawSquares(new_turtle, 200, 4, 10) turtle.done()

Example 3
""" Example 3 """ def drawSquares(my_turtle, side_length, no_of_squares, distance_apart): """ :param my_turtle: instance of the turtle :param side_length: initial length of the square e.g 200 before drawing the inner squares :param no_of_squares: determines how many squares exist e.g 5 means the count of squares is 5 :param distance_apart: distance from once square to the next :return: """ my_turtle.penup() x, y = my_turtle.position() my_turtle.goto(x - side_length/ 2, y - side_length / 2) # current x, y is positioned at the center my_turtle.pendown() my_turtle.setheading(-45) # square sits on corner instead of on side by default for _ in range(no_of_squares): radius = side_length * 2**0.5 / 2 my_turtle.circle(radius, steps=4) # determines the size of the shape 4 = square, 5=pentagon side_length -= 50 my_turtle.penup() x, y = my_turtle.position() my_turtle.goto(x + distance_apart / 2, y + distance_apart / 2) my_turtle.pendown() if __name__=='__main__': window = turtle.Screen() # window and attributes setup window.bgcolor("blue") window.title("tuts@codeunderscored:~ Example 3: How to draw 4 Squares in Python") new_turtle = turtle.Turtle() new_turtle.penup() new_turtle.goto(10, 10) new_turtle.pendown() drawSquares(new_turtle, 200, 4, 50) turtle.done()

Example 4
""" Example 4 """ def drawSquares(my_turtle, side_length, no_of_squares, distance_apart): """ :param my_turtle: instance of the turtle :param side_length: initial length of the square e.g 200 before drawing the inner squares :param no_of_squares: determines how many squares exist e.g 5 means the count of squares is 5 :param distance_apart: distance from once square to the next :return: """ if no_of_squares < 1: return for _ in range(4): my_turtle.forward(side_length) my_turtle.left(90) my_turtle.penup() x, y = my_turtle.position() my_turtle.goto(x + distance_apart / 2, y + distance_apart / 2) my_turtle.pendown() drawSquares(my_turtle, side_length - distance_apart, no_of_squares - 1, distance_apart) if __name__=='__main__': window = turtle.Screen() # window and attributes setup window.bgcolor("yellow") window.title("tuts@codeunderscored:~ Example 4: How to draw 4 Squares in Python") new_turtle = turtle.Turtle() new_turtle.penup() new_turtle.goto(10, 10) new_turtle.pendown() drawSquares(new_turtle, 200, 5, 40) turtle.done()
