Variables & Naming
Store values under names, understand assignment, and follow Python naming conventions.
- Assign values to variables and reassign them
- Explain why variables are labels, not boxes
- Use augmented assignment operators (+=, -=, *=, /=)
- Follow snake_case naming conventions
Why does a value need a name?
Write a program that computes the average of three quiz scores:
Now suppose the scores change, the teacher wants the average of instead. Without names, the average expression appears in several places and you must find every one of them and edit it by hand. That is a recipe for missing one.
A mathematician solves this by naming quantities: write once, then refer to them forever after. A program has the same need: values appear again and again, and the machine must find the current one every time. Python’s answer is the variable, a name that points at a value. Once score1 names , you can write score1 any number of times and Python looks up its current value each time.
= binds a name to a value
In math, “let ” pins the symbol to the number . Python does the same thing with exactly the same symbol:
score1 = 7.5
score2 = 8.5
score3 = 9.0
average = (score1 + score2 + score3) / 3
print(f"{average:.2f}") # 8.33The right-hand side is evaluated first, and only then does the name on the left start pointing at the result. If you changed the scores and ran the file again, the same computation would use the new values, the names give the machine a place to look up “the current value of ”.
Names can be re-pointed
Here is where a variable is not like a math symbol. In math, is a statement with no solution. In Python it is a perfectly ordinary instruction, read right to left:
says “the next value of is the current one, plus one.” The instant you see this in Python, you are counting:
count = 0
count = count + 1 # old value 0 was read, 1 was computed, the name now points at 1
count = count + 1 # now count names 2Reassignment is re-pointing a label, not filling a box. The old value is not “changed” or “replaced”, the name simply looks at a different value now.
Reading-updating-storing is one gesture: +=
The pattern above, read count, add 1, point count at the result, is so common that Python has a shorthand. Say the step size is and you are moving through a sequence generically:
Printed out, the update reads x = x + h. Python merges the read and the store into one operator:
x = 5
x += 1 # same as x = x + 1 -> 6
x -= 2 # same as x = x - 2 -> 4
x *= 3 # same as x = x * 3 -> 12
x /= 4 # same as x = x / 4 -> 3.0Read x += h aloud as “advance x by h”, a single motion, the way the recurrence does.
A name you can say out loud
Almost any word works as a name, but almost anything being correct is not the same as being good. Which is more informative in a reading of a report script?
x = 87.5 # names a number, nothing more
quiz_score = 87.5 # names the quantityA few rules and one habit:
- A name starts with a letter or underscore and may only contain letters, digits, underscores,
second_score✓,2nd_score✗. - Python’s convention is snake_case: lowercase words joined by
_, sostudent_name, notstudentName. It matches how the names read aloud:quiz_scoreis the quiz score. - A small set of words is reserved,
if,for,class,True,False, and cannot be names.
You will reread your own code more often than you write it; the name you choose while writing is what pulls meaning back out later.
A worked example: the running tally
Reassigning pays off the moment a quantity must be built step by step, the recurrence with the running sum as :
total = 0
total += 8.5 # total becomes 8.5
total += 9.0 # then 17.5
total += 10.0 # then 27.5
average = total / 3
print(f"{average:.2f}") # 9.17Each += advances one step: read the current value, add, point the name at the result. The names total and average keep the two quantities distinct, so the recipe reads as what it does.
Common pitfalls
- Using a reserved word as a name.
class = "Math"raises aSyntaxError,classis reserved. - Starting with a digit.
2nd_place = "B"is invalid;second_place = "B"is fine. - Confusing
=and==.=points a name at a value;==asks whether two values are equal. The one-character slip changes a statement into a question. +=writes to a name that must already exist.total += 1on a name never assigned raises aNameError. The gesture reads the current value first; a name with nothing to read has no current value.
🧩 Challenges
🧩 Challenge, think first, then reveal
If x = 5 and then y = x, and then x = 10, what is y? Explain why in terms of “names point to values” rather than “boxes contain values.”
💡 Answer: y is still 5. When y = x ran, both names pointed at 5. Re-pointing x at 10 moves x's label; y still points at 5. Labels point; nothing is "copied into a box".
🧩 Challenge, think first, then reveal
Write a program that swaps two variables: a = 7, b = 3. After the swap, a should be 3 and b should be 7. Do it without a temporary variable (Python has a neat trick).
💡 Answer: a, b = b, a, Python evaluates the right side first (both old values), then points the left-hand names at them. No temp variable needed.
🧩 Challenge, think first, then reveal
Which of these are valid variable names, and why do the invalid ones fail: _count, 2nd, my-name, total, class?
💡 Answer: _count ✓ (underscore start is fine), 2nd ✗ (starts with a digit), my-name ✗ (the hyphen is the minus operator, not allowed in a name), total ✓, class ✗ (reserved keyword).
🤔 Socratic Questions
- Why does Python choose
snake_caseovercamelCase? What does the underscore visual metaphor suggest about how to read variable names? x += 1andx = x + 1give the same result for numbers. Can you think of a reason a language might still offer both forms?- If variables are labels, not boxes, what happens when you write
a = [1, 2, 3]thenb = athenb.append(4)? Doesasee the4? (Try it, this previews mutable objects, covered much later.)
✅ Quick check
1. What is the value of y after: x = 10; y = x; x = 20?
2. Which is a valid Python variable name?
3. What does x evaluate to after: x = 5; x += 3; x -= 1?