A variable is simply a name that refers to a value stored in memory. Instead of writing the number 25 everywhere in your code, you can store it in a variable called age and reuse that name — if the value needs to change later, you only update it in one place. Python makes creating variables about as simple as it gets.
Creating a Variable
Unlike many languages, Python does not require you to declare a variable's type ahead of time. You just assign a value with the = operator, and the variable is created on the spot:
name = "Alice"
age = 25
height = 5.6
is_student = True
print(name, age, height, is_student)
Output:
Alice 25 5.6 True
This is called dynamic typing — Python determines the type of a variable from the value you assign to it. You can check a variable's type at any time with the built-in type() function, a concept covered more fully in our guide to Python data types.
Reassigning Variables
Because Python doesn't lock a variable to a fixed type, you can reassign it to a completely different type later on — this is perfectly legal, if not always good practice:
value = 10 # value is an int
print(type(value))
value = "ten" # now value is a str
print(type(value))
Output:
<class 'int'>
<class 'str'>
Multiple Assignment
Python lets you assign several variables in a single line, which is handy for setting up related values at once:
# Assign different values to multiple variables
x, y, z = 1, 2, 3
print(x, y, z)
# Assign the same value to multiple variables
a = b = c = 0
print(a, b, c)
Output:
1 2 3
0 0 0
Variable Naming Rules and Conventions
Python enforces a few hard rules for variable names, plus a widely followed style convention:
- Names can contain letters, digits, and underscores, but cannot start with a digit (
1valueis invalid,value1is fine). - Names are case-sensitive —
ageandAgeare two different variables. - You cannot use Python's reserved keywords as variable names, such as
if,for, orclass. - By convention (PEP 8), Python variables use snake_case — lowercase words separated by underscores, like
total_pricerather thantotalPriceorTotalPrice.
Descriptive names make a real difference in readability — total_price communicates intent far better than a generic name like x, especially once a program grows beyond a few lines.
With variables covered, the next step is understanding the different kinds of values a variable can hold — explored in depth in our guide to Python data types.
