Variables and Assignment
How Python variables work under the hood, naming rules, and multiple assignment.
2 min read
A variable is a name that points to a value. Python creates one the moment you assign to it — no let, var, or type declaration required.
age = 25
price = 19.99
name = "Ada Lovelace"
is_active = TrueAssignment binds a name to a value
It helps to think of = as "point this name at that value" rather than "put this value in a box called age." In Python, variables are labels, not containers — several names can point to the very same object:
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4] -- a and b point to the same listMutating the list through b is visible through a too, because there's only one list in memory; a and b are just two names for it. This matters more with mutable types like lists and dictionaries than with immutable ones like numbers and strings, where reassignment simply points the name somewhere new instead.
Naming rules
A variable name must start with a letter or underscore, followed by any combination of letters, digits, and underscores. Names are case-sensitive, so total and Total are different variables.
user_name = "ok"
_private = "ok"
2nd_place = "invalid -- can't start with a digit"By convention, Python variables use snake_case (lowercase words separated by underscores), not camelCase. This isn't enforced by the interpreter, but following it is expected in any Python codebase you'll work in.
Multiple assignment
Python lets you assign several variables in one line:
x, y, z = 1, 2, 3This is especially handy for swapping values without a temporary variable:
x, y = y, xPython evaluates the right-hand side fully as a tuple (y, x) before assigning, so the swap happens in one step — no temp variable needed, unlike most other languages.
Constants, by convention only
Python has no true constant keyword. If a value shouldn't change, the convention is to name it in ALL_CAPS as a signal to other developers:
MAX_CONNECTIONS = 100Nothing stops code from reassigning MAX_CONNECTIONS later — it's a naming convention, not enforced immutability. Treat it as a promise to whoever reads the code next, yourself included.
Getting comfortable with how names, assignment, and (for mutable types) shared references work now will save confusion later, especially once functions start passing these same values around.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.