Draw Hexagon using Turtle in Python
In this tutorial, we are going to see how to draw Hexagon using Turtle in Python. First, you have to know about the Turtle graphics that is available in Python.
- When a user wants to create pictures and shapes, then turtle can be used which is a library in Python.
- It almost resembles a drawing board. It is possible to move turtle with the help of many turtle functions.
The following are some of the common methods used in the turtle library.
- We can use this method to move the turtle in a forward direction. The line is drawn behind the turtle.
- Syntax: instance.forward(distance)
- We can use this method to move the turtle in a backward direction.
- Syntax: instance.backward(distance)
- Used to turn the turtle in a left direction by a specified angle.
- Syntax: instance.left(angle)
- Used to turn the turtle in a right direction by a specified angle.
- Syntax: instance.right(angle)
Some of the other methods are up(), down(), width(Value), goto(x,y), setx(pos), sety(pos), and etc.
- First, we have imported the turtle module.
- Then, we created a work screen using turtle.Screen() method.
- Using a turtle.Turtle() class, a turtle instance is defined.
- Execute a loop for 6 times to draw 6 sides of a Hexagon.
- For each iteration, the turtle has to move 90 units forward and 300 degrees left.
- So, we used forward() and left() methods.
Let’s have a look at the implementation.
import turtle screen=turtle.Screen() TurtleIns=turtle.Turtle() #iterate loop 6 times for 6 sides for i in range(6): TurtleIns.forward(90) TurtleIns.left(300)
When we run the above code, our output will look like the video given below:
So, I hope that you guys learned something new from this tutorial.