# 5 Best Ways to Draw Different Shapes Using the Python Turtle Library

Rate this post

π‘ Problem Formulation: This article aims at providing coders, from beginners to experts, with clear methods on utilizing the Python Turtle library to create various geometric shapes. For instance, if a user needs to draw a square with equal sides, the expected output is a polygon with four equal straight sides and four right angles.

## Method 1: Drawing a Square

Drawing a square using the Turtle library is a beginner-friendly task that involves moving the turtle forward and turning it right by 90 degrees four times. The `forward()` function moves the turtle in its current heading, and the `right()` function turns the turtle clockwise.

Here’s an example:

```import turtle

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("lightgreen")

# Create a turtle named alex
alex = turtle.Turtle()

# Drawing a square
for _ in range(4):
alex.forward(100)
alex.right(90)

# Finish up
turtle.done()```

The output is a green window displaying a square with sides of 100 pixels.

This snippet creates a window, initializes a turtle, and uses a loop to repeat the forward and right turn actions to draw a square. After completion, `turtle.done()` is called to keep the window open.

## Method 2: Drawing a Circle

The Python Turtle library simplifies drawing circles by providing the `circle()` function. This function takes a radius as an argument and draws a circle with the specified radius around the turtle’s current position.

Here’s an example:

```import turtle

t = turtle.Turtle()

t.circle(50)

turtle.done()```

The output is a simple circle with a radius of 50 pixels.

In this example, the turtle draws a circle with a 50-pixel radius around its current position. Once the circle is complete, `turtle.done()` prevents the window from closing immediately.

## Method 3: Drawing a Triangle

To draw an equilateral triangle, we can use the Turtle library functions `forward()` and `left()`. Turning the turtle left by 120 degrees at each corner ensures equal angles.

Here’s an example:

```import turtle

t = turtle.Turtle()

# Drawing an equilateral triangle
for _ in range(3):
t.forward(100)
t.left(120)

turtle.done()```

The output is a triangle with each side 100 pixels long and each angle 120 degrees.

This code sets up a turtle to draw a triangle by moving forward and taking left turns at 120-degree angles. Upon completing the shape, `turtle.done()` is called.

## Method 4: Drawing a Star

The star shape can be drawn by alternating between forward motion and right turns of 144 degrees to get the perfect five-pointed star using the Turtle library.

Here’s an example:

```import turtle

star = turtle.Turtle()

# Drawing a star
for i in range(5):
star.forward(100)
star.right(144)

turtle.done()```

The output is a star with five points.

This code employs a simple loop to instruct the turtle to move forward and turn in a way that creates the star pattern. The `turtle.done()` command displays the result.

## Bonus One-Liner Method 5: Drawing Complex Shapes in a Single Command

The Turtle library allows for the creation of complex shapes with a single directive by using a combination of the `dot()` and `goto()` methods to plot points directly.

Here’s an example:

```import turtle

t = turtle.Turtle()
[ t.goto(x, y) or t.dot() for x in range(-100, 100, 20) for y in range(-100, 100, 20) ]

turtle.done()```

The output is a grid of dots, each separated by 20 pixels both horizontally and vertically.

By using a list comprehension, this one-liner sends the turtle to various coordinates to plot dots, creating a set of points that form a grid pattern.

## Summary/Discussion

• Method 1: Drawing a Square. Strengths: Simple and perfect for beginners. Weaknesses: Limited to squares.
• Method 2: Drawing a Circle. Strengths: Handy for perfect circles. Weaknesses: Cannot be used for ellipses or other ovals.
• Method 3: Drawing a Triangle. Strengths: Easy introduction to angles and turns. Weaknesses: Specifically for equilateral triangles.
• Method 4: Drawing a Star. Strengths: Creates complex shapes with simple repetitions. Weaknesses: Adjusting the angle is necessary for different star types.
• Method 5: Drawing Complex Shapes. Strengths: Allows for creativity and complexity in designs. Weaknesses: May require advanced understanding of loops and coordinates.