---
title: "Create Polygons with Python"
url: https://blog.creatuwebpymes.com/en/how-to-create-polygons-with-python/
date: 2025-03-10
modified: 2025-06-07
author: "franciscobritoda69"
image: https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/poligonos-en-python-figuras-geometricas.webp
categories: ["Python code"]
tags: ["python code", "python code tutorials", "turtle"]
type: post
lang: en
---

# Create Polygons with Python

## **How to create polygons with Python**

We are going to learn how to create polygons with Python, using the (https://docs.python.org/es/3.9/library/turtle.html#:~:text=Lib%2Fturtle.py-,Introducci%C3%B3n,0)%20en%20un%20plano%20x%2Dy.) module.

We presume that you already know the basics about this library, otherwise you can look at this post about (/en/draw-a-square-with-python/). We have several posts in our (/en/web-programming/) on Python and Turtle you may find interesting.

## **Polygons with Python code
**

If we want to create polygons with Python, the first thing we will do is to import the turtle module, with the reserved keyword '**import**'. This will give us access to the functions and objects of this module.

Then we access the constructor **turtle.Turtle()** which we assign to a variable called **t**.



#### Py

```
#import Turtle module
import turtle
#create the turtle object
t = turtle.Turtle()
```

Then we will create the function that will allow us to create polygons with Python.

This function will have a series of parameters such as **t -> the turtle object** , **d -> the number of pixels of the sides** , **n -> the number of sides** that the polygon will have, and finally **screen_reset** -> reset the screen after each figure has been drawn.



#### Py

```
# polygon function
def polygon(t,d,n,screen_reset):

# reset the screen
if screen_reset == True:
reset()

# angle to indicate the number of sides
angle = 360 / n

# Create the polygon with range()
for i in range(n):
t.fd(d)
t.lt(angle)

# reset function def reset():
turtle.resetscreen()
```

``

Basically the code above inside the polygon functions tells us that if the variable **screen_reset** is True, then every time a polygon is drawn, the screen will be cleared before the next one can be drawn.

However, if the value of screen_reset is False, then we will be able to see several polygons at the same time together.

We also have the variable **angle** which will divide the polygon into the number of sides according to the angle established. To do that, we divide 360 by the number of sides of the polygon and it will give us the correct angle.

Finally we actually create the polygon with an iteration for and range(). The instruction **t.fd(d)** tells us that turtle(t) moves forward . It moves the amount of pixels of the side of the polygon** (d)** and **t.lt (angle)** tells us that turtle moves to the left the angle indicated above.



#### Py

```
# call the function three times polygon(t,100,6,False) polygon(t,120,6,False) polygon(t,150,6,False)
```

!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/poligonos-en-python-figuras-geometricas.webp)

We have called the function three times so we can see three polygons, as the **screen_reset** variable is False. Otherwise we would have just seen the last polygon drawn. 

### How to create a star with Python

Now we will create a star with Python code, as it is one of the most popular geometric shapes.

We will follow the code steps as we did previously. We will also introduce a new parameter to the **star function**, such as **color**. We also have to import the **math** module in conjunction with the turtle module.



#### Py

```
import turtle,math

# create the turtle object
t = turtle.Turtle()

# reset function
def reset():
turtle.resetscreen()

# star function
def star(t,d,screen_reset,color):

# reset
if screen_reset == True:
reset()

# establish the angles
angle = 180 / 5
angle = 180 - angle

# color of the star
t.color(color)

# create the star with for and range()
for i in range(5):
t.td(d)
t.lt(angle)
```

Once we import the turtle and math modules, we create the** turtle - t** object.

We also define the reset( ) screen function as we did in the previous example.

As for the star function itself, we pass the parameters of the **object -> t **, **distance in pixels -> d **, the option to** reset screen** -> **screen_reset** and the **colour** of the figure -> **color**.

The angles of the star are defined with the variable **angle**. In this case, as we want a 5-pointed star, we divide 180 / 5.

We indicate the colour in **t.colour(color)** and the rest as in the previous geometric figure.



#### Py

```
# call the function three times
star(t,250,5,False,'red')
star(t,150,5,False,'green')
star(t,50,5,False,'blue')
```

``

!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/star-in-python-figura-geometrica.webp)

Draw geometric figures with Python and also with the Turtle module, is an exercise that Python students usually come across in their initial projects. If you wish to learn more about this language, here are some ebooks to (https://amzn.to/3Pod40E).

Creatuwebpymes is a (https://creatuwebpymes.com/en/web-design-lanzarote/) based in Lanzarote – Canary Islands. We ❤️ programming in Python, hence the reason for this post.

#### Other Posts in our (https://blog.creatuwebpymes.com/en/web-programming/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/cuadrado-completo-con-borde-negro-blog-1.webp)](/en/draw-a-square-with-python/)

#### (/en/draw-a-square-with-python/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/entrada-metodos-de-listas-en-python-blog.webp)](/en/python-list-methods/)

#### (/en/python-list-methods/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/dibujar-triangulo-con-javascript.webp)](https://blog.creatuwebpymes.com/en/triangle-with-javascript/)

#### (https://blog.creatuwebpymes.com/en/triangle-with-javascript/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/base-de-datos-indexeddb-square.webp)](/en/indexeddb-api-local-database/)

#### (/en/indexeddb-api-local-database/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/Practical-exercises-Python-Lists-blog.webp)](https://blog.creatuwebpymes.com/en/python-lists/)

#### (https://blog.creatuwebpymes.com/en/python-lists/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/python-comprehensions-blog.webp)](/en/comprehension-in-python/)

#### (/en/comprehension-in-python/)
