Names and Assignment
Assignment connects a name to a value.
count = 3
Read this as:
Let the name
countrefer to the value3.
Do not read it as "count equals 3" in the same way you would read an equation in mathematics. In Python, assignment is an action. It updates what a name refers to.
Reassignment
A name can point to a different value later:
Output:
3
4
The second assignment does not change the past. It changes what count means
from that point forward.
Reassignment
The same name can refer to a new value later in the program.
Ready to run.
Names Should Help the Reader
This works:
x = 0.42
But this is clearer:
loss = 0.42
Short names are fine for tiny examples, coordinates, and formulas. For
experiments, use names that explain the value's role: loss, learning_rate,
batch_size, token_count.
Names Are Case-Sensitive
Python treats these as different names:
That is usually a bad idea. It is legal, but confusing.
A Name Must Exist Before Use
Using a name before assigning it produces a NameError:
print(score)
If no earlier line assigned score, Python cannot know what value to print.
What value does the final line print?
Compute it first, then check your number.
HintRead from top to bottom
Find the last assignment to steps that runs before print(steps).
SolutionThe current value is five
steps first refers to 2, then reassignment makes it refer to 5. The
print call runs afterward, so it prints 5.
Names Point to Current Values
Assignment is not a permanent truth. It is an instruction that makes a name refer to a value from that point in the program.