What Is a Variable in Python?
A variable in Python is a name that refers to a value.
You use variables to store data and use it later in your code. For example, you can store a person's name, an age, a price, or a True/False value in a variable.
In Python, you create a variable by assigning a value with =.
name = "Sam"
age = 12
print(name)
print(age)
Output:
Sam
12
In this example:
nameis a variable that refers to the text"Sam"ageis a variable that refers to the number12
This is the basic idea of a variable: a named place for storing data.
What a variable means
A variable is a name that refers to a value.
That means:
- It lets you store data and use it later
- The value can be text, a number,
True/False, or other kinds of data - In Python, you create a variable by assigning a value with
=
Example:
message = "Hello"
print(message)
Output:
Hello
Here, message is the variable name, and "Hello" is the value.
If you are new to Python, you may also want to read Python variables explained for beginners.
Why variables are useful
Variables are used in almost every Python program because they make code easier to work with.
They help because:
- They make code easier to read
- They let you reuse values without typing them again
- They make it easier to update data in one place
Example without a variable:
print("Sam")
print("Sam")
Example with a variable:
name = "Sam"
print(name)
print(name)
The second version is easier to update. If the name changes, you only need to change it once.
How to create a variable
To create a variable in Python:
- Write the variable name on the left
- Write
=in the middle - Write the value on the right
Example:
score = 100
print(score)
Output:
100
It helps to remember that = here means assignment, not mathematical equality. It means “put this value into this variable name.”
Simple variable examples
Variables can store different kinds of values.
Store text
city = "London"
print(city)
Store a whole number
count = 5
print(count)
Store a decimal number
price = 9.99
print(price)
Store a boolean
is_ready = True
print(is_ready)
These are different data types in Python. If you want a broader view, see Python data types overview.
Changing a variable value
A variable can point to a new value later.
This is called reassignment.
count = 5
print(count)
count = 6
print(count)
Output:
5
6
The newest assigned value is the current one.
This is another reason variables are useful. You can update information as your program runs.
Basic naming rules
Python variable names must follow some simple rules:
- Names can contain letters, numbers, and underscores
- Names cannot start with a number
- Names cannot contain spaces
- You cannot use Python keywords like
if,for, orclass
Valid names:
user_name = "Ana"
score1 = 10
total_price = 19.99
Invalid names:
# 2name = "Ana"
# user name = "Ana"
# class = "Math"
Those examples are invalid because:
2namestarts with a numberuser namecontains a spaceclassis a Python keyword
Good variable names
Choose names that describe the value clearly.
Good variable names make code easier to read and debug.
Prefer names like:
total_priceuser_nameis_logged_in
Instead of unclear names like:
tpxa1
A common Python style is to use lowercase_with_underscores for variable names.
Clear names are especially helpful for beginners.
Variables are not the same as values
A variable name and a value are not the same thing.
- The variable name is the label
- The value is the actual data
Example:
name = "Ana"
print(name)
print("name")
Output:
Ana
name
Why is the output different?
print(name)uses the variable callednameprint("name")prints the text"name"exactly as written
So in:
name = "Ana"
nameis the variable"Ana"is the string value
This difference matters when printing, comparing, and updating data.
Common mistakes
Beginners often run into the same problems when learning variables.
Thinking = means “equals” in the math sense
In Python, = usually means assignment.
x = 5
This means “assign the value 5 to x.”
It does not mean “x equals 5” in the same way as a math equation.
Using quotes around a variable name by mistake
name = "Sam"
print(name)
print("name")
Output:
Sam
name
With quotes, Python treats it as text, not as a variable.
Using an invalid variable name
These names are not valid:
2nameuser name
Use names like:
name2user_name
Trying to use a variable before assigning it
print(age)
age = 12
This causes an error because age does not have a value yet.
Python often shows this as a NameError: name is not defined.
Confusing variables with data types
A variable is the name you use.
A data type is the kind of value stored there, such as a string, integer, or boolean.
You can inspect a value's type with the type() function.
Useful debugging checks:
name = "Sam"
age = 12
print(name)
print(type(name))
print(age)
print(type(age))
Possible output:
Sam
<class 'str'>
12
<class 'int'>
FAQ
Do I need to declare a variable type in Python first?
No. Python creates the variable when you assign a value to it.
Can a variable change from one type to another?
Yes. A variable can refer to a string first and a number later, though clear code usually avoids unnecessary changes.
Is a variable the same as a value?
No. The variable is the name. The value is the data stored under that name.
Why does Python say a name is not defined?
Usually because the variable was used before it was assigned, or the name was misspelled.