Variables and Types
A variable is a label on a value. Python picks the type of the value for you — and the type decides what you can do with it.
Assign, then ask what it is
The = sign binds a name to a value. type() asks Python what kind of value the name currently points at.
Looks can deceive
"10" and 10 look almost identical. They are different types, and the type changes the behaviour.
The types you will meet first
36. Arithmetic keeps them exact.3.14.+ on a string glues rather than adds — see the editor on the left.True or False, the answer to yes/no questions.x = "10" and x = 10 are both fine — they just make x different kinds of thing. This is why type() is your friend: when code behaves oddly, the first question is always "what did Python think this was?"Variables and Types: A Practical Guide
A name is not a box. It is a label that points at a value.
Quick Context
When you write age = 36, Python creates the value 36 and points the name age at it. The value lives on its own; the name is just a label. Assign again and the label moves — the old value is left behind, and once nothing references it, it is cleaned up.
Why types matter
The type of a value decides what the operators mean. + on two ints adds them; + on two strs concatenates them. Neither is "wrong" — the behaviour follows the type. When a program does something surprising, nine times out of ten a value is a different type than you assumed, and type() settles it in one line.
Interactive Exploration Guide
- Read the outputs. In the first editor, check the order: the two values print, then their two types. The last line runs only after the first three have finished.
- Reuse a name. Add
age = "six"as a new last line and run. The labelagenow points at a string — same name, new value, new type. - Compare the arithmetic. The second editor prints
"10"+"10" = "1010"but10+10 = 20. That is the whole lesson in two lines. - Break the glue. Try
print(a + b)in the second editor and run. Python's error message is a type complaint: you cannot add a string and an int, and it tells you exactly which types collided.
Key Takeaway
A variable is a label, not a container. The type of the value it labels is chosen by Python, and it dictates behaviour — especially what + means. Whenever behaviour looks wrong, ask type() first; it is the single most useful question you can ask a running program.