AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Turtle Graphics: Drawing with Code

📚 Technology⏱️ 17 min read🎓 Grade 8
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 17 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Watch someone draw a kolam at their doorstep early in the morning. They almost never trace the whole intricate pattern in one free-flowing sweep. Instead, they repeat a small, exact motif — a loop, a dot-to-dot line, a curve — turning by the same angle each time, until the pattern closes back on itself. A nine-dot kolam and a five-pointed star rangoli are both built the same way: one precise step, repeated, with a controlled turn in between. That is exactly how a computer draws pictures with turtle graphics. You are about to learn to give a set of drawing instructions so precise that a machine with zero artistic sense can reproduce a perfect hexagon, a five-pointed star, or a spiral — just from "move forward" and "turn" commands.

Where turtle graphics came from

Turtle graphics is not a modern invention. It was created in 1967 by the mathematician and computer scientist Seymour Papert, who worked at MIT and helped design a programming language called Logo specifically to teach children how to think in precise, step-by-step instructions. Early versions of Logo controlled real, physical robots shaped like turtles — small motorised machines that dragged a pen across a sheet of paper on the floor as they rolled around a classroom, obeying commands like "move forward 50" and "turn right 90." The robot really did look like a turtle with a pen for a tail, and the name stuck even after the "turtle" moved onto computer screens as a small triangular cursor. Decades later, the same idea — a cursor that remembers where it is and which way it is facing, and moves only when you tell it to — became the basis for the block-based language Scratch, developed at the MIT Media Lab, which many Indian schools use before students move to text-based coding. When you write turtle graphics code in Python, you are using a direct descendant of one of the very first languages ever designed to teach programming.

The turtle's state: position, heading, and pen

Before writing a single command, you need to understand what the turtle actually "remembers" between one instruction and the next. At every moment, a turtle has exactly three pieces of information:

  • Position — its location on the drawing area, given as a pair of coordinates (x, y), just like a point on a graph.
  • Heading — the direction it is currently facing, measured in degrees. A fresh turtle starts facing East (to the right), which is heading 0°. Turning increases or decreases this number.
  • Pen state — whether the pen touching the paper is down (drawing a line as the turtle moves) or up (the turtle glides without leaving a mark).

This is called the turtle's state, and it is the single most important idea in this chapter. Every command you give either changes the position, changes the heading, or changes the pen state — nothing else happens. A common beginner assumption is that commands like "draw a square" exist as single built-in instructions. They do not. The turtle has no idea what a square is. It only knows how to move forward by a distance and turn by an angle. Every shape you will ever draw is built entirely out of these two primitive actions, repeated and combined.

The two commands that build everything

In Python's turtle module, you create a turtle object and then give it instructions:

import turtle

t = turtle.Turtle()
t.forward(100)   # move 100 pixels in the direction it is facing
t.right(90)      # turn 90 degrees clockwise, without moving

turtle.done()

t.forward(distance) moves the turtle in a straight line, in whatever direction it is currently facing, drawing a line behind it if the pen is down. t.right(angle) and its mirror t.left(angle) rotate the turtle in place — the position does not change, only the heading. Notice that forward takes a distance and right/left take an angle — mixing these up (say, calling t.forward(90) when you meant to turn 90 degrees) is the single most common early bug, because Python will run it without complaining; it will just draw the wrong picture.

Worked example: tracing a square, step by step

Let's draw a square with side length 100 and trace exactly what the turtle's state is after every single command, the same way you would trace a loop variable in any program. The turtle starts at position (0, 0), facing East, heading 0°.

t = turtle.Turtle()
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)

Here is the state after each line, tracked as a table. Read it the same way you would trace values in a "dry run" of any algorithm:

Command          Position     Heading   Facing
--- start ---     (0, 0)         0°     East
forward(100)    (100, 0)         0°     East
right(90)       (100, 0)       270°     South
forward(100)  (100, -100)     270°     South
right(90)     (100, -100)     180°     West
forward(100)    (0, -100)     180°     West
right(90)       (0, -100)      90°     North
forward(100)      (0, 0)       90°     North
right(90)         (0, 0)       0°      East

