In this Python tutorial, we will learn about Python Turtle with help of this cheat sheet and we will also cover different examples related to python turtle cheat sheet. And, we will cover these topics.
- What is turtle in Python
- How to install turtle library
- How to import turtle library
- How to set the background color
- How to change the shape of a turtle
- How to draw the shape using turtle
- How to change the speed of turtle
- How to change the color of the arrow or turtle
- How to change the width of the turtle
- How to give the movement to turtle
- How to fill the image with the help of a turtle
- How to implement actions using turtle
- How to write the text with the help of a turtle
- How to add graphics with python turtle
- How to make a grid with python turtle
What is turtle in Python
Turtle is a Python feature or library of python turtle which is used to draw some shapes, pictures on the drawing board.
The turtle is working as a pen which helps the user to draw different shapes on the screen and here screen is used as a drawing board where the user can draw pictures with the help of a turtle.
We can use different functions like a turtle.right(),turtle.forward(),turtle.up(),turtle.left(),turtle.backward() with the help of these functions our turtle can move in the different directs and draw different shapes.
How to install turtle library
In this section, we will learn about how to install a turtle library in python turtle.
The turtle module is used to make objects it also provide graphics and is used to draw different shapes on the screen. Here the screen is used as a drawing board.
The command used for the installation of a turtle library is pip install python turtle.
Syntax:
pip install PythonTurtle
Output:
After running the above syntax we get the following output in which we can see the python turtle is installed successfully.
How to import turtle library
In this section, we will learn how to import the turtle library using the following syntax
Syntax:
From turtle import *
import turtle
The above syntax explains we have to use the module which we have earlier installed as a package under the python files through this we can import the package or the library while performing any logical task as giving a solution to the problem.
How to set the background color
In this section, we will learn about how to set the color or image in the background using python turtle.
As we know turtle is used to draw different shapes and pictures on the screen and we want to set the background color for this screen. We can also change the color of the background.
Syntax:
turtle.bgcolor("black")
The above syntax explains how we can add the “black” background color to the screen where the turtle can easily draw the picture.
Output:
In the following output in which we can see a screen with a “black ” background color.
How to change a shape of a turtle
In this section, we will learn about how to change the shape of a turtle in a python turtle.
The turtle module is used to make objects. We can draw different shapes with the help of a turtle here turtle is works as a pen.
We can also change the shape of the turtle we give different shapes like “arrow”, “circle”, “triangle”, “turtle” etc. Here is the syntax to change the shape of the turtle.
Syntax:
turtle.shape("turtle")
Output:
In the following output in which we can see the turtle is placed on the screen with the help of this turtle, we can draw different shapes.
How to draw the shape using turtle
In this section, we will learn about how to draw different shapes using turtle in python turtle.
Turtle is used as a pen for drawing different shapes on the drawing board here screen is used as a drawing board where we can draw different shapes like “circle”, “triangle”, “star” etc.
Syntax:
turtle.circle(90)
the above syntax explains how to draw the shape of a circle with a radius using a turtle. Here we give 90 degrees as the radius to draw a perfect circle.
Output:
Read How to attach an image in Turtle Python
How to change the speed of turtle
In this section, we will learn about how to change the speed of turtle in python turtle.
Speed is used to change the speed of a turtle by giving the value as an argument. The turtle speed is lies between 0-10.
Speedstrings are drawn by the speed values:
Fastest: 0
slowest: 1
slow: 3
Normal: 6
Fast: 10
Syntax:
turtle.speed(value)
In the above syntax, we can put any value in the argument for giving the speed to the turtle.
How to change the color of the arrow or turtle
In this section, we will learn about how to change the color of the arrow or turtle in the python turtle.
The turtle() method is used to make objects. It is used for drawing different shapes on the screen. We can easily change the color of the turtle.
Syntax:
turtle.color("cyan")
In the above syntax, we give the color in the argument for simply changing the color of the turtle.
Output:
In the following output, we can see a beautiful and colored turtle placed on the screen which attracts the user’s eye.
How to change the width of the turtle
In this section, we will learn about how to change the width of the turtle in python turtle.
The default size of the turtle is 20 pixels we can change the turtle size according to the requirement. If we want to large turtle then we increase the width of the turtle if we want a small turtle then we decrease the width of the turtle size.
Syntax:
turtle.width()
In the above syntax, we can give the value as an argument it requires only one positive argument.
Output:
In the following output in which we can see an arrow that can be drawn with the help of a turtle with width “5”.
How to give the movement to turtle
In this section, we will learn about how to give the movement to turtle in python turtle.
As we know turtle is used for drawing different shapes it works as a pen we can also control the movement of a turtle.
There are four directions where a turtle can move and draw the shape on the screen.
- Forward
- Backward
- Left
- Right
Syntax:
turtle.right(90)
The above syntax describes the turtle’s right direction movement.
turtle.left(90)
The above syntax describes the turtle’s left direction movement.
turtle.forward(100)
The above syntax describes the turtle’s forward direction movement.
turtle.backward(100)
The above syntax describes the turtle’s backward direction movement.
Output:
In the following output in which we can see the turtle move in the forward, right, left, and backward directions.
How to fill the image with the help of turtle
In this section, we will learn about how to fill the image with the help of a turtle in a python turtle.
Turtle is used to draw different shapes and images we can also fill the color with the help of the turtle inside the shape. This color gives the attractive look to the image and our image is beautiful.
Syntax:
tur.fillcolor("yellow")
Output:
In the following output, we can see a hexagon shape filled with yellow color is drawn with the help of a turtle on the screen.
How to implement actions using turtle
In this following section, we will learn about how to implement the actions using turtle in python turtle.
As we all know we can draw different shapes with the help of a turtle here we can simply click the mouse by this action turtle start moving and start drawing the shape on the turtle.
Syntax:
turtle.onclick(func)
In the above syntax, we can use the onclick function for moving the turtle in a different direction.
Output:
In the following output we can see the turtle is moving in the forward direction after that on clicking on them they turn and move again.
How to write the text with the help of a turtle
In this section, we will learn about how to write the text with the help of a turtle in a python turtle.
We write any text we want with the help of the write() function. If users want to write the text on the screen they simply write with the help of the write function.
Syntax:
turtle.write(arg,move=false,align='left',font=('Times New Roman',12,'normal')
- arg: It is used for the info that is written on the screen.
- move=false is used to justify the movement of the turtle.
- align is used to give the direction to the turtle.
- font is used to give the font size, font color to the text.
Output:
In the following output in which we can see the text is written on the screen with the help of the turtle.
How to add graphics with python turtle
In this section, we will learn about how to add graphics in python turtle.
The graphic is used to give the attractive look to our console. It is also used to give definition to our text, images, and pictures.
Code:
In this code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.
- turt.forward(50) is used to move the turtle in the forward direction.
- turt.right(90) is used to move the turtle in the right direction.
from turtle import *
import turtle
turt = turtle.Turtle()
for x in range(4):
turt.forward(50)
turt.right(90)
turtle.done()
Output:
After running the above code we get the following output in which we can see the design are created with the help of graphics.
How to make a grid with python turtle
In this section, we will learn about how to make a grid in python turtle.
A grid is a network of horizontal or vertical lines. The horizontal and vertical lines are connected with each other and a grid is formed where the user can mention the point on the perfect place in the graph.
Code:
In the following output, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.forward(300) is used to move the turtle in the forward direction.
- tur.up() is used to move the turtle in the upward direction.
- tur.down() is used to move the turtle in the downward direction.
- tur.write() is used to write the text on the screen.
- tur.speed(100) is used to give the speed to the turtle.
- tur.backward(300) is used to move the turtle in the backward direction.
- tur.color(‘blue’) is used to give the color to the turtle.
from turtle import *
import turtle
ws=turtle.Screen()
tur=turtle.Turtle()
def draw_yaxs(val):
tur.forward(300)
tur.up()
tur.setpos(val,300)
tur.down()
tur.backward(300)
tur.up()
tur.setpos(val+10,0)
tur.down()
def draw_xaxs(val):
tur.forward(300)
tur.up()
tur.setpos(300,val)
tur.down()
tur.backward(300)
tur.up()
tur.setpos(0,val+10)
tur.down()
def label():
tur.penup()
tur.setpos(155,155)
tur.pendown()
tur.write(0,font=("Verdana", 18, "bold"))
tur.penup()
tur.setpos(290,155)
tur.pendown()
tur.write("x",font=("Verdana", 18, "bold"))
tur.penup()
tur.setpos(155,290)
tur.pendown()
tur.write("y",font=("Verdana", 18, "bold"))
ws.setup(800,800)
tur.speed(100)
tur.left(90)
tur.color('blue')
for i in range(30):
draw_yaxs(10*(i+1))
tur.right(90)
tur.up()
tur.setpos(0,0)
tur.down()
for i in range(30):
draw_xaxs(10*(i+1))
tur.hideturtle()
turtle.done()
Output:
After running the above code we get the following output in which we can see the grid is formed with horizontal and vertical lines.
Related Python tutorials:
- Python Turtle Oval
- Python Turtle Star – How to Draw
- How to Draw Flower in Python Turtle
- Python Turtle Grid
- Python Turtle Pen + Examples
So, in this tutorial, we discussed the Python turtle cheat sheet and we have covered different examples related to its implementation. Here is the list of examples that we have covered.
- What is turtle
- How to install turtle library
- How to import turtle library
- How to set the background color
- How to change a shape of turtle
- How to draw the shape using turtle
- How to change the speed of turtle
- How to change the color of the arrow or turtle
- How to change the width of the turtle
- How to give the movement to turtle
- How to fill the image with the help of turtle
- How to implement actions using turtle
- How to write the text with the help of a turtle
- How to add graphics with python turtle
- How to make a grid with python turtle
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.