Pythondex
Turtle Python Programs

Draw Spiral Hexagon In Python Using Turtle

Last updated July 3, 2023 by Jarvis Silva

In this tutorial I will show you how to draw Spiral Hexagon using python, we will use the turtle module to create this program in python, turtle is a GUI library with the help of this library you can draw anything in python. 

Python Code To Draw Spiral Hexagon


import turtle

colors = ["cyan","yellow","blue","green","white","red"]

p = turtle.Pen()
turtle.bgcolor("black")

for i in range(300):
    p.pencolor(colors[i%6])
    p.width(i/100+2)
    p.forward(i)
    p.left(59)

Above is the code for drawing Spiral Hexagon, this program is entirely created using turtle functions so let’s see how they work:

  • The code begins by importing the turtle module.
  • A list named ‘colors’ is created, containing six different colors.
  • A turtle object named ‘p’ is created.
  • The background color of the turtle screen is set to black using turtle.bgcolor().
  • A loop is initiated, which will iterate 300 times.
  • Inside the loop, the pen color of the turtle is set based on the current iteration using p.pencolor() and the modulo operator (%).
  • The width of the pen is adjusted based on the current iteration using p.width().
  • The turtle moves forward by a distance equal to the current iteration using p.forward().
  • The turtle turns left by an angle of 59 degrees using p.left().
  • The loop continues, and the turtle creates a spiral pattern by repeating steps 6-9.

Above was a short explaination of the code so now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer or else you can use this online python compiler.

When you run the program and it will open a new window and it will start drawing Spiral Hexagon and below is the finished drawing of beautiful spiral hexagon.

Spiral Hexagon Python Drawing

As you can see, we successfully drew a colourful Spiral Hexagon in python, I hope you were able to run this program successfully. 

Here are some more python drawing tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python guides and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