x = x + 5 is an instruction.x, add 5 to it, and put the result back in x. height = 100 # Start at 100m
velocity = -10 # Falling at 10m/s
for second in range(5):
height = height + velocity
print("Time:", second, "s | Height:", height, "m")
range(5) tells the computer to repeat 5 times.
Each time it loops, it "updates" the height box.
Why? Because in the real world, forces (like air resistance) change every millisecond. A single formula can't handle that. A Loop can.
# Calculate height every second for 5 seconds
height = 100
velocity = -10 # falling at 10 m/s
for second in range(5):
height = height + velocity
print("At second", second, "the height is", height)
Key Insight: The = sign means "Update the box with a new value."
Copy the code above into a Colab cell.
Run it: Observe the height dropping by 10m each time.
Refine the "Tick": Change the code to use a small time step (
dt = 0.1
for step in range(10):
height = height + (velocity * dt)
The Physics Link: In the real world, gravity doesn't just "calculate" where you are after 10 seconds. It pulls you every millisecond.
Code Literacy: Introducing the Loop.
for second in range(1, 6):
Explain that the code inside the loop repeats for every "step" in the range.
Visualizing the Step: Introduce
# INITIAL CONDITIONS
y = 100 # Starting height (meters)
v = 0 # Starting velocity (m/s)
g = -9.8 # Gravity (m/s^2)
dt = 0.1 # Time step (0.1 seconds)
# THE SIMULATION (The Loop)
for step in range(10):
v = v + (g * dt) # Update velocity
y = y + (v * dt) # Update position
print("Height:", round(y, 2))
The "Accumulator": Explain why v = v + ... isn't an algebraic equality, but an "Update Command."
The Ordering: "What happens if we update position before we update velocity?"
Use an LLM to make the simulation "smarter" by using a while loop (simulating until a condition is met).
The Guided Prompt:
"I have a Python simulation for a falling object. Currently, it runs for 10 steps. Change this to a
whileloop so the simulation continues as long as the height (y) is greater than zero. Print a message saying 'The object hit the ground!' at the end."
This is where students see the power of coding over calculators.
The Challenge: Ask the LLM to add a simple air resistance formula: drag = -0.1 * v.
Modification: Students must find the line where v is updated and change it to include the drag: v = v + (g + drag) * dt.
Verification: Does the object fall slower? Does it reach a "terminal velocity"?
Prompt Template:
"Write a Python simulation of a falling coffee filter.
Use a
whileloop that runs untilheight > 0.Include a
dragvariable that reduces the acceleration as the object speeds up.Print the final time it took to hit the ground."
Your Job: Look at the code. Can you find the line where the drag is calculated?
Can you make it fall slower?
Find the drag_coefficient in the code.
Double it.
Observe: Does the "terminal velocity" (the constant speed at the end) change?
This is the 'Aha!' moment for students: they are controlling the laws of physics.
Task: Create a simulation where a ball falls. If the height becomes less than zero, change the velocity direction so it "bounces" back up!
Hint: Use an if statement inside your while loop.
"In Weeks 1 and 2, we used Python to solve equations we already knew, like $d = vt$. But today, we unlock the real power of coding. We are going to stop using 'Final State' formulas and start simulating the world one tiny 'tick' at a time. This is how NASA simulates rocket launches and how game engines like Unity calculate physics."
"This is the biggest hurdle for new coders. In your math class, $x = x + 5$ is a headache. In Python, it's a command. Imagine you are standing at position 10. If I tell you `position = position + 2`, you simply take two steps forward. You are now at 12. This 'Update Pattern' is how we simulate motion without needing complex calculus."
"Instead of you typing out every step, we use the `for` loop. The `range(5)` is our clock. Every time the clock ticks, the computer looks at the current velocity and updates the height. As teachers, we can use this to show students exactly how a falling object 'loses' height every second."
"I want you to run the simple loop in your notebook. But then, I want you to look at the 'Delta Time' or `dt`. In physics, we know that motion is continuous. By making `dt` smaller—say, 0.01 instead of 1.0—our simulation becomes much more accurate. Try it and see if the 'jumpiness' of the output changes."
"Standard high school physics usually ignores air resistance because the math is too hard for 10th graders. But with a loop, it’s easy. We just tell the computer: 'Every step, calculate the drag and subtract it from the gravity.' Use the prompt on the screen to have the AI build this 'Complex' model for you. You'll see that the code structure is almost identical to the simple version."
Now, verify the AI. If you increase the drag, does the object slow down? This is where you become a 'Virtual Scientist.' You are testing the parameters of a digital universe. Look for the 'Terminal Velocity'—that moment where the speed stops changing. This is a visual concept that is much easier to see in a simulation than in a textbook diagram."
"Why do we do this? Because it moves students away from 'plugging numbers into a formula' and toward 'understanding the mechanism.' If a student understands that velocity changes position every millisecond, they understand the core of kinematics. You are giving them a 'Numerical Microscope'.
"Your homework is to make a ball bounce. This requires an `if` statement. You're telling the computer: 'Keep falling, BUT if you hit the floor, flip your velocity.' It’s a great exercise in logical 'branching' within a physics context."