Fundamentals 11 min read

Python Turtle Graphics: Installation, Basic Shapes, and Advanced Visual Patterns

This guide explains that the turtle module is included with Python, then provides step‑by‑step code examples for drawing basic shapes like squares, stars, spirals, concentric circles, polygons, fractal trees, as well as advanced patterns such as colorful spirals, Koch snowflakes, Mandelbrot sets, the Chinese flag, and a simple map of China, all illustrated with complete runnable code snippets.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python Turtle Graphics: Installation, Basic Shapes, and Advanced Visual Patterns

The turtle library is part of Python’s standard library, so no separate installation is required; you can simply import it in any script.

Example 1 – Square

import turtle
# Create a canvas
screen = turtle.Screen()
screen.bgcolor("white")
# Create a turtle object
pen = turtle.Turtle()
pen.speed(1)  # set drawing speed
for _ in range(4):
    pen.forward(100)  # move forward 100 units
    pen.right(90)     # turn right 90 degrees
pen.hideturtle()
turtle.done()

Example 2 – Five‑pointed Star

import turtle
screen = turtle.Screen()
screen.bgcolor("black")
pen = turtle.Turtle()
pen.color("yellow")
pen.speed(3)
for _ in range(5):
    pen.forward(200)
    pen.right(144)
pen.hideturtle()
turtle.done()

Example 3 – Spiral Line

import turtle
screen = turtle.Screen()
screen.bgcolor("lightblue")
pen = turtle.Turtle()
pen.color("red")
pen.speed(0)  # fastest speed
for i in range(360):
    pen.forward(i * 0.1)
    pen.right(10)
pen.hideturtle()
turtle.done()

Example 4 – Concentric Circles

import turtle
screen = turtle.Screen()
screen.bgcolor("white")
pen = turtle.Turtle()
pen.speed(0)
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
for i in range(7):
    pen.color(colors[i])
    pen.circle(50 + i * 20)
    pen.up()
    pen.sety((50 + i * 20) * -1)
    pen.down()
pen.hideturtle()
turtle.done()

Example 5 – Polygon (e.g., Pentagon)

import turtle
screen = turtle.Screen()
screen.bgcolor("white")
pen = turtle.Turtle()
pen.speed(0)
sides = 5  # number of sides
angle = 360 / sides
length = 100
for _ in range(sides):
    pen.forward(length)
    pen.right(angle)
pen.hideturtle()
turtle.done()

Example 6 – Fractal Tree

import turtle

def draw_tree(branch_length, t):
    if branch_length > 5:
        t.forward(branch_length)
        t.right(20)
        draw_tree(branch_length - 15, t)
        t.left(40)
        draw_tree(branch_length - 15, t)
        t.right(20)
        t.backward(branch_length)

screen = turtle.Screen()
screen.bgcolor("white")
pen = turtle.Turtle()
pen.left(90)
pen.up()
pen.backward(100)
pen.down()
pen.color("green")
pen.speed(0)

draw_tree(100, pen)
pen.hideturtle()
turtle.done()

Advanced Example – Colorful Spiral

import turtle
import random
screen = turtle.Screen()
screen.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
turtle.colormode(255)  # use RGB colors
for i in range(36):
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    pen.color(r, g, b)
    pen.circle(100)
    pen.right(10)
pen.hideturtle()
turtle.done()

Advanced Example – Koch Snowflake (Fractal Curve)

import turtle

def koch_curve(t, order, size):
    if order == 0:
        t.forward(size)
    else:
        for angle in [60, -120, 60, 0]:
            koch_curve(t, order-1, size/3)
            t.left(angle)

screen = turtle.Screen()
screen.bgcolor("white")
pen = turtle.Turtle()
pen.speed(0)
pen.penup()
pen.goto(-200, 0)
pen.pendown()
for _ in range(3):
    koch_curve(pen, 4, 400)
    pen.right(120)
pen.hideturtle()
turtle.done()

Advanced Example – Simple Mandelbrot Set

import turtle
import math

def mandelbrot(c, max_iter):
    z = 0
    n = 0
    while abs(z) <= 2 and n < max_iter:
        z = z*z + c
        n += 1
    return n

screen = turtle.Screen()
screen.setup(800, 800)
screen.setworldcoordinates(-2.5, -1.25, 1.5, 1.25)
screen.tracer(0, 0)
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()
max_iter = 100
for x in range(-250, 150):
    for y in range(-125, 125):
        c = complex(x / 100.0, y / 100.0)
        m = mandelbrot(c, max_iter)
        color = m / max_iter
        pen.color(color, color, color)
        pen.goto(x/100.0, y/100.0)
        pen.dot(2)
screen.update()
turtle.done()

Example – Drawing the Chinese Flag

import turtle
screen = turtle.Screen()
screen.setup(800, 600)
screen.bgcolor("white")
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
# Red background rectangle
pen.penup()
pen.goto(-400, 300)
pen.pendown()
pen.color("red")
pen.begin_fill()
for _ in range(2):
    pen.forward(800)
    pen.right(90)
    pen.forward(600)
    pen.right(90)
pen.end_fill()
# Function to draw a star

def draw_star(size, x, y):
    pen.penup()
    pen.goto(x, y)
    pen.setheading(0)
    pen.pendown()
    pen.color("yellow")
    pen.begin_fill()
    for _ in range(5):
        pen.forward(size)
        pen.right(144)
    pen.end_fill()
# Large star
draw_star(100, 0, 150)
# Four small stars
stars = [
    (70, -100, 180),  # top‑right
    (70, -100, 306),  # top‑left
    (70, -100, 270),  # bottom‑left
    (70, -100, 90)    # bottom‑right
]
for size, x, angle in stars:
    pen.penup()
    pen.goto(x, 50)
    pen.setheading(angle)
    pen.forward(150)
    pen.pendown()
    draw_star(size, pen.xcor(), pen.ycor())

turtle.done()

Example – Simple Map of China (Geopandas + Matplotlib)

pip install geopandas matplotlib
import geopandas as gpd
import matplotlib.pyplot as plt
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
china = world[world.name == 'China']
fig, ax = plt.subplots(figsize=(10, 8))
china.boundary.plot(ax=ax, linewidth=1, color='black')
china.plot(ax=ax, facecolor='lightgray', edgecolor='black')
ax.set_title('Map of China', fontsize=16)
ax.axis('off')
plt.show()

This collection of snippets demonstrates how the built‑in turtle module can be used for introductory graphics programming, from simple geometric shapes to recursive fractals and basic geographic visualisation.

graphicsPythoncode examplesTutorialprogramming fundamentalsvisualizationturtle
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.