Canvas · turtle editor

Python Turtle

Canvas on the left, turtle editor on the right. Press Run and the pen starts from the middle of the canvas. forward draws a line, right turns. A print lands in the box below.

Canvasx 0 y 0 0°

You are not registered. The editor resets to the square.

Showcase
Output
Run the code. The turtle walks on the canvas.

Both sides work together

The canvas is where the pen stands. The middle is (0, 0). The horizontal line is x, the vertical line is y. Up is +y, right is +x. The pen starts facing right; that direction is 0 degrees.

The code in the editor runs in your browser. It is not sent to a server. Run writes print lines into the box below and draws turtle commands on the canvas. Square, Star, and Flower drop ready-made samples onto that same canvas.

Clear empties the canvas and leaves the code. Reset puts the square sample back in the editor. A syntax mistake is underlined. Press Run and the same mistake is written in plain English, with the line number.

What the pen understands

These names live in the turtle module. Write import turtle and open a pen with t = turtle.Turtle(). The same names also work as turtle.forward(100), which moves the first pen.

  • forward(n) / fd(n)Walks n steps the way the pen faces. If the pen is down, it draws a line.
  • backward(n) / bk(n)Walks back along the same line.
  • right(deg) / left(deg)Turns in place. right(90) is the corner of a square. right(170), thirty-six times, makes a star.
  • penup() / pendown()Lifts and lowers the pen. A lifted pen leaves no mark.
  • goto(x, y)Moves the pen to that point. If the pen is down, it draws the path.
  • circle(r)Draws a circle of radius r. A positive radius curves to the left.
  • color(c) / pensize(n)Sets the ink and the thickness. A color can be a name such as red, or a code such as #facc15.
  • begin_fill() / end_fill()Fills the closed path between the two calls. Draw the shape, then call end_fill.
  • write(text)Leaves text where the pen is standing.
  • hideturtle() / done()hideturtle hides the arrow. done and mainloop do not wait on this page; the drawing is already on the canvas.

A shape from a loop

A square repeats one job four times: 140 steps forward, 90 degrees right. for _ in range(4) writes that. Change the step count to 36 and the turn to 170 and the same pattern becomes a star. The flower sample lays twelve circles, turning 30 degrees between them.

Pick colors from a list with the loop variable: colors[i % len(colors)]. The remainder wraps to the start when the list ends, so twelve circles share six colors.

The numbers above the canvas are the pen’s last place. x and y are canvas coordinates, the third number is the facing angle. 0 faces right, 90 faces up, 180 faces left, 270 faces down.

Next to this page

What this canvas does not do

input() does not ask a question here; put the number in the code. onclick, onkey, and mainloop do not wait for a click. A very long loop would lock the canvas; when the step limit is reached, Run stops and asks you to shorten it. Set the background with turtle.bgcolor("white"). On a light background use t.color("black"), or the cream line disappears.

Frequently asked questions

What is Python turtle?

A drawing module that walks a pen by command. forward draws a line, right turns, circle draws a circle. On this page the pen’s track shows on the canvas to the left.

Why does the drawing reset when I am not registered?

A session without registration does not keep the drawing. When the page opens, the editor returns to the square. Putting a piece in the showcase takes a registration.

How are the canvas and the editor tied?

Run executes the code on the right. turtle commands go to the canvas, print lines go to the box below. Clear wipes the canvas and leaves the code.

Where does the code go?

It does not go to a server. The Python interpreter runs in the browser, and the drawing is produced on your device.

Which commands work?

forward, backward, left, right, goto, penup, pendown, circle, dot, color, pensize, begin_fill, end_fill, write, hideturtle, home, clear, reset, and done. The short names work too: fd, bk, lt, rt, pu, pd.

Why don’t input and mainloop wait?

The page does not listen for clicks or keys. input() stops the program while it waits for a value. mainloop and exitonclick do not close the drawing; the canvas stays open.

Where do I see a syntax error?

The editor underlines the broken spot. Press Run and the same mistake is written in plain English, with the line number. Whatever was drawn before the mistake can stay on the canvas.

How do I change the color and the background?

t.color("#facc15") or t.color("red") sets the ink. turtle.bgcolor("white") lightens the canvas. On a light background, make the pen a dark color.