Objective
Learning objectives
- Explain a variable as a labeled placeholder for a number, not inherently something to "solve for"
- Correctly apply order of operations to evaluate an expression
- Write and evaluate the same expression as a Python variable formula
- Evaluate a real cost-style formula for several different inputs
Hook
If an AI model costs $0.002 for every 1,000 tokens processed, what's the cost for any number of tokens? Notice this question doesn't have one single numeric answer — it depends entirely on how many tokens. That "depends on" is exactly what a variable exists to capture, and it's worth sitting with before any formal notation shows up.
See it
Think of a variable as a labeled box: tokens is a box, and right now it can hold 500, or 10,000, or any number at all — the box doesn't care which, and nothing about it needs to be "figured out" the way a locked box would. The formula cost = (tokens / 1000) * 0.002 isn't a puzzle with a hidden answer; it's a recipe that works no matter what you put in the tokens box.
If this feels different from what "math" trained you to expect, that's worth noticing directly
A lot of school math presents a letter as something to hunt down — "solve for x." That's one real use of a variable, and Lesson 00-03 gets to it. But it's not the primary one, and treating every variable as a mystery to solve makes the far more common use — a variable as a stand-in for "any number, plug in what you like" — harder to see clearly. This lesson is specifically about that second, more common use.
Name it
This is a variable (tokens), part of an expression ((tokens / 1000) * 0.002) — a combination of numbers, variables, and operations that computes a value once you know what's in the box.
Order of operations decides which part of an expression happens first when there's more than one operation — the same rule you may remember as PEMDAS/BODMAS: parentheses first, then exponents, then multiplication/division (left to right), then addition/subtraction (left to right).
print(2 + 3 * 4)
print((2 + 3) * 4)
14
20
2 + 3 * 4 computes the multiplication first (3 * 4 = 12), then adds (2 + 12 = 14) — not left to right, which would incorrectly give 20. Parentheses override this order explicitly: (2 + 3) * 4 forces the addition first, correctly giving 20. Same three numbers, same three operations, genuinely different answers depending on order — this is exactly why the rule exists and matters.
Code it
def cost(tokens, rate_per_1k=0.002):
return (tokens / 1000) * rate_per_1k
for t in [500, 1000, 2500, 10000, 100000]:
print(f"{t} tokens -> ${cost(t):.4f}")
500 tokens -> $0.0010
1000 tokens -> $0.0020
2500 tokens -> $0.0050
10000 tokens -> $0.0200
100000 tokens -> $0.2000
tokens here is doing exactly what the "labeled box" idea described — the same function, the same formula, evaluated for five completely different values with zero changes to the recipe itself. This is the direct, practical link between "a variable" in math and "a variable" in code: they're the same idea, and you already understand code variables from a prior course if you've taken one.
# Using cost(tokens) = (tokens / 1000) * 0.002, what does cost(50000) equal?
answer = 0.1
print(answer)
Following the recipe exactly: 50000 / 1000 = 50, then 50 * 0.002 = 0.1. There's no trick here — this is the entire point of a variable-based formula: the same steps, applied mechanically, work for any input without needing to be redesigned each time.