Two things to notice. First, the turtle ends up exactly where it started, facing exactly the way it started — this is what "closing" a shape means, and it is not an accident; it happens because the four turns of 90° added up to a full 360° rotation. Second, look at how the heading cycles: 0° → 270° → 180° → 90° → 0°. Each right(90) subtracts 90 from the heading, wrapping back to a positive number once it goes below zero (270° and "−90°" describe the same direction, facing South). This wraparound is exactly like a clock: 3 o'clock minus 90 "hours" worth of rotation brings you to 12, then 9, then 6, then back to 3.

Here is that same square, drawn out, with the turtle's heading marked at each corner:

y x start (0,0) East, 0° turn right 90° South, 270° turn right 90° West, 180° turn right 90° North, 90° turn right 90° forward(100) four times, right(90) four times blue arrow = turtle's heading at that corner. Total turning = 360°, so the path closes.

The misconception that trips almost everyone up: interior angle vs. turn angle

Now try a regular hexagon (6 equal sides). Since a hexagon has 6 sides, the almost-universal first guess is to compute the hexagon's interior angle — the angle inside each corner — and use that as the turn. For a regular hexagon, the interior angle is 120°, using the formula (n − 2) × 180° / n = (6 − 2) × 180° / 6 = 120°. So a student writes:

for i in range(6):
    t.forward(80)
    t.right(120)

Run this, and something strange happens: it does not draw a hexagon. It draws a triangle — and then traces over the exact same triangle a second time, wasting the other three iterations of the loop. Here is why. The turtle does not turn by the interior angle at all; it turns by the exterior angle, which is how much its heading changes at each corner, not how sharp the corner inside the shape looks. For a triangle, the exterior angle is 120° (360° ÷ 3), which is exactly the number the student typed — so the code above is secretly a perfectly correct triangle-drawing program, not a hexagon-drawing one.

The correct rule, and the one worth memorising, is this: for any convex polygon, the exterior angles always add up to exactly 360°, no matter how many sides it has (this is the same "sum of exterior angles = 360°" fact you meet in the CBSE Class 8 chapter on quadrilaterals and polygons — turtle graphics is that theorem, animated). So for a regular polygon with n equal sides, the turn angle at every corner is:

turn angle = 360 / n

For a hexagon, that is 360 / 6 = 60°, not 120°. The correct code is:

for i in range(6):
    t.forward(80)
    t.right(60)

This traces all six sides, turning a total of 6 × 60° = 360°, and closes perfectly into a hexagon. The general rule — turn by the exterior angle, not the interior angle — is the single fact that separates "I can draw a square by luck" from "I can draw any regular polygon on request."

Why loops matter here: removing repetition

Look back at the square code: forward(100) and right(90), written out four times. Every regular polygon's drawing code has this same shape — one pair of instructions, repeated n times, with n varying only in how many sides the shape has and what angle it turns. Writing this out by hand for, say, a 20-sided polygon would take 40 lines and would be painful to check for mistakes. This is exactly the situation a for loop exists to solve: instead of writing the same two lines n times, you tell the computer to repeat them n times.

def draw_polygon(t, sides, length):
    angle = 360 / sides
    for i in range(sides):
        t.forward(length)
        t.right(angle)

This single function draws a triangle if you call draw_polygon(t, 3, 80), a hexagon if you call draw_polygon(t, 6, 80), or a 20-sided near-circle if you call draw_polygon(t, 20, 30) — the same four lines of logic handle every regular polygon that exists, because the pattern "move, turn by 360/n" is true for all of them. This is the core idea of procedural abstraction: once you notice a repeating pattern and can describe it with a formula, you write it once, as a reusable procedure with parameters, instead of retyping it for every specific case. The variable sides is a parameter — a that gets a real value only when the function is actually called, exactly like x in an algebraic formula only becomes a real number once you substitute one in.

