Skip to Content
Course content

84: Introduction to the turtle Module

Click on the "Edit" button in the top corner of the screen to edit your slide content.

What is this turtle module and why are we using it?

Think of the turtle module as a way to "see" your code in action. Instead of just printing text to a console, you're controlling a little cursor (the "turtle") that leaves a trail of ink as it moves across a window. I've always found it's one of the best ways to wrap your head around coordinate systems and loop logic because if you make a mistake in your math, you don't just get an error message—you get a weird, jagged shape on your screen.

How do I actually make the turtle move and draw?

The basics are pretty intuitive. You'll mostly be using forward(), backward(), left(), and right(). The movement is relative to where the turtle is currently facing. If you want to draw a simple square, you just repeat a pattern of moving and turning. Here is how I'd set that up:

import turtle

# We create a turtle object to work with
t = turtle.Turtle()

for _ in range(4):
    t.forward(100) # Move 100 pixels
    t.right(90)    # Turn 90 degrees clockwise

turtle.done() # Keeps the window open when finished

Notice how I used a loop there. You could write t.forward(100) four times, but that's not how we do things in production code. Keep it DRY (Don't Repeat Yourself).

Can I change the colors or move the turtle without drawing?

Absolutely. By default, you're stuck with a thin black line, which is boring. You can use pencolor() to change the ink and pensize() to make the line thicker. But the most important tools here are penup() and pendown(). If you want to move the turtle to a new starting position without leaving a streak across your canvas, you have to "lift the pen."

t.pencolor("blue")
t.pensize(5)

t.forward(50)
t.penup()       # Stop drawing
t.forward(50)   # Move forward without a line
t.pendown()     # Start drawing again
t.forward(50)

I've seen a lot of beginners forget pendown() and then spend ten minutes wondering why their turtle isn't drawing anything. Just keep that in mind.

How do I create more complex shapes?

Once you realize that any regular polygon is just a series of identical turns, you can draw almost anything. The trick is the math: a full circle is 360 degrees. If you want a hexagon, you divide 360 by 6, which gives you 60-degree turns. If you want a star, you'll need a wider angle, like 144 degrees.

# Drawing a simple star
t.pencolor("gold")
for _ in range(5):
    t.forward(150)
    t.right(144)

It's a bit like giving directions to someone who can only walk in straight lines and turn at sharp angles. Once you master that logic, you can build some surprisingly intricate patterns.




📋 Practical Task

Exercise: The Rainbow Hexagon Spiral

Your task is to create a colorful geometric spiral using the turtle module. Instead of a simple shape, you will create a spiral that expands as it turns.

  • Create a list of colors (e.g., ["red", "purple", "blue", "green", "orange", "yellow"]).
  • Use a for loop that runs 36 times.
  • Inside the loop, change the turtle's pen color to the next color in your list using the modulo operator (%).
  • Make the turtle move forward by a distance that increases every time the loop runs (start at 10 and increase by 10 each iteration).
  • Turn the turtle 60 degrees after every movement to maintain the hexagonal shape.

If done correctly, you should see a colorful, expanding hexagon spiral spinning out from the center of the screen.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.