While conducting a knowledge-sharing session for the New York Python user group, I was asked a question about the difference between variables and identifiers in Python. This is a very basic thing, but as a developer, you should know it. In this tutorial, I will explain the difference between variables and identifiers in Python with examples.
What is an Identifier in Python?
An identifier is a name used to identify a variable, function, class, module, or other object in Python. Essentially, it is a label that you, as a programmer, attach to a particular element in your code to reference it later. Identifiers must follow certain rules:
- They can contain letters (both uppercase and lowercase), digits, and underscores (_).
- They must start with a letter or an underscore.
- They are case-sensitive (e.g.,
myVariableandmyvariableare two different identifiers).
What is a Variable in Python?
A variable in Python is a reserved memory location to store values. In Python, when you create a variable, you are essentially assigning a name (identifier) to that memory location. This allows you to store and manipulate data within your program. Variables can hold different types of data, such as integers, floats, strings, and more.
Key Differences Variable and Identifier in Python
- Purpose:
- Identifier: Used to uniquely name elements in your code.
- Variable: Used to store data that can be manipulated during program execution.
- Role in Memory:
- Identifier: Acts as a reference to the memory location.
- Variable: The actual memory location where data is stored.
- Usage:
- Identifier: Can be used for variables, functions, classes, etc.
- Variable: Specifically used to store data.
Examples
Let’s look at some practical examples to understand these concepts in Python:
Example 1: Variable and Identifier
my_variable = 10
print(my_variable)
In this example:
my_variableis an identifier that names the variable.- The variable
my_variablestores the integer value10.
Here is the output you can see in the screenshot below

Example 2: Identifiers in Functions
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result)
Here:
add_numbersis an identifier for the function.aandbare identifiers for the function’s parameters.resultis an identifier for the variable that stores the function’s return value.
You can see the output in the screenshot below:

Example 3: Case Sensitivity
myVar = 20
myvar = 30
print(myVar) # Output: 20
print(myvar) # Output: 30
In this example:
myVarandmyvarare two different identifiers due to case sensitivity.- They reference two different variables with distinct values.
Check out Difference Between = and == in Python With Examples
Variable Vs. Identifier in Python
Here is a summary of the differences between variables and identifiers in Python.
| Aspect | Identifier | Variable |
|---|---|---|
| Purpose | Used to uniquely name elements in your code | Used to store data that can be manipulated during program execution |
| Role in Memory | Acts as a reference to the memory location | The actual memory location where data is stored |
| Usage | Can be used for variables, functions, classes, etc. | Specifically used to store data |
| Naming Rules | – Can contain letters, digits, and underscores – Must start with a letter or underscore – Case-sensitive | – Follows the identifier rules as it is a type of identifier |
| Examples | my_variable, add_numbers, MyClass | my_variable = 10, result = add_numbers(5, 3) |
This table clearly distinguishes between identifiers and variables in Python, highlighting their differences in purpose, role in memory, usage, and naming rules.
Best Practices for Naming Identifiers
- Be Descriptive: Use meaningful names that describe the purpose of the variable or function.
total_price = 100.50
- Follow Conventions: Stick to naming conventions such as
snake_casefor variables and functions, andCamelCasefor classes.
def calculate_total():
pass
- Avoid Reserved Words: Do not use Python reserved words (keywords) as identifiers.
# Incorrect
def = 5
# Correct
my_def = 5
Conclusion
I hope you now understand the difference between variables and identifiers in Python, which helps you write Python code effectively. Identifiers are the names given to various elements in your program, while variables are the actual data containers. In this tutorial, I explained the difference between variables and identifiers in Python with examples.
You may also like:
- Difference Between Mutable And Immutable In Python With Examples
- Difference Between / and // in Python Division
- Local and Global Variables in Python

I’m Michelle Gallagher, a Senior Python Developer at Lumenalta based in New York, United States. I have over nine years of experience in the field of Python development, machine learning, and artificial intelligence. My expertise lies in Python and its extensive ecosystem of libraries and frameworks. Throughout my career, I’ve had the pleasure of working on a variety of projects that have leveraged my skills in Python and machine learning. Read more…