Python Basics

How to draw spiral square and star in Python Turtle?

Spiral figures are made by reducing the length of side by a fixed number in each iteration. Following are the steps that you must follow to draw any spiral figure-

  1. Choose a length of the side of a figure and assign it to a variable side. For example, side of a figure is 100 units.
    side = 100
  2. Run a for loop for a considerable times and in that loop use forward() and right() function of turtle module. Pass the side variable to forward() function and pass the value of an exterior angle of a figure to right() function. After that, reduce the length of the side by a fixed number.
    for i in range(100):
       t.forward(side)
       t.right(exteriorAngle)
       side = side - 2

Draw spiral square in Python Turtle

#Python program to draw spiral square in turtle programming
import turtle

t = turtle.Turtle()
side = 200
for i in range(100):
   t.forward(side)
   t.right(90) #Exterior angle of a square is 90 degree
   side = side - 2

Output of the above program

Spiral square in Python Turtle

Draw spiral star in Python Turtle

#Python program to draw spiral star in turtle programming
import turtle

t = turtle.Turtle()
side = 200
for i in range(100):
   t.forward(side)
   t.right(144) #Exterior angle of a star is 144 degree
   side = side - 2

Output of the above program

Spiral star in Python Turtle

Draw spiral triangle in Python Turtle

#Python program to draw spiral triangle in turtle programming
import turtle

t = turtle.Turtle()
side = 200
for i in range(70):
   t.forward(side)
   t.right(120) #Exterior angle of a triangle is 120 degree
   side = side - 3

Output of the above program

Spiral triangle in Python Turtle

Draw spiral pentagon in Python Turtle

#Python program to draw spiral pentagon in turtle programming
import turtle

t = turtle.Turtle()
side = 200
for i in range(104):
   t.forward(side)
   t.right(72) #Exterior angle of a pentagon is 72 degree
   side = side - 2

Output of the above program

Spiral pentagon in Python Turtle

Draw spiral polygon in Python Turtle

#Python program to draw spiral polygon in turtle programming
import turtle
 
t = turtle.Turtle()
numberOfSides = int(input('Enter the number of sides of a polygon: '))
lengthOfSide = int(input('Enter the length of a side of a polygon: '))
exteriorAngle = 360/numberOfSides
for i in range(200):
   t.forward(lengthOfSide)
   t.right(exteriorAngle)
   lengthOfSide = lengthOfSide - 0.5

Output of the above program

User Input in Python Turtle Spiral polygon in Python Turtle

Recommended Posts