JL Apps

Guides for Python Editor

Python turtle graphics: 5 examples to start with

The turtle module is one of the friendliest ways into programming. You give a little turtle commands – move forward, turn left, change color – and it draws a line wherever it goes. Loops, functions and even recursion suddenly become visible.

Here are five short programs, from a simple square to a recursive tree. Each one runs as it is, and each one teaches one new idea.

Python Editor app icon

The app for this

Python Editor: Learn & Code

Run Python 3 Code Offline

Download on the App Store

The basic commands

All examples use the same handful of commands. The turtle starts in the middle of the screen, facing right. Distances are in pixels, angles in degrees.

  • forward(distance) and backward(distance) – move and draw
  • left(angle) and right(angle) – turn on the spot
  • penup() and pendown() – stop and start drawing
  • color(), pencolor() and pensize() – change how lines look
  • speed(0) – draw as fast as possible
  • turtle.done() – finish the drawing

Example 1: a square

A square is four equal sides with a 90-degree turn after each. Instead of writing forward and left four times, a for loop repeats them. The underscore is a common name for a loop variable you don't need.

import turtle

t = turtle.Turtle()
t.pensize(3)
t.color("royalblue")

for _ in range(4):
    t.forward(150)
    t.left(90)

turtle.done()

Example 2: a filled star

For a five-pointed star, the turtle turns 144 degrees at each point. After five turns it has turned 720 degrees in total – twice around – and is back at the start. color() with two values sets the line color and the fill color; everything drawn between begin_fill() and end_fill() gets filled.

import turtle

t = turtle.Turtle()
t.color("orange", "gold")
t.begin_fill()
for _ in range(5):
    t.forward(200)
    t.right(144)
t.end_fill()

turtle.done()

Example 3: a colorful spiral

Here the line gets longer with every step because the distance depends on the loop counter i. Turning 59 degrees instead of 60 makes the hexagon shape drift slightly with each round, which creates the spiral. i % 6 picks the next of the six colors in turn.

import turtle

t = turtle.Turtle()
t.speed(0)
colors = ["red", "orange", "gold", "green", "blue", "purple"]

for i in range(120):
    t.pencolor(colors[i % 6])
    t.forward(i * 2)
    t.left(59)

turtle.done()
Do it with Python EditorLearn more →

Example 4: a ring of circles

circle(80) draws a circle with a radius of 80 pixels, starting at the turtle's position. Turning by 360 divided by the number of colors after each circle spreads them evenly around the center. Add more colors to the list and the pattern adapts by itself.

import turtle

t = turtle.Turtle()
t.speed(0)
t.pensize(2)
colors = ["red", "orange", "gold", "green", "blue", "purple"]

for color in colors:
    t.color(color)
    t.circle(80)
    t.left(360 / len(colors))

turtle.done()

Example 5: a tree with recursion

A tree is a branch with two smaller trees on top. That sentence is the whole idea of recursion: the function tree() calls itself for each smaller branch until depth reaches 0. The important part is the end of the function: after drawing both sub-branches, the turtle turns back and moves backward, so it ends exactly where it started. Without that, the branches would end up in the wrong places.

Try changing the angle of 25 degrees, the factor 0.7 or the depth – small changes give very different trees. Keep depth moderate: each extra level doubles the number of branches.

import turtle

t = turtle.Turtle()
t.speed(0)
t.left(90)
t.penup()
t.goto(0, -200)
t.pendown()
t.color("saddlebrown")

def tree(length, depth):
    if depth == 0:
        return
    t.pensize(depth)
    t.forward(length)
    t.left(25)
    tree(length * 0.7, depth - 1)
    t.right(50)
    tree(length * 0.7, depth - 1)
    t.left(25)
    t.backward(length)

tree(120, 7)
turtle.done()

Ideas to keep going

Once these work, change one thing at a time and predict what will happen before you run the code. That habit is what turns copying into understanding.

  • Turn the square into a function square(size) and draw several sizes.
  • Ask for the number of star points with input() – an odd number n works with an angle of 180 - 180 / n.
  • Draw a house from a square and a triangle.
  • Use penup() and goto(x, y) to place several drawings side by side.

Common mistakes

Most turtle problems come down to a few classics. If a drawing looks wrong, check these first.

  • Naming your own file turtle.py: Python then imports your file instead of the module, and turtle.Turtle no longer exists. Pick another name.
  • Forgetting pendown() after penup(): the turtle moves but draws nothing.
  • Mixing up left and right: the shape mirrors or never closes.
  • Angles that don't add up: a closed shape needs turns that add up to a multiple of 360 degrees.

Running turtle on iPhone and iPad

On a computer, turtle opens its own window. On an iPhone or iPad you need an app that supports the module. Python Editor does: you can watch the turtle draw stars, spirals and houses, just like in computer science class – also offline. One of its 20 included examples is a colorful spiral you can open and change.

Frequently asked questions

Why does the turtle window close right away on my computer?

When the program ends, the window may close with it. turtle.done() at the end keeps it open until you close it yourself.

How do I make the turtle draw faster?

Call speed(0) on your turtle before drawing. Values from 1 to 10 go from slow to fast; 0 means as fast as possible.

Can I use Python turtle on iPad?

Yes, with an app that supports the turtle module. In Python Editor, the turtle draws right on the device, including offline.

More guides

Python EditorFree on the App StoreGet