Python Turtle Graphics
Creative Coding with Turtle Graphics
Revision of loops, functions , selectors and list.
Learning Objectives
Understand and use the turtle module
Revise for loops, if statements, and functions
Use lists to manage colors or directions
Apply conditionals within loops to vary behavior
Create a personalized turtle drawing pattern
1. Turtle Programming in Python
Step-by-step basics:
Import the turtle module:
import turtle
Create turtle screen and turtle object:
screen = turtle.Screen()
t = turtle.Turtle()
Common commands:
t.forward(100)
t.backward(50)
t.right(90)
t.left(45)
t.penup()
t.pendown()
t.color("blue")
t.begin_fill()
t.end_fill()
Drawing a simple square:
for _ in range(4):
t.forward(100)
t.left(90)
To finish:
turtle.done()
2. Turtle Snowflake Challenge
Goal: Create a snowflake pattern using Turtle and loops.
Step-by-step:
1. Import modules and create turtle:
import turtle
import random
snowflake = turtle.Turtle()
snowflake.speed(0)
1. Define colors:
colors = ["blue", "cyan", "white", "lightblue"]
1. Create a function to draw one snowflake arm:
def draw_branch(branch_len):
for i in range(3):
for j in range(3):
snowflake.forward(branch_len / 3)
snowflake.backward(branch_len / 3)
snowflake.right(45)
snowflake.left(90)
snowflake.backward(branch_len / 3)
snowflake.left(45)
snowflake.right(90)
snowflake.forward(branch_len)
1. Draw full snowflake:
for i in range(8):
snowflake.color(random.choice(colors))
draw_branch(100)
snowflake.left(45)
1. Finish drawing:
turtle.done()
Challenge 2
Create a pattern using turtle that includes:
A for loop
A function
A list for colors or angles
An if / else block
Example Starter:
import turtle
pen = turtle.Turtle()
colors = ['red', 'blue', 'green', 'purple', 'orange']
def draw_pattern():
for i in range(50):
pen.color(colors[i % len(colors)])
pen.forward(i * 5)
if i % 2 == 0:
pen.left(90)
else:
pen.right(60)
draw_pattern()
turtle.done()
Try different shapes, varying pen sizes or speeds.
Optional Extension
Add user input for number of loops or shape size
Menu to choose between different drawing modes
Save drawing using .getcanvas().postscript()