Turtle Graphics
Turtle Graphics
Turtle Graphics is a popular drawing library in Python that allows users to create pictures
and shapes by controlling a virtual "turtle" that moves around the screen. It is part of
Python’s standard library and is often used to introduce programming concepts to beginners
in a fun and interactive way.
The turtle module is based on a drawing board-like system where you can control the turtle's
movement using commands to create drawings, patterns, and even animations.
1. Turtle: This is the on-screen pen (or "turtle") that moves around and draws lines
based on commands you give.
2. Drawing Board (Screen): The area where the turtle draws shapes.
3. Commands: You control the turtle by giving commands like moving forward, turning
left or right, lifting the pen, or changing the pen's color.
First, you need to import the turtle module. Here’s a simple example to draw a square:
Python Code:
import turtle
# Drawing a square
for _ in 4: # Repeat 4 times
my_turtle.forward(100) # Move forward by 100 units
my_turtle.right(90) # Turn 90 degrees to the right
1. Movement:
o turtle.forward(x): Move the turtle forward by x units.
o turtle.backward(x): Move the turtle backward by x units.
o turtle.right(angle): Turn the turtle clockwise by angle degrees.
o turtle.left(angle): Turn the turtle counterclockwise by angle degrees.
2. Pen Control:
o turtle.penup(): Lift the pen so the turtle moves without drawing.
o turtle.pendown(): Put the pen down so the turtle draws as it moves.
o turtle.pensize(width): Set the thickness of the pen.
o turtle.pencolor(color): Set the color of the pen.
3. Screen Control:
o turtle.bgcolor(color): Change the background color of the drawing
window.
o turtle.clearscreen(): Clear everything on the screen.
4. Other Commands:
o turtle.circle(radius): Draw a circle with the given radius.
o turtle.speed(speed): Set the turtle's drawing speed (from 1 to 10
or fastest).
import turtle
# Draw a star
for _ in range(5):
star_turtle.forward(150)
star_turtle.right(144) # Turn the turtle right by 144 degrees
1. Drawing a Circle
Python code:
import turtle
2. Drawing a Triangle
Python code:
import turtle
# Draw a triangle
for _ in range(3):
triangle_turtle.forward(100)
triangle_turtle.left(120) # Turn left by 120 degrees
3. Drawing a Hexagon
Python code:
import turtle
# Draw a hexagon
for _ in range(6):
hexagon_turtle.forward(100)
hexagon_turtle.left(60) # Turn left by 60 degrees
4. Spiral Pattern
This example creates a spiral pattern by increasing the length of each forward movement.
Python code:
import turtle
# Create a turtle object
spiral_turtle = turtle.Turtle()
# Draw a spiral
for i in range(50):
spiral_turtle.forward(i * 10) # Move forward by increasing distance
spiral_turtle.right(144) # Turn right by 144 degrees
5. Star Pattern
python code:
import turtle
6. Flower Pattern
This example uses a loop to create a flower-like pattern by drawing multiple circles at
different angles.
Python code:
import turtle
Python code:
import turtle
This example shows how to use multiple colors to create a pattern of squares.
python code:
import turtle
# List of colors
colors = ["red", "blue", "green", "yellow", "purple", "orange"]
9. Concentric Circles
This example draws multiple circles of increasing radius to create a pattern of concentric
circles.
python code:
import turtle
# Create a turtle object
concentric_turtle = turtle.Turtle()
This example shows how to draw multiple polygons by increasing the number of sides.
Python code:
import turtle