Skip to content
VLSI Mentor

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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
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

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
seed = 4210
print(seed)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
4210

One 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.
A name, seed, with an arrow labelled refers to pointing at a separate value, 4210, which is of type int. The name and the value are drawn as separate things connected by an arrow, not as a container holding contents.seeda name you chose4210a value — an intrefers to12
Figure 1 — the name/value model. The name does not contain the value; it points at it. That distinction looks pedantic now and starts paying for itself in the next chapter, where a second name is pointed at the same 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 = 4210 is valid; 4210 = seed is not.
  • It can be done again with a different value. A mathematical statement cannot change its mind. An instruction can be given twice.
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
status = "running"
print(status)

status = "complete"
print(status)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
running
complete

The 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:

Azvya Education Pvt. Ltd.VLSI Mentor
BROKEN ON PURPOSE — error_count is used before it is assigned
print(error_count)
error_count = 0
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
NameError: name 'error_count' is not defined

That 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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
error_count = 0
print(error_count)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
0

Order 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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
n = 4210
t = "axi_smoke"
e = 0
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
seed = 4210
test_name = "axi_smoke"
error_count = 0

Both 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_count beats int_value.
  • Avoid single letters except where the meaning is genuinely universal.
  • Do not pad the name with the obvious. test_name is better than the_name_of_the_test_being_run.
  • Be consistent within a file. If it is run_dir on line 4, do not call it rundir on 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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
test_name = "axi_smoke"
seed = 4210
passed = False

print(test_name)
print(seed)
print(passed)

test_name = "axi_burst"
print(test_name)
print(seed)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
axi_smoke
4210
False
axi_burst
4210

The 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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
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:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
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.