Controlling the pen: drawing without leaving a mark

Sometimes you need the turtle to reposition itself without drawing a line — to start a second shape somewhere else on the page, for instance, or to leave gaps in a dashed pattern. Two commands control this:

t.penup()      # lift the pen — moving now leaves no mark
t.forward(150) # glide to a new spot, nothing is drawn
t.pendown()    # put the pen back down — drawing resumes

Notice that penup() and pendown() change only the third piece of the turtle's state — the pen — and touch neither the position nor the heading. This is worth stating explicitly because it is easy to assume, incorrectly, that lifting the pen somehow "resets" the turtle. It does not. If the turtle was facing 270° before penup(), it is still facing 270° after pendown(); only whether it draws has changed.

Going further: what happens if you turn more than 360/n?

The formula 360/n assumes the turtle moves to the next corner of the polygon each time. But nothing stops you from turning further and skipping a corner on purpose. Try five iterations of forward(120) followed by right(144) instead of the "expected" pentagon angle of 360/5 = 72°:

for i in range(5):
    t.forward(120)
    t.right(144)

This does not draw a pentagon — it draws a five-pointed star (a pentagram), the same shape found in countless rangoli and mandala designs. What's happening is that 144° is exactly double 72°: instead of walking to the neighbouring corner of a regular pentagon, the turtle's steeper turn makes each line "jump over" one corner and connect to the next-but-one vertex, so the path crosses over itself and forms points. This is a genuine extension of the same exterior-angle idea — regular polygons and star polygons are governed by the same "does the total turning add up to a whole number of full turns" rule, just with a different turn size chosen on purpose.

Check your understanding

  1. A turtle starts at (0, 0) facing 0° (East). It executes t.left(90) — no forward yet. What is its new position and new heading? (Careful: has it moved?)
  2. Trace this code by hand, corner by corner, the way the square was traced above, and state the shape it draws and its side length: for i in range(3): t.forward(60); t.right(120).
  3. A student wants to draw a regular 9-sided polygon (a nonagon) with side length 50. Write the right(...) angle they should use, showing the division that produces it.
  4. Explain in your own words why t.right(interior angle) gives the wrong shape, using the hexagon example (120° vs. 60°) as evidence.
  5. A turtle has executed t.penup(), then t.forward(80), then t.right(45). Which of the turtle's three state values changed during this sequence, and which stayed the same at each individual step?
  6. Write a function draw_polygon(t, sides, length) from memory (without looking back), then use it to describe, in words, what two calls — one for a square and one for an octagon — would need to look like.

Summary

A turtle is a drawing cursor with exactly three pieces of memory: where it is (position), which way it is facing (heading), and whether it is currently marking the page (pen state). Every picture it draws, no matter how elaborate, is built from only two primitive actions — moving forward by a distance, and turning by an angle — combined and repeated. Tracing a program by hand, corner by corner, the way you would trace any loop, tells you exactly where the turtle will be and which way it will face at every step, and this is the most reliable way to predict — and debug — what a piece of turtle code will actually draw. The turn angle for a regular polygon with n sides is always 360/n, the exterior angle, never the interior angle inside the shape — a single correction that fixes the most common bug beginners hit. Loops remove the need to retype the same move-and-turn pair for every side, and writing that pattern once as a function with parameters — sides, length — turns a single piece of code into a tool that can draw any regular polygon, or, by turning further than 360/n on purpose, a star.

Think About It

Think about this: How would you explain turtle graphics: drawing with code to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

Practice Exercises

Now it is time to practice! Complete these challenges to solidify your understanding:

  • Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
  • Exercise 2: Find a real-world example where turtle graphics: drawing with code is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
  • Exercise 3: Create a mind-map connecting turtle graphics: drawing with code to at least 3 other topics you have studied.
← Games PygameStatistics →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn