PY-01-002-P · Primer — Variables & Types
The idea in one sentence
A variable in Python is not a box that holds a value — it's a name you attach to an object, and that object carries its own type.
Why it exists
Every program needs to remember things: a user's age, a price, a list of tasks. You need a way to refer to that data later by a friendly name instead of re-typing it. Python's answer is deliberately simple: you write name = value, and from then on the name points at that value. What makes Python especially beginner-friendly is that you never declare what kind of thing a name will hold — no int age or String title up front. The name is free to point at a whole number now and a piece of text a moment later, because the type lives on the object, not on the name. This design lets you write and change code fast, which is exactly why Python is such a popular first language.
How it works
When you run x = 42, Python creates an integer object 42 somewhere in memory and binds the name x to it. The object knows it's an int; the name x knows nothing except which object it currently points to. Because of that, you can reassign x to a completely different kind of object at any time — Python won't complain. This flexibility is called dynamic typing: types are checked as your program runs, based on the object each name currently references. The built-in function type() asks an object what it is, which is handy for learning and debugging. The core built-in types you'll use daily are int, float, str, bool, list, and dict. Watch how one name freely changes the type of object it holds:
x = 42
print(type(x)) # <class 'int'>
x = "now I'm a string" # same name, brand-new object
print(type(x)) # <class 'str'>
# A quick tour of the core built-in types:
print(type(3.14), type(True), type([1, 2]), type({"a": 1}))
# <class 'float'> <class 'bool'> <class 'list'> <class 'dict'>
The one gotcha
Because a variable is a name pointing at an object, b = a does not copy the data — it makes b a second name for the same object. If that object can be changed in place (like a list), edits made through one name are visible through the other:
a = [1, 2, 3]
b = a # b now points at the SAME list as a
b.append(4)
print(a) # [1, 2, 3, 4] <- a changed too!
Both names see [1, 2, 3, 4] because there is only one list. When you truly want an independent copy, ask for one explicitly (e.g. b = a.copy()).
See it — one name, then a rebind to a new object
flowchart LR
subgraph Before["x = 42"]
N1["name: x"] --> O1["int object<br/>42"]
end
subgraph After["x = 'now a string'"]
N2["name: x"] --> O2["str object<br/>'now a string'"]
O3["int object 42<br/>(no name -> collected)"]
end
Before -->|"reassignment<br/>rebinds the name"| After
The one-line takeaway
A variable is a name bound to an object, the object carries the type, and Python checks that type as it runs — so a name can point at anything, anytime.
Where you'll meet it
You'll lean on this in your very first script when a value "mysteriously" changes because two names shared one list, when type(x) saves you during debugging, and later when you pass a list into a function and discover the function can modify your original. Understanding names-vs-objects now makes mutability, function arguments, and copying feel obvious instead of magical.
Named concepts: names bound to objects, dynamic typing, the core built-in types, type() and reassignment
