To draw a square in Python, we can use the Python turtle module.

```
import turtle
t = turtle.Turtle()
def draw_square(length):
for i in range(0,4):
t.forward(length)
t.right(90)
draw_square(100)
```

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of shapes in Python. For example, we can draw circles and draw triangles easily in Python with the turtle module.

Squares are easy to draw because every side has the same length. We can define a function which takes in the side length and loops four times, drawing each side and rotating 90 degrees until the loop is done.

Below is an example of how to draw a square in Python with the turtle module.

```
import turtle
t = turtle.Turtle()
def draw_square(length):
for i in range(0,4):
t.forward(length)
t.right(90)
draw_square(100)
```

## How to Draw a Square with Different Colors in Python

With the turtle colors in Python, we can change the colors of our shapes easily.

The main function you can use to change the color of a line is with the turtle **pencolor()** function.

Below is an example and the output of how to draw a green square using **pencolor()** in Python.

```
import turtle
t = turtle.Turtle()
t.pencolor("green")
def draw_square(length):
for i in range(0,4):
t.forward(length)
t.right(90)
draw_square(100)
```

With turtle, you can also easily fill shapes with color.

To fill a shape, there are a few steps to take. We use the **fillcolor()** function to define the fill color of our shape, and then use the **begin_fill()** and **end_fill()** functions to define when to begin and end filling shapes with the fill color.

Just like the **pencolor()** function, the **fillcolor()** function takes any valid color given a color mode.

Let’s take the example from above and fill our square with the color ‘light blue’ using **fillcolor()**, **begin_fill()** and **end_fill()** in Python.

```
import turtle
t = turtle.Turtle()
t.fillcolor("light blue")
t.pencolor("green")
t.begin_fill()
def draw_square(length):
for i in range(0,4):
t.forward(length)
t.right(90)
draw_square(100)
t.end_fill()
```

## How to Draw a Rectangle in Python Using turtle Module

One other shape which is easy to make is a rectangle. With the function above, we can modify it to easily define a function which will draw a rectangle easily in Python.

Rectangles have four sides with different lengths for the height and width.

We can create a simple rectangle by defining a function that takes in two integers representing side length and side height. Then we can loop four times, using the **forward()** function to create a side representing either the length or height, then rotating the cursor 90 degrees with the **right()** function.

Below is a simple example of how to use Python to make a rectangle.

```
import turtle
t = turtle.Turtle()
def draw_rectangle(length, height):
for i in range(0,4):
if i % 2 == 0:
t.forward(length)
t.right(90)
else:
t.forward(height)
t.right(90)
draw_rectangle(100, 200)
```

Hopefully this article has been helpful for you to learn how to draw a square in Python.

## Leave a Reply