Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to draw different shapes using the Python Turtle library?
The Python Turtle library provides a fun way to create graphics by controlling a turtle that moves around a drawing canvas. In this tutorial, we'll learn how to draw different geometric shapes including squares, rectangles, circles, and hexagons using turtle graphics.
What is Turtle Graphics?
Turtle graphics is a drawing method where you control a "turtle" that moves around the screen, leaving a trail behind it. You can command the turtle to move forward, turn left or right, and lift or lower its pen to create various shapes and patterns.
Drawing a Square
A square has four equal sides and four 90-degree angles. We use a loop to draw each side ?
import turtle
# Create turtle object
t = turtle.Turtle()
turtle.speed(2)
# Draw square
side_length = 100
for i in range(4):
t.forward(side_length)
t.left(90)
turtle.done()
This code moves the turtle forward by the side length, then turns left 90 degrees, repeating this four times to create a square.
Drawing a Rectangle
A rectangle has two pairs of equal sides. We can draw it by alternating between two different side lengths ?
import turtle
t = turtle.Turtle()
turtle.speed(2)
# Draw rectangle
length = 150
width = 80
for i in range(2):
t.forward(length)
t.left(90)
t.forward(width)
t.left(90)
turtle.done()
Drawing a Circle
Turtle provides a built-in circle() method that makes drawing circles simple ?
import turtle t = turtle.Turtle() turtle.speed(2) # Draw circle radius = 80 t.circle(radius) turtle.done()
Drawing a Hexagon
A hexagon has six equal sides. Since the exterior angle of a regular hexagon is 60 degrees, we turn the turtle by 60 degrees after drawing each side ?
import turtle
t = turtle.Turtle()
turtle.speed(2)
# Draw hexagon
side_length = 80
for i in range(6):
t.forward(side_length)
t.left(60)
turtle.done()
Complete Example - Drawing All Shapes
Here's a complete program that draws all four shapes with proper positioning ?
import turtle
# Setup screen
screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Drawing Shapes with Turtle")
t = turtle.Turtle()
t.speed(3)
t.color("blue")
# Draw square
t.penup()
t.goto(-200, 100)
t.pendown()
for i in range(4):
t.forward(80)
t.left(90)
# Draw rectangle
t.penup()
t.goto(-50, 100)
t.pendown()
for i in range(2):
t.forward(120)
t.left(90)
t.forward(60)
t.left(90)
# Draw circle
t.penup()
t.goto(150, 50)
t.pendown()
t.circle(50)
# Draw hexagon
t.penup()
t.goto(-200, -100)
t.pendown()
for i in range(6):
t.forward(60)
t.left(60)
# Keep window open
screen.exitonclick()
Key Turtle Methods
| Method | Description | Example |
|---|---|---|
forward(distance) |
Move turtle forward | t.forward(100) |
left(angle) |
Turn turtle left | t.left(90) |
right(angle) |
Turn turtle right | t.right(45) |
circle(radius) |
Draw a circle | t.circle(50) |
penup() |
Lift pen (no drawing) | t.penup() |
pendown() |
Lower pen (start drawing) | t.pendown() |
Conclusion
The Python Turtle library makes it easy to draw geometric shapes using simple movement and rotation commands. By combining forward() and left() methods in loops, you can create various polygons, while the circle() method provides an easy way to draw curved shapes.
