MYP Digital Design
Turtle reference
Turtle command cheatsheet
This cheatsheet is your quick guide to the most useful Python Turtle commands.
Getting Started
| Command | What It Does | Example |
|---|---|---|
import turtle |
This is the very first thing you need to do! It brings the turtle library into your program. | import turtle |
t = turtle.Turtle() |
Creates a turtle for you to control. We'll call it 't'. | t = turtle.Turtle() |
Moving the Turtle πΆββοΈ
These commands tell your turtle where to go. The number inside the parentheses is the number of "steps" or "degrees."
| Command | What It Does | Example |
|---|---|---|
t.forward(steps) |
Moves the turtle forward. | t.forward(100) |
t.backward(steps) |
Moves the turtle backward. | t.backward(50) |
t.right(degrees) |
Turns the turtle to its right. | t.right(90) |
t.left(degrees) |
Turns the turtle to its left. | t.left(45) |
t.goto(x, y) |
Moves the turtle to a specific location on the screen. | t.goto(50, -20) |
t.circle(radius) |
Draws a circle. The radius is how big it is. | t.circle(50) |
Changing the Pen βοΈ
These commands control whether the turtle draws a line or not.
| Command | What It Does | Example |
|---|---|---|
t.penup() |
Lifts the pen up. The turtle can move without drawing. | t.penup() |
t.pendown() |
Puts the pen back down. The turtle will start drawing again. | t.pendown() |
t.pensize(size) |
Changes how thick the line is. | t.pensize(5) |
t.color("color_name") |
Changes the color of the pen. Use quotes! | t.color("red") |
Filling Shapes π¨
You can use these commands to fill in a shape with a color.
| Command | What It Does | Example |
|---|---|---|
t.begin_fill() |
Starts the "filling" process. | t.begin_fill() |
t.end_fill() |
Stops filling. Everything between this and begin_fill() will be filled. |
t.end_fill() |
t.fillcolor("color_name") |
Chooses the color to fill with. | t.fillcolor("blue") |
Helpful Reminders π‘
- Variables: A variable is like a container for information. You can use it to store numbers or text.
size = 100ormy_color = "red". - Loops: A
forloop is used to repeat code a certain number of times. It's super useful for drawing shapes!python # This will repeat the code inside 4 times for i in range(4): t.forward(50) t.left(90) - Selection: An
ifstatement lets your program make a decision.python # This checks if the color is "red" if my_color == "red": t.color("red")
Screen functions
onclick() will execute a function, providing the (x,y) coordinates of the mouse click.
Example:
def handle_screen_click(x,y):
print("You clicked at ",x,y)
screen = turtle.Screen()
screen.onclick(handle_screen_click)