Beginner — Day 4
For Loops & Functions
Day Schedule
Online
| Time | Activity |
|---|---|
| 10:00 | For Loops range(), stepping through a list; loop variable i |
| 10:30 | Functions def, parameters, return values; demo calling the same function twice |
| 10:50 | Turtle Library import, coordinate system, penup/pendown — draw on whiteboard |
| 11:00 | Break |
| 11:10 | Breakout Groups Project: Snowflake Art (for loop + function + turtle) |
| 12:20 | Reminders |
In-Person
| Time | Activity |
|---|---|
| 8:00 | Warm-Up Review Day 3: lists and while loops |
| 8:20 | For Loops range(), stepping through a list; loop variable i |
| 8:55 | Functions def, parameters, return values; demo calling the same function twice |
| 9:25 | Turtle Library import, coordinate system, penup/pendown — draw on whiteboard |
| 9:40 | Break |
| 9:50 | Breakout Groups Project: Snowflake Art (for loop + function + turtle) |
| 11:00 | Break |
| 11:10 | Extra Activity Snow Scene: combine loops, functions, and random positioning |
| 11:50 | Open Coding Catch up or extend the snowflake art |
| 12:20 | Reminders |
Learning Objectives
- Create a for loop using range()
- Create a for loop that steps through a list
- Write a simple function with no parameters
- Write a function with parameters
- Use import to load a library and call its functions
Main Topic
# INSTRUCTOR — Day 4: For Loops & Functions (Beginner)
# -----------------------------------------------
# TEACH: A for loop repeats code a fixed number of times.
# range(n) produces numbers 0, 1, 2, ... n-1
# The loop variable (i) holds the current count — it's available inside the loop.
for i in range(5):
print("Step " + str(i)) # prints 0, 1, 2, 3, 4
# TEACH: range can start from a different number
for i in range(1, 6):
print("Step " + str(i)) # prints 1, 2, 3, 4, 5
# TEACH: Functions — define once, call many times.
# def starts the definition; indented block is the body.
# Parameters are the "inputs" inside the parentheses.
def greet(name):
print("Hello, " + name + "!") # name takes a new value each call
greet("Jordan") # name = "Jordan"
greet("Alex") # name = "Alex"
greet("Sam") # name = "Sam"
# TEACH: functions can have multiple parameters
def add(a, b):
print(str(a) + " + " + str(b) + " = " + str(a + b))
add(3, 4)
add(10, 25)
# TEACH: functions with return values (brief intro if time allows)
def double(n):
return n * 2
result = double(7)
print("Double 7 is: " + str(result))
Project — Snowflake Art
# INSTRUCTOR — Day 4: Project: Snowflake Art (Beginner)
# -----------------------------------------------
# TEACH: A snowflake is drawn by going forward and back 6 times,
# rotating 60 degrees each time (6 x 60 = 360 = full circle).
# Wrapping it in a function lets us call it repeatedly — key payoff moment.
# Students see the power of functions visually and immediately.
# TEACH: New concept — libraries and import. Introduce this before touching the code.
# "A library is extra Python code that someone else wrote and packaged up for us to use."
# "import turtle loads the Turtle Graphics library, which adds drawing commands to Python."
# Demo: comment out the import line, run the code → NameError: name 'turtle' is not defined.
# Then un-comment and show it works. This makes import feel necessary, not arbitrary.
#
# TEACH: Turtle coordinate system — draw this on the whiteboard.
# (0, 0) is the center. x increases going right, y increases going up.
# The turtle starts at (0, 0) facing right, holding a pen that draws as it moves.
# penup() lifts the pen so moving doesn't leave a line. pendown() puts it back.
import turtle
def draw_snowflake(size):
# TEACH: size is a parameter — each snowflake can be a different size
for i in range(6): # 6 arms on a snowflake
turtle.forward(size)
turtle.backward(size)
turtle.right(60) # 360 / 6 = 60 degrees per arm
turtle.speed(10) # TEACH: higher = faster drawing (0 is fastest)
turtle.color("white")
turtle.bgcolor("black")
draw_snowflake(100) # TEACH: call it! The function runs all 12 moves for us.
# TEACH: call it again with a different size — same function, different result
draw_snowflake(50)
turtle.done() # TEACH: keeps the window open
# TEACH: ask students — what if we wanted 8 arms instead of 6?
# What two numbers would they change? (range(8) and right(45) — 360/8=45)
Extra — Snow Scene
# INSTRUCTOR — Day 4: Extra Activity: Snow Scene (Beginner)
# -----------------------------------------------
# TEACH: Combine a for loop with a function call and random positioning.
# Students see how loops and functions work together for a big visual effect.
# Key concept: random.randint() returns a different number each time.
import turtle
import random
def draw_snowflake(size):
for i in range(6):
turtle.forward(size)
turtle.backward(size)
turtle.right(60)
turtle.speed(0) # TEACH: speed(0) is the fastest setting
turtle.color("white")
turtle.bgcolor("midnight blue")
for i in range(10): # draw 10 snowflakes
x = random.randint(-300, 300)
y = random.randint(-200, 200)
turtle.penup() # TEACH: lift pen before moving so we don't draw a line
turtle.goto(x, y)
turtle.pendown()
size = random.randint(30, 100) # random size each snowflake
draw_snowflake(size)
turtle.hideturtle()
turtle.done()
# TEACH: challenge — make each snowflake a random color
# turtle.color(random.choice(["white", "light blue", "cyan", "silver"]))
# This must go INSIDE the for loop, before draw_snowflake(size)