Python Part I · Module 2 · Variables and Data Types
Variables and Names That Explain Themselves
Every value in the last chapter was printed and then lost. A program that can count errors needs to hold on to a number and refer to it later, and that is what a variable is for. This chapter introduces the mechanism, which takes one line of syntax, and then spends its time on the mental model, which takes longer and matters far more. You will very likely have been told at some point that a variable is a box that holds a value. That picture is comfortable, and it will mislead you within two modules. Python's actual model is simpler and stays correct all the way through the course: a value exists, and a name refers to it. The chapter closes on the other half of its title — writing names a colleague can read without asking you what they mean.
Foundation10 min readPythonPart IFundamentalsVariablesMental Model
Part I · Module 2 · Chapter 2.2 · Variables and Names That Explain Themselves
1. The Problem
Here is a program that counts nothing, because it cannot remember anything:
print(4210)
print(4210)If the seed changes, you have to edit both lines and hope you found them all. In a hundred-line script you will not.
What is needed is a way to say this value, from now on, is called something — and then use the name.
2. Giving a Value a Name
seed = 4210
print(seed)4210One new line, and three new pieces to account for.
seed — a name you chose. It did not exist before this line; you are creating it here.
= — the assignment operator. Read it as "is assigned" or "now refers to". Do not read it as "equals" — §4 is about why.
4210 — a value, exactly as Chapter 2.1 described.
After that line, seed refers to the value 4210. Writing seed anywhere afterwards gives you that value, which is why print(seed) displayed 4210 rather than the word "seed".
A name that refers to a value like this is called a variable.
3. The Model: A Name Refers to a Value
This is the part worth reading slowly.
You may have been taught that a variable is a box, or a container, or a labelled storage location, and that assignment puts a value inside it. That picture is the one most beginners carry, and it produces wrong predictions from Module 5 onward.
Python's model is different, and simpler:
- The value exists.
- The name refers to it.
- Assignment connects a name to a value.
4. = Is Not Equality
In mathematics, x = 5 states a fact that is either true or false.
In Python, seed = 4210 is an instruction: make the name seed refer to the value 4210. It is something that happens, once, at the moment that line runs.
Two consequences follow immediately, and both surprise people:
- The two sides are not interchangeable.
seed = 4210is valid;4210 = seedis not. - It can be done again with a different value. A mathematical statement cannot change its mind. An instruction can be given twice.
status = "running"
print(status)
status = "complete"
print(status)running
completeThe second assignment did not modify the text "running". It pointed the name status at a different value. The word "running" was not edited, altered or overwritten — the name simply stopped referring to it.
5. A Name Must Exist Before You Use It
Python creates a name when you assign to it. Use one before that and it does not exist:
print(error_count)
error_count = 0NameError: name 'error_count' is not definedThat is Chapter 1.11's NameError, now with a more concrete meaning: Python looked for a value associated with the name error_count and found that nothing had been assigned to it yet.
Swap the two lines and it works:
error_count = 0
print(error_count)0Order matters, because assignment is something that happens rather than something that is true.
6. Names That Explain Themselves
The mechanism is now complete. The rest of this chapter is about the half that a reviewer notices.
Compare:
n = 4210
t = "axi_smoke"
e = 0seed = 4210
test_name = "axi_smoke"
error_count = 0Both work identically. Python does not care. But the second version can be read by a colleague at three in the morning without opening anything else, and the first cannot.
The convention in Python is lowercase words joined by underscores — test_name, error_count, run_dir. This style is called snake_case, and it is what essentially all Python code uses for variable names.
A few habits worth adopting now, while you have only a handful of names to manage:
- Say what it holds, not what type it is.
error_countbeatsint_value. - Avoid single letters except where the meaning is genuinely universal.
- Do not pad the name with the obvious.
test_nameis better thanthe_name_of_the_test_being_run. - Be consistent within a file. If it is
run_diron line 4, do not call itrundiron line 40.
Chapter 2.4 covers what Python actually permits in a name, which is a separate question from what is worth writing.
7. Predict, Run, Observe
Predict the full output before running:
test_name = "axi_smoke"
seed = 4210
passed = False
print(test_name)
print(seed)
print(passed)
test_name = "axi_burst"
print(test_name)
print(seed)axi_smoke
4210
False
axi_burst
4210The last two lines are the check. Reassigning test_name changed nothing about seed — the two names are independent, and pointing one somewhere new leaves the other exactly where it was.
8. Exercise
Exercise 1 — three facts. Write a file that gives names to three facts about a test run — its name, its seed, and whether it passed — and prints each on its own line. Use snake_case throughout.
Exercise 2 — predict the error. Predict what happens, then run it:
run_dir = "runs/axi_smoke"
print(run_directory)Which error appears, and what exactly was Python unable to find? Fix it in the way that requires changing one line, then in the way that requires changing the other. Which fix is right depends on what you meant — say which you would choose.
Exercise 3 — rename for a reader. Here is a working fragment with names chosen badly:
a = "axi_smoke"
b = 4210
c = 0
print(a)
print(b)
print(c)Rewrite it with names that explain themselves. You have not been told what c holds — pick a plausible meaning and name it accordingly. Notice that you could not have done that from the original.
Exercise 4 — the model. In your own words, one sentence each: what does status = "running" do, and what does the second status = "complete" do to the text "running"?
(For the second: nothing. It does nothing to it at all.)
9. Summary
- A variable is a name that refers to a value, created by assigning to it.
- The model is value, name, and a connection between them — not a box containing contents. The box picture predicts wrongly from Module 5 onward.
=is an instruction, not a statement of equality. It happens once, when the line runs.- The two sides are not interchangeable, and a name can be reassigned to a different value later.
- Reassignment does not modify the old value. The name simply stops referring to it.
- A name must be assigned before it is used, or Python reports a
NameError. - snake_case, and names that say what they hold. Python does not care; the next person to read your file does.
The mechanism is now yours. The next chapter takes the one idea this chapter introduced and did not finish — what = actually does at the moment it runs — and tests it against the assumption that costs RTL engineers the most time.
Where this fits
Part of the Python curriculum.
