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
Turtle graphics using Python
Python's Turtle graphics library provides an easy way to create drawings and animations. After importing turtle, you can use commands like forward(), backward(), right(), and left() to move the turtle cursor and draw shapes. By combining these commands, you can create beautiful graphics ranging from simple shapes to complex patterns.
Simple Turtle Commands
forward(10) ? Moves the turtle forward by 10 pixels
backward(5) ? Moves the turtle backward by 5 pixels
right(35) ? Turns the turtle clockwise by 35 degrees
left(55) ? Turns the turtle counter-clockwise by 55 degrees
goto(x,y) ? Moves the turtle to position (x, y)
dot() ? Creates a dot at the current position
shape('circle') ? Changes the turtle shape to a circle
Drawing a Star
Let's draw a 5-pointed star using a loop. We move the turtle forward and turn right by 144 degrees repeatedly ?
import turtle
star = turtle.Turtle()
for i in range(5):
star.forward(100)
star.right(144)
turtle.done()
Drawing Letter E
Here we create the letter E by moving the turtle in all four directions with precise positioning ?
import turtle
t = turtle.Turtle()
t.penup()
t.setpos(-20, 40)
t.pendown()
t.pensize(10)
t.pencolor("blue")
# Top line
t.forward(100)
t.backward(100)
t.right(90)
# Left vertical line
t.forward(100)
t.left(90)
# Middle line
t.forward(100)
t.backward(100)
t.right(90)
# Continue vertical
t.forward(100)
t.left(90)
# Bottom line
t.forward(100)
turtle.done()
Multiple Squares
This example draws multiple squares of increasing size, all starting from the same center point ?
import turtle
mult_square = turtle.Turtle()
def draw_square(length, color):
mult_square.pencolor(color)
mult_square.pensize(2)
for i in range(4):
mult_square.forward(length)
mult_square.right(90)
mult_square.setheading(0)
for size in range(60, 120, 15):
draw_square(size, "blue")
turtle.done()
Spiral Hexagon
This creates a colorful spiral pattern by gradually increasing the forward distance and using different colors ?
import turtle
colors = ["pink", "yellow", "blue", "green", "white", "red"]
sketch = turtle.Pen()
turtle.bgcolor("black")
for i in range(100):
sketch.pencolor(colors[i % 6])
sketch.width(i/100 + 1)
sketch.forward(i)
sketch.left(59)
turtle.done()
Key Turtle Methods
| Method | Purpose | Example |
|---|---|---|
penup() |
Lift pen (no drawing) | Move without drawing lines |
pendown() |
Put pen down (start drawing) | Resume drawing |
pencolor() |
Change pen color | Set line color |
bgcolor() |
Change background color | Set canvas background |
Conclusion
Turtle graphics is perfect for learning programming concepts through visual feedback. Use basic movement commands to create simple shapes, then combine them with loops and functions for complex patterns. The turtle module makes programming fun and interactive for beginners.
