Last modified: May 25, 2026
Python Variables Explained Simply
Python variables are names that store data. They are like labels for values in memory. When you create a variable, Python reserves space for your data. This guide explains everything you need to know.
What Is a Python Variable?
A variable is a container for storing data values. Unlike other languages, Python has no command for declaring a variable. The variable is created the moment you assign a value to it. This makes Python very beginner-friendly.
Variables in Python are dynamic. You can change their type anytime. For example, a variable can hold a number first and a string later. Python handles this automatically. To understand the core concept better, read our Python Variable Meaning Explained guide.
# Creating a variable
name = "Alice" # String variable
age = 25 # Integer variable
height = 5.6 # Float variable
print(name)
print(age)
print(height)
Alice
25
5.6
Variable Naming Rules
Python has strict rules for variable names. You must follow them to avoid errors. A variable name can only contain letters, numbers, and underscores. It cannot start with a number. Names are case-sensitive. So myVar and myvar are different.
Python keywords like if, for, and while cannot be used as variable names. Use descriptive names for better code readability. For example, use student_name instead of sn.
# Valid variable names
first_name = "John"
lastName = "Doe"
_private = "secret"
var123 = 456
# Invalid variable names (will cause error)
# 123var = "error" # Cannot start with number
# my-var = "error" # Hyphen not allowed
# for = "error" # 'for' is a keyword
Variable Assignment
Assignment uses the equals sign =. The right side is evaluated first. Then the result is stored in the left-side variable. You can assign multiple variables in one line. This is called unpacking or multiple assignment.
Python also allows chained assignment. This means you can assign the same value to multiple variables at once. For more examples, check our Python Variables Examples Guide.
# Multiple assignment
x, y, z = 10, 20, 30
print(x, y, z)
# Chained assignment
a = b = c = 100
print(a, b, c)
10 20 30
100 100 100
Variable Types in Python
Python supports many data types. The most common ones are integers, floats, strings, and booleans. You do not need to declare the type. Python infers it from the assigned value.
Use the type() function to check a variable's type. This is helpful for debugging. Python is dynamically typed. This means the type can change during program execution. Learn more about types in our Understanding Python Variable Types article.
# Different variable types
num = 42 # Integer
pi = 3.14159 # Float
text = "Hello" # String
is_active = True # Boolean
print(type(num))
print(type(pi))
print(type(text))
print(type(is_active))
Variable Scope
Scope determines where a variable is accessible. In Python, there are four types of scope: local, enclosing, global, and built-in. Variables defined inside a function are local. They cannot be accessed outside.
Global variables are defined outside functions. They can be accessed anywhere. However, modifying a global variable inside a function requires the global keyword. For a deeper dive, read Mastering Python Variable Scoping.
# Global variable
global_var = "I am global"
def my_function():
# Local variable
local_var = "I am local"
print(global_var) # Accessible
print(local_var) # Accessible
my_function()
# print(local_var) # This would cause an error
I am global
I am local
Variable References and Memory
In Python, variables are references to objects in memory. When you assign a variable, Python creates an object and points the variable to it. Multiple variables can reference the same object. This is important for mutable objects like lists.
If you modify a mutable object through one variable, all references see the change. Immutable objects like integers and strings behave differently. Changing them creates a new object. Understand this better with our Understanding Python Variable References and Memory Management guide.
# Variable references
list1 = [1, 2, 3]
list2 = list1 # Both reference same list
list2.append(4)
print(list1) # list1 also changed
# Immutable example
a = 10
b = a
b = 20
print(a) # a remains 10
[1, 2, 3, 4]
10
Best Practices for Variables
Use descriptive names. This makes your code self-documenting. Avoid single-letter names except in loops. Use snake_case for variable names. This is the Python convention.
Constants are written in UPPER_CASE. This is a convention, not a rule. Python does not enforce constants. Always initialize variables before using them. Uninitialized variables cause errors.
# Good variable names
user_age = 30
total_price = 99.99
is_verified = False
# Constant convention
MAX_LIMIT = 1000
PI = 3.14159
# Always initialize
count = 0 # Good
# print(unknown_var) # Error: not defined
Common Mistakes with Variables
Beginners often make mistakes with variable names. Using reserved keywords is a common error. Another mistake is forgetting that Python is case-sensitive. Also, remember that variables must be defined before use.
Do not use type as a variable name. It shadows the built-in type() function. Similarly, avoid list, str, and dict. These are built-in names. Overwriting them can cause confusing bugs.
# Avoid these mistakes
# type = "text" # Bad: shadows built-in function
# list = [1, 2] # Bad: shadows built-in list
# Correct way
data_type = "text"
my_list = [1, 2]
Conclusion
Python variables are simple yet powerful. They store data and make your code dynamic. Remember the naming rules and scope. Use descriptive names for readability. Practice with examples to master variables.
Variables are the foundation of Python programming. Understanding them well makes learning other concepts easier. Start with simple assignments and gradually explore advanced topics like unpacking and references. Happy coding!