Variables
A variable in Python is simply a name that refers to an object living somewhere in memory. Unlike many other languages, a Python variable is not a labeled box that holds a fixed-type value — it is more like a sticky note attached to a value. The same name can be re-attached to a completely different kind of value at any time.
Creating a Variable
You create a variable simply by assigning a value to a name with the = operator. There is no separate declaration step, and no type keyword.
name = "Ada Lovelace" age = 36 is_programmer = True print(name) # Ada Lovelace print(age) # 36 print(is_programmer) # True
Behind the scenes, Python creates an object (the string "Ada Lovelace", the integer 36, and so on) and makes the variable name point to it. You can think of assignment as binding a name to an object, not copying a value into a box.
Dynamic Typing
Python is dynamically typed: a variable does not have a fixed type. The type belongs to the object it currently refers to, and that can change freely over the variable’s lifetime.
x = 10 # x refers to an int print(type(x)) # <class 'int'> x = "ten" # x now refers to a str print(type(x)) # <class 'str'> x = [1, 2, 3] # x now refers to a list print(type(x)) # <class 'list'>
Naming Rules and Conventions
Python enforces a small set of hard rules for variable names, and the community follows a larger set of style conventions on top of them.
Names can contain letters, digits, and underscores, but cannot start with a digit.
Names are case-sensitive:
age,Age, andAGEare three different variables.Names cannot be a reserved keyword, such as
if,for,class, orNone.Names cannot contain spaces or symbols like
-,@, or!.By convention (PEP 8), variables use snake_case: total_price, not totalPrice or TotalPrice.
Names starting with an underscore (like
_internal) signal "internal use" by convention.
# Valid names user_name = "sam" _count = 0 total2 = 100 # Invalid names (would raise SyntaxError) # 2total = 100 # user-name = "sam" # class = "Wizard"
Checking for Reserved Keywords
Python ships a keyword module that lists every reserved word, so you never have to memorize the full set.
import keyword
print(keyword.kwlist)
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
# 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', ...]
print(keyword.iskeyword("class")) # True
print(keyword.iskeyword("class_")) # FalseMultiple and Chained Assignment
Python allows you to assign several variables in a single statement. There are two distinct patterns worth knowing.
Pattern | Example | Meaning |
|---|---|---|
Multiple assignment | a, b = 1, 2 | Unpacks the right-hand tuple into separate names. |
Chained assignment | a = b = c = 0 | All three names are bound to the same object. |
# Multiple assignment - great for swapping values too a, b = 1, 2 print(a, b) # 1 2 a, b = b, a # swap without a temporary variable print(a, b) # 2 1 # Chained assignment - all names point to the same object x = y = z = 0 print(x, y, z) # 0 0 0 y = 5 print(x, y, z) # 0 5 0 -> reassigning y does not affect x or z
Reassignment Changes Type Freely
Because a variable is just a name, reassigning it to a value of a different type is completely legal — there is no compiler to stop you.
data = 42
data = "now I'm a string"
data = [1, 2, 3]
data = {"now": "a dict"}
print(data) # {'now': 'a dict'}Object Identity: id() and is
Every object in Python has a unique identity for its lifetime, which you can inspect with the built-in id() function. Two variables that refer to the exact same object will report the same id(), and comparing them with is checks identity rather than equality of value.
a = [1, 2, 3] b = a # b refers to the SAME list object as a c = [1, 2, 3] # c is a DIFFERENT object with equal contents print(id(a), id(b), id(c)) print(a is b) # True - same object print(a is c) # False - different objects print(a == c) # True - equal contents