001. Variables
Bind meaningful names to values
001. Variables
Aryan has seen how shell commands like ps aux expose memory info. Now he wants to capture those values in Python so he can do math on them.
He runs ps aux and jots down three numbers for a Chrome process:
- PID: 812
- Memory used: 544 MB
- CPU percent: 6.8
He writes this quickly — single-letter names from his C habit:
p = 812
m = 544
c = 6.8Two weeks later, he opens the file and stares: what is m? megabytes? max? modules?
Variables are named bindings. The name is a contract with the reader. Rename them so future-Aryan (and his code reviewer) knows exactly what each number represents.
💡 Fun fact: Python variables are just labels — unlike C, there is no memory address fixed to a name. Guido van Rossum designed it this way intentionally in 1991 so the runtime could manage memory automatically via reference counting.
⚠️ Watch out: Using single-letter names like a, b, c is legal Python but a maintenance trap. When you revisit the code in a week, you will have no idea what a was supposed to hold.
🤔 Think about it: If you renamed every variable to x, the code would still run — so what is the real cost of a bad name?
Learning objectives
- Assign values to variables with =
- Choose descriptive, meaningful variable names
- Update a variable’s value using arithmetic
Key concepts
- variables
- assignment
- naming
Try it
Concept detail
A variable is a named binding to an object in memory. The name is a contract with the reader. ‘a = 100’ is legal Python but tells the reader nothing. ‘battery_level = 100’ communicates the intent, the unit, and the domain — all for free.
In Python, variables are created on first assignment. There are no type declarations. The name just becomes a label pointing to whatever object sits on the right-hand side. Reassignment (‘battery_level = battery_level - 20’) makes the label point to a NEW integer object; it does not mutate the number 100.
Good naming is the cheapest form of documentation. It costs nothing to type and saves every future reader (including you in 6 months) from reverse-engineering intent from math.
Solution
battery_level = 100
x_position = 0
y_position = 0
battery_level = battery_level - 20
x_position = x_position + 5
y_position = y_position + 3Tests
def test_battery_level():
assert battery_level == 80
def test_x_position():
assert x_position == 5
def test_y_position():
assert y_position == 3