Write, Run & Share Python Turtle code online using OneCompiler's Turtle online compiler for free. It's one of the robust, feature-rich online compilers for the Python turtle module, running on Python 3.12. The turtle window is streamed live into the browser — no Python install, no Tk setup, no virtual display to wrangle. The editor shows sample boilerplate code when you choose language as Turtle and start coding.
Turtle graphics was born in the 1960s as part of the Logo programming language, designed by Seymour Papert at MIT to teach kids to think about geometry and code at the same time. Python's turtle module ships in the standard library and brings the same idea forward: a little arrow ("the turtle") sits in the middle of a window, and you tell it to move forward, turn, lift its pen up, change colour. Out of those few commands you get spirals, fractals, polygons, snowflakes, and most introductory CS exercises ever written.
Import turtle, give the turtle some instructions, then call turtle.done() to keep the window open.
import turtle
turtle.forward(100) # move 100 pixels forward
turtle.left(90) # turn 90 degrees counter-clockwise
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.done()
The turtle has a position, a heading (an angle in degrees), and a pen that is either down (drawing) or up (just moving).
import turtle
turtle.forward(100) # alias: turtle.fd
turtle.backward(50) # alias: turtle.bk
turtle.left(45) # alias: turtle.lt
turtle.right(90) # alias: turtle.rt
turtle.penup() # stop drawing
turtle.goto(0, 0) # jump to coordinates
turtle.setheading(0) # face east
turtle.pendown() # start drawing again
turtle.circle(50) # circle of radius 50
turtle.circle(50, 180) # half-circle
turtle.done()
color() sets both pen and fill at once. Wrap a shape in begin_fill() / end_fill() to flood-fill it.
import turtle
turtle.pensize(3)
turtle.color("navy", "skyblue") # pen colour, fill colour
turtle.speed(0) # 1 (slow) … 10 (fast), 0 = instant
turtle.begin_fill()
for _ in range(5):
turtle.forward(120)
turtle.right(144) # exterior angle of a 5-point star
turtle.end_fill()
turtle.done()
Most turtle programs are a for loop with two lines inside it.
import turtle
# Square
for _ in range(4):
turtle.forward(80)
turtle.right(90)
# Any regular polygon
def polygon(sides, length):
for _ in range(sides):
turtle.forward(length)
turtle.right(360 / sides)
turtle.penup(); turtle.goto(150, 0); turtle.pendown()
polygon(6, 60) # hexagon
turtle.done()
A tiny change inside the loop — increase the step, rotate by something other than 90° — and you go from boring squares to hypnotic patterns.
import turtle
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
turtle.bgcolor("black")
turtle.speed(0)
turtle.width(2)
for i in range(180):
turtle.color(colors[i % len(colors)])
turtle.forward(i * 2)
turtle.left(59) # not 60 — the small offset is what creates the spiral
turtle.done()
turtle.Turtle() creates an independent turtle. Useful for animations where two things move at once.
import turtle
screen = turtle.Screen()
screen.bgcolor("white")
t1 = turtle.Turtle()
t1.shape("turtle")
t1.color("green")
t2 = turtle.Turtle()
t2.shape("turtle")
t2.color("red")
t2.penup(); t2.goto(0, -50); t2.pendown()
for _ in range(36):
t1.forward(50); t1.right(10)
t2.forward(40); t2.left(10)
turtle.done()
onkey reacts to a keypress, onclick reacts to a mouse click. You have to call listen() once for keyboard events to start firing.
import turtle
t = turtle.Turtle()
t.shape("turtle")
def up(): t.setheading(90); t.forward(20)
def down(): t.setheading(270); t.forward(20)
def left(): t.setheading(180); t.forward(20)
def right(): t.setheading(0); t.forward(20)
screen = turtle.Screen()
screen.onkey(up, "Up")
screen.onkey(down, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.listen()
# click anywhere to drop a dot
screen.onclick(lambda x, y: (t.penup(), t.goto(x, y), t.dot(10, "red"), t.pendown()))
turtle.done()
Turtle is the friendliest possible way to see recursion in action.
import turtle
def branch(length, depth):
if depth == 0:
return
turtle.forward(length)
turtle.left(25)
branch(length * 0.7, depth - 1)
turtle.right(50)
branch(length * 0.7, depth - 1)
turtle.left(25)
turtle.backward(length)
turtle.speed(0)
turtle.left(90)
turtle.penup(); turtle.goto(0, -200); turtle.pendown()
turtle.color("forestgreen")
branch(100, 8)
turtle.done()