Draw with Python's turtle. Copy any example into the Editor and press โถ Run.
Import the turtle, then give it instructions. The turtle starts in the middle, facing right.
import turtle as tt tt.forward(100) # walk forward 100 steps tt.left(90) # turn left 90 degrees tt.forward(100)
We import turtle as tt so commands are short to type: tt.forward(...), tt.left(...), and so on.
Move and turn the turtle. Angles are in degrees.
| forward(d) | Move forward by d steps. |
| backward(d) | Move backward by d steps. |
| right(a) | Turn right by a degrees. |
| left(a) | Turn left by a degrees. |
| goto(x, y) | Jump to position (x, y). Centre is (0, 0). |
| setx(x) ยท sety(y) | Set just the x or y position. |
| setheading(a) | Face a direction: 0 = right, 90 = up. |
| home() | Go back to the centre, facing right. |
| circle(r) | Draw a circle of radius r. |
| dot(size, color) | Draw a dot at the current spot. |
# Draw a square
import turtle as tt
for side in range(4):
tt.forward(100)
tt.right(90)
Lift the pen to move without drawing; set colours and line width.
| penup() | Lift the pen (move without drawing). |
| pendown() | Put the pen down (draw again). |
| pensize(w) | Set the line thickness. |
| pencolor(c) | Set the line colour. |
| fillcolor(c) | Set the fill colour. |
| color(pen, fill) | Set pen (and optionally fill) colour. |
| begin_fill() / end_fill() | Fill the shape drawn between these. |
# A filled red triangle
import turtle as tt
tt.color("black", "red")
tt.begin_fill()
for side in range(3):
tt.forward(120)
tt.left(120)
tt.end_fill()
Colours can be names ("red"), hex ("#ff8800"), or made with the helpers below.
import turtle as tt
tt.pencolor("skyblue")
tt.pensize(4)
tt.penup()
tt.goto(-50, 0)
tt.pendown()
tt.forward(100)
Handy functions that return a colour you can pass to pencolor, fillcolor, color or bgcolor.
| greyscale(n) | n as a float 0..1 or an int 0..255 โ a shade of grey. |
| hsb(hue, sat, bright) | Hue 0..360; saturation and brightness as float 0..1 or int 0..255. |
# A rainbow spiral using hsb()
import turtle as tt
for i in range(60):
tt.pencolor(tt.hsb(i * 6, 1.0, 1.0))
tt.forward(i * 3)
tt.left(59)
# Shades of grey
import turtle as tt
for i in range(10):
tt.pencolor(tt.greyscale(i / 9)) # 0.0 black .. 1.0 white
tt.forward(100)
tt.right(36)
Show or hide the turtle, change the background, and clear the drawing.
| hideturtle() | Hide the turtle marker. |
| showturtle() | Show the turtle marker. |
| bgcolor(c) | Set the background colour. |
| clear() | Erase the drawing (keep the turtle). |
| reset() | Erase and start the turtle over. |
import turtle as tt
tt.bgcolor("black")
tt.pencolor("yellow")
tt.hideturtle()
for side in range(5):
tt.forward(150)
tt.right(144) # a five-pointed star
Ask the turtle where it is and which way it is facing.
| position() | Current (x, y) position. |
| xcor() ยท ycor() | Current x or y position. |
| heading() | Current direction in degrees. |
| isdown() | True if the pen is down. |
| isvisible() | True if the turtle is visible. |
| towards(x, y) | Angle from here towards (x, y). |
| distance(x, y) | Distance from here to (x, y). |
import turtle as tt
tt.forward(100)
print("I am at", tt.position())
print("facing", tt.heading(), "degrees")
Draw a reference grid with the x and y axes โ handy for planning coordinates.
| showgrid() | Faint lines every 10, bold lines every 100, with the axes on top. |
import turtle as tt tt.showgrid() # default: minor 10, major 100 tt.goto(100, 100) tt.goto(-100, 50)
(0, 0) in the centre, with x increasing to the right and y increasing upwards โ just like maths graphs.Find out how big the drawing area is (in turtle steps), so your drawing can
scale to fit. The canvas is centred, so x goes from -width/2 to +width/2.
| canvas_width() | Width of the drawing area, in turtle steps. |
| canvas_height() | Height of the drawing area, in turtle steps. |
import turtle as tt
w = tt.canvas_width()
h = tt.canvas_height()
print("The canvas is", w, "x", h)
# Draw a border just inside the edges.
tt.penup()
tt.goto(-w / 2 + 10, -h / 2 + 10)
tt.pendown()
for dist in (w - 20, h - 20, w - 20, h - 20):
tt.forward(dist)
tt.left(90)
Turtle()So far we used one turtle via tt.forward(...). You can also make your
own turtle objects with tt.Turtle() and give each one a name. Every turtle
has all the same commands (forward, left, pencolor, โฆ) โ just call them on the turtle,
like jerry.forward(100).
import turtle as tt
# Make two independent turtles.
tom = tt.Turtle()
jerry = tt.Turtle()
tom.pencolor("blue")
jerry.pencolor("red")
# They move on their own, on the same canvas.
for step in range(4):
tom.forward(100)
tom.left(90)
jerry.forward(60)
jerry.right(90)