top of page
Search

Python for Kids: Draw Your First Shapes with Turtle (Full Code)

5 days ago
4 min read

Python is a great first text coding language for kids, and the easiest way to start is with turtle: a little on-screen "robot" that draws a line wherever it drives. In about 20 minutes, a child aged roughly 9 and up can write a real Python program that draws shapes, a star and a rainbow spiral. Below is the full code, how it works, and how the same ideas carry straight over to programming real robots.

Why turtle is the best first Python project

Turtle comes built into standard Python, so there is nothing extra to install or buy. Kids type a few commands, press Run, and immediately see the result. That fast feedback is what keeps beginners going.

It also teaches the exact thinking a robot needs. A turtle only knows how to drive forward and turn, which is also all a two-wheeled robot base does. When kids work out that a square is "forward, turn 90 degrees, four times," they are already planning a robot path.

  • No hardware needed: it runs on any Windows, Mac or Linux computer with Python.

  • Real text code: the same Python used in schools, colleges and industry.

  • Visual: mistakes show up as wonky drawings, which makes debugging fun instead of frustrating.

If your child is still deciding between block coding and typing code, our guide Scratch to Python: when should kids switch? can help you decide whether now is the time.

Step 1: Set up Python

  1. Download Python from python.org and install it (on Windows, tick "Add Python to PATH").

  2. Open IDLE, the simple editor that comes with Python.

  3. Choose File → New File, paste in the code below, save it as shapes.py, then press F5 to run it.

Note: don't name your file turtle.py. Python would try to import your own file instead of the real turtle library, and the program will not run.

Step 2: Your first shape (a square)

Start small. These six lines draw a square:

import turtle

t = turtle.Turtle()

for _ in range(4):
    t.forward(100)   # drive 100 steps
    t.left(90)       # turn 90 degrees

turtle.done()

Ask your child to predict what happens if you change 4 to 3 and 90 to 120. (A triangle!) Guess first, then run it. That habit of predicting and testing is the heart of engineering.

Step 3: The full project

This version adds a reusable function, a star and a rainbow spiral. We tested the logic before publishing; it uses only the standard turtle library.

import turtle

screen = turtle.Screen()
screen.bgcolor("black")

t = turtle.Turtle()
t.speed(0)        # 0 = fastest
t.pensize(2)

def polygon(sides, length):
    """Draw any regular shape: 3 = triangle, 4 = square, 6 = hexagon."""
    angle = 360 / sides          # the exterior angle
    for _ in range(sides):
        t.forward(length)
        t.left(angle)

def move_to(x, y):
    """Lift the pen, jump somewhere new, put the pen down."""
    t.penup()
    t.goto(x, y)
    t.pendown()

# 1. A row of shapes
t.pencolor("cyan")
move_to(-250, 150)
for sides in [3, 4, 5, 6]:
    polygon(sides, 60)
    t.penup()
    t.forward(120)
    t.pendown()

# 2. A five-pointed star
t.pencolor("yellow")
move_to(-75, 0)
for _ in range(5):
    t.forward(150)
    t.right(144)

# 3. A rainbow spiral
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
move_to(0, -150)
for i in range(60):
    t.pencolor(colors[i % len(colors)])
    t.forward(i * 2)
    t.left(91)

turtle.done()

How the code works

  • polygon(sides, length) is a function, a named set of steps you can reuse. Each turn is 360 ÷ sides, because a robot that drives around any closed shape turns a full 360 degrees in total.

  • move_to(x, y) lifts the pen so the turtle can travel without drawing, just like a robot driving to a new spot before starting a task.

  • The star turns 144 degrees at each point. Five turns of 144 add up to 720, two full circles, which is why the lines cross.

  • The spiral turns 91 degrees instead of 90. That extra degree makes each square slightly rotated, and the growing forward() distance makes it spiral outward. The % operator cycles through the colors list.

From turtle to real robots

Everything here maps onto a real robot. forward() becomes "drive this many centimeters," left() becomes a turn, and a function like polygon() becomes a reusable mission routine. The difference is that real motors slip and real floors are not perfect, so robots need sensors to stay accurate. Our post on gyro turns vs encoder turns in Python shows the next step.

When your child is ready for hardware, our mBot2 course and LEGO SPIKE Prime course both move into Python on a real robot. Not sure which kit to buy? See our coaches' kit guide.

Challenges to try next

  • Draw a house: a square with a triangle on top.

  • Make polygon() take a color as a third input.

  • Change the spiral's 91 to 89, 121 or 145 and see what patterns appear.

  • Use a loop to draw 36 circles, turning 10 degrees between each, to make a flower.

Common questions

What age can kids start Python?

Many kids are ready around 9 to 11, once they type comfortably and have tried block coding. Younger children usually do better starting in Scratch, and some motivated younger kids are ready sooner.

Do we need to buy anything?

No. Python and turtle are free and run on almost any laptop or desktop computer. A robot kit only comes later, if your child wants to move code into the real world.

My child's drawing looks wrong. What's the most common bug?

Indentation. In Python, the lines inside a for loop or function must be indented by the same amount. If a line is not indented, it runs only once, after the loop ends.

Keep learning with Fusion Robotics Education

Start with our free self-paced courses, including Scratch for younger beginners. For the full catalog, try All-Access for $39/month with a 7-day free trial. Prefer a live coach? Remote 1-on-1 lessons are $149/month; text (619) 786-5731 to set one up.

In San Diego? Our weekend group sessions in Scripps Ranch are 1.5 hours a week for ages 6–18, with at most 10 kids and every child on their own project at their own pace. The first session is free: text (619) 786-5731 to book.

 
 
 

Comments


bottom of page