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.

Turtle:

  • 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.

1.forward()

  • We can use this method to move the turtle in a forward direction. The line is drawn behind the turtle.
  • Syntax: instance.forward(distance)

2.backward()

  • We can use this method to move the turtle in a backward direction.
  • Syntax: instance.backward(distance)

3.left()

  • Used to turn the turtle in a left direction by a specified angle.
  • Syntax: instance.left(angle)

4.right()

  • 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.

Program

Steps:

  1. First, we have imported the turtle module.
  2. Then, we created a work screen using turtle.Screen() method.
  3. Using a turtle.Turtle() class, a turtle instance is defined.
  4. Execute a loop for 6 times to draw 6 sides of a Hexagon.
  5. For each iteration, the turtle has to move 90 units forward and 300 degrees left.
  6. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *