Turtle Commands¶
By the end of this lesson you'll be able to:
- Move a turtle forward, backward, and in any direction using
forward(),back(),left(), andright() - Change the pen color and line thickness with
color()andpensize() - Change the turtle's icon using
shape()
Every turtle program starts with the same three setup lines, and then you give the turtle a sequence of commands — move here, turn that way, draw this color. In this lab you'll try the most important commands and see exactly what each one does.
Welcome to This Lesson!
Hi, I'm Monty! I live inside your browser and I draw whatever you tell me to.
In this lesson you'll learn the commands that make me move, turn, and change color.
Let's code it together!
Moving and Turning¶
The turtle starts at the center of the canvas, facing right. Four commands control movement:
forward(n)— walk forwardnsteps, drawing a lineback(n)— walk backwardnsteps, drawing a lineleft(n)— turn leftndegrees (counter-clockwise)right(n)— turn rightndegrees (clockwise)
Two commands control the pen:
color('name')— set the drawing color (try'red','blue','green','orange')pensize(n)— set the line thickness;1is thin,10is chunky
Scratch Bridge
In Scratch you used "Move 10 steps", "Turn ↻ degrees", and "Set pen color".
forward(), right(), and color() do exactly the same things in Python!
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
What Do You Think Will Happen?
The turtle draws three line segments. What color will each segment be?
Make your guess — then click Run to find out!
Try It Now¶
Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.
Were you right? The turtle draws a blue segment, turns right, draws a red segment, turns right again, and finishes with a green segment — an upside-down U shape in three colors.
If you see a red error message: Check that every command ends with () and that the color name is inside quotes — 'blue' not blue.
How It Works¶
The turtle starts facing right. Each color() call sets the pen color for every line drawn after it — until you call color() again. right(90) rotates the turtle 90 degrees clockwise, making it face downward after the first turn.
The shape('turtle') call on line 3 changes the cursor icon from a plain arrow to a turtle shape. You can also try 'arrow', 'circle', 'square', or 'classic'.
Explanation Table¶
| Line | What it does |
|---|---|
import turtle |
Loads the turtle graphics library |
monty = turtle.Turtle() |
Creates a turtle and names it monty |
monty.shape('turtle') |
Sets the cursor to a turtle icon |
monty.pensize(3) |
Makes the line 3 pixels thick |
monty.color('blue') |
Sets the pen color to blue |
monty.forward(100) |
Moves 100 steps forward, drawing a line |
monty.right(90) |
Rotates 90 degrees clockwise |
Learning Check¶
Your Turn — Add the Missing Line
The program below should draw a green line going right, then a blue line going down.
But both lines come out the same color! Add one line after monty.right(90) to fix it.
Experiments¶
Try these changes. Predict what will happen first, then run to check!
-
Change
monty.shape('turtle')tomonty.shape('circle'). You'll know it worked when the cursor is a filled circle instead of a turtle icon. -
Change
monty.pensize(3)tomonty.pensize(10). You'll know it worked when all three lines are noticeably thicker. -
Add
monty.back(50)after the lastforward(100). You'll know it worked when the turtle backs up, leaving a green line on top of the existing green segment. -
Add
monty.left(90)instead ofmonty.right(90)for both turns. You'll know it worked when the shape curves upward instead of downward. -
Add
monty.hideturtle()as the very last line. You'll know it worked when the turtle icon disappears, leaving a clean drawing.
Great Work!
You've learned the core turtle commands — the building blocks of every drawing in this course!
Try the experiments above to explore what else Monty can do. Let's code it together!