Draw any polygon in Turtle – Python

Prerequisite: Python Turtle Basics

Turtle is an inbuilt module of python. It enables us to draw any drawing by a turtle and methods defined in the turtle module and by using some logical loops. turtle drawings are basically drawn using four methods defined in the turtle module.

forward(x): moves the turtle(pen) in the forward direction by x unit.

backward(x): moves the turtle(pen) in the backward direction by x unit.

right(n): rotate the turtle(pen) by n degree in clockwise direction.



left(n): rotate the turtle(pen) by n degree in anticlockwise direction.

In this article, we will learn how to draw different shaped Polygons using Turtle module. Given the number of sides (n) and length of sides (l), one can easily draw any polygon shape. Let’s try to understand it better with the help of examples.

filter_none

edit
close

play_arrow

link
brightness_4
code

# draw any polygon in turtle
  
import turtle
  
# creating turtle pen
t = turtle.Turtle()
  
# taking input for the no of the sides of the polygon
n = int(input("Enter the no of the sides of the polygon : "))
  
# taking input for the length of the sides of the polygon
l = int(input("Enter the length of the sides of the polygon : "))
  
  
for _ in range(n):
    turtle.forward(l)
    turtle.right(360 / n)

chevron_right


Input :

10
100

Output :

Input :

3
150

Output :

Input :

4
150

Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.