Python Turtle Cheatsheet

·

4 min read

Python Turtle Cheatsheet

Table of Contents

Why I made this

I made this cheatsheet because one day, I was tutoring a student who needed to use python turtle to draw the US Flag.

In case you're not familiar, turtle is a preinstalled library in python that allows you to draw graphics. Many beginner computer science courses use it to teach students because it is easy to set up and use.

The student was getting stuck on how to add color, and I asked if his teacher gave them any references for when they got stuck. He said yes and showed me a link he was given to the official python turtle documentation.

"Oh great! Did you try using it?"

"No."

Confused, but just chalking it up to laziness, I told him to click the link.

Once the page loaded, I felt a flash of regret as I realized the student wasn't at fault. The first sentence of the page says turtle is a popular way for introducing programming to kids but scroll down for 1 second and tell me if you think any fully aged adult, much less a kid, new to programming is going to be able to parse through that. Take a look for yourself!

Hopefully this cheatsheet is more clear and will help you start using turtle faster because the best way to learn is to start doing!

Import turtle and set up turtle object

import turtle

turtle.title('My Turtle Program')

t = turtle.Turtle()

t.forward(100)

Move turtle

You can move the turtle forward or backwards a particular distance

t.forward(100)

t.backward(100)

Use the shorthand versions if you get tired of writing out forward and backward

t.fd(100)

t.bk(100)

See goto and home for how to move the turtle with x and y coordinates

Turn turtle

You can turn the turtle either right or left at a particular angle

t.right(90)

t.left(90)

Shorthand versions

t.rt(90)

t.lt(90)

goto and home

You can also move the turtle by giving it x and y coordinates. The turtle always starts at (0,0) when you first start the program.

t.goto(100,100)

You can always bring the turtle back to (0,0) using t.home()

t.forward(100)
t.right(90)
t.forward(90)
t.home()

Pen up and down

Sometimes you want to move the pen without drawing. Here's how you draw 2 parallel lines.

t.forward(100)
t.right(90)
t.penup()

t.forward(100)
t.right(90)

t.pendown()
t.forward(100)

Draw a Square

This is how you draw a square.

t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)

or if you want to use a for-loop

for i in range(4):
    t.forward(100)
    t.right(90)

Draw a Rectangle

Similar to drawing a square

t.forward(100)
t.right(90)
t.forward(30)
t.right(90)
t.forward(100)
t.right(90)
t.forward(30)
t.right(90)

Draw a Circle

You can provide a radius to t.circle() to create a circle.

t.circle(100)

Draw a Dot

You can provide a diameter to the t.dot() to create a dot.

t.dot(50)

Draw a Star

for i in range(5):
    t.forward(100)
    t.right(144)

Changing the Screen Color

We can change the screen color on the imported turtle library

import turtle

turtle.bgcolor('blue')

Changing the Pen Color

t.pencolor('red')
t.forward(100)

You can also use Hexcode

# change pen color to green
t.pencolor('#008000')
t.forward(100)

or RGB

# by default the colormode is 1. we need to change it to 255 to use typical rgb values.
# if we want to stay on 1, then we just need to divide each of the rgb values by 255.
turtle.colormode(255)

red = (255, 0, 0)
t.pencolor(red)
t.forward(100)

Filling in a Shape With Color

t.fillcolor('red')

t.begin_fill()
t.forward(100)
t.left(120)
t.forward(100)
t.left(120)
t.forward(100)
t.end_fill()

You can also use Hexcode

# change pen color to green
t.fillcolor('#008000')

or RGB

# by default the colormode is 1. we need to change it to 255 to use typical rgb values.
# if we want to stay on 1, then we just need to divide each of the rgb values by 255.
turtle.colormode(255)

red = (255, 0, 0)
t.fillcolor(red)

Changing the Pen Speed

Sometimes you want the pen to move faster. 0 is the slowest. 10 is the highest.

t.speed(10)

Changing the Pen Size

Sometimes you want to have larger brush strokes

t.pensize(10)

Shorthand for Changing Pen Properties

It can be tedious to write everything out. Here's how you can do it with t.pen()

t.pen(pencolor='red', fillcolor='green', pensize=5, speed=10)

Clear the Screen

This clears the screen but leaves your turtle properties the same

t.clear()

Reset

This resets everything including the turtle properties

t.reset()