A variable is a named reference that points to a value (object) stored in memory.
Think of a variable as a label attached to a value.
name = "Ironman"name→ Variable (Reference / Identifier)=→ Assignment operator"Ironman"→ Value (String object)
When Python executes
name = "Ironman"it performs these steps:
Python creates the string object.
"Ironman"
Python stores the object in memory.
Memory
┌──────────────────┐
│ "Ironman" │
└──────────────────┘
Python creates the variable.
name
The variable points to the object.
name
│
▼
┌──────────────────┐
│ "Ironman" │
└──────────────────┘
The variable does not store the value directly.
It stores a reference (memory address) to the object.
Variable
name
│
▼
Value
"Ironman"
Or
Variable Value
name ─────────────► "Ironman"
age = 25Python performs
Step 1
25
↓
Step 2
Memory
┌──────┐
│ 25 │
└──────┘
↓
Step 3
age
│
▼
┌──────┐
│ 25 │
└──────┘
name = "Tony"
age = 53
height = 5.9
is_alive = TrueMemory diagram
name
│
▼
"Tony"
age
│
▼
53
height
│
▼
5.9
is_alive
│
▼
True
Or
name ─────► "Tony"
age ─────► 53
height ─────► 5.9
is_alive ─────► True
Each variable points to its own object.
city = "London"Memory
city
│
▼
┌────────────┐
│ "London" │
└────────────┘
city = "London"
city = "Paris"city
│
▼
"London"
city
│
▼
"Paris"
Python changes the reference.
The variable now points to another object.
Before
city
│
▼
"London"
↓
After
city
│
▼
"Paris"
"London"
If no variable points to "London" anymore, Python's Garbage Collector automatically removes it later.
a = 10
b = aPython does not create another 10.
Both variables point to the same object.
┌─────┐
│ 10 │
└─────┘
▲ ▲
│ │
│ │
a b
Or
a ─────┐
│
▼
10
▲
│
b ─────┘
a = 10
b = a
a = 20Initially
a ─────┐
│
▼
10
▲
│
b ─────┘
After
a
│
▼
20
b
│
▼
10
Python creates a new object (20) and makes a point to it.
b still points to 10.
x = 100
y = x
z = y ┌──────┐
│ 100 │
└──────┘
▲ ▲ ▲
│ │ │
│ │ │
x y z
Three variables point to one object.
x = 100
y = x
z = y
y = 500x
│
▼
100
z
│
▼
100
y
│
▼
500
Only y changes.
message = "Hello"message
│
▼
"Hello"
Later
message = "Python"message
│
▼
"Python"
If "Hello" has no references left, Python removes it automatically.
Variable Area
name
age
city
salary
│
│
▼
Memory (Objects)
┌────────────┐
│ "Tony" │
├────────────┤
│ 53 │
├────────────┤
│ "London" │
├────────────┤
│ 50000 │
└────────────┘
Variables point to objects stored in memory.
- A variable is not the value.
- A variable is not the memory location itself.
- A variable is a reference (name) to an object.
- Python automatically manages memory.
- Variables can point to different objects over time.
Imagine labeled boxes.
Label
name
│
▼
"Ironman"
The label (name) tells you where to find the value.
Changing the label to another value
Before
name
│
▼
"Ironman"
After
name
│
▼
"Tony"
The label stays the same.
Only what it points to changes.
Variable
│
▼
Reference
│
▼
Object in Memory
│
▼
Value
Example
name = "Ironman"name
│
▼
┌──────────────────┐
│ "Ironman" │
└──────────────────┘
Python creates the object first, then binds the variable name to that object.
Variable names are identifiers used to refer to objects (values) in Python.
name = "Ironman"name→ Variable (Identifier)=→ Assignment Operator"Ironman"→ Value (Object)
A variable name follows this pattern:
First Character
│
▼
Letter (A-Z, a-z)
or
Underscore (_)
+
Remaining Characters
│
▼
Letters
Numbers
Underscore (_)
Example
student_name1
│
├── student
├── _
├── name
└── 1
Variables may start with or contain English letters.
name
student
price
total
Python
python
Agename = "Tony"
student = "Peter"
price = 500Letters are valid identifier characters.
Numbers are allowed after the first character.
age1
version2
python3
student123
price2025
x1age1 = 20
version2 = "2.0"Python cannot parse identifiers beginning with digits.
1name
2age
100marks
123abc
9priceExample
1name = "Tony"Output
SyntaxError: invalid decimal literal
name1 = "Tony"
marks100 = 95The underscore is treated as a normal identifier character.
student_name
first_name
last_name
_marks
_private
employee_salary
phone_numberExample
first_name = "Tony"Python treats spaces as separators between tokens.
student name
first name
user agestudent name = "Tony"Output
SyntaxError
student_name = "Tony"
user_age = 20The hyphen is the subtraction operator.
student-name
total-pricePython reads
student - nameinstead of one variable.
student_name
total_priceThese characters cannot appear in identifiers.
student@name
student#name
student$name
student%name
student^name
student&name
student*name
student!name
student?name
student.name
student,name
student:name
student;name
student/name
student\name
student+name
student=name
student(name)
student[name]
student{name}
student|name
student~name
student`nameThese characters have special meanings in Python.
Use _ instead.
student_namePython treats uppercase and lowercase letters differently.
name = "Tony"
Name = "Steve"
NAME = "Bruce"These are three different variables.
name
Name
NAME
Memory
name ─────► "Tony"
Name ─────► "Steve"
NAME ─────► "Bruce"
Keywords already have predefined meanings.
if = 10
for = 20
while = 30
class = "A"
return = 5
try = 1Output
SyntaxError
False
None
True
and
as
assert
async
await
break
case
class
continue
def
del
elif
else
except
finally
for
from
global
if
import
in
is
lambda
match
nonlocal
not
or
pass
raise
return
try
while
with
yield
Python allows this.
list = [1, 2, 3]But now
list("abc")fails because list no longer refers to the built-in function.
Avoid names like
list
dict
set
tuple
str
int
float
bool
sum
max
min
len
type
print
inputPython supports Unicode identifiers.
नाम = "Ironman"
年龄 = 20
π = 3.14159Although valid, English names are recommended for readability.
name
student
student_name
student_name1
student1
_name
_marks
price
PRICE
Price
user_age
employee_salary
phone_number
email_address
address
salary
height
weight
version2
python3
data
count
counter
total_marks
max_value
min_value
first_name
last_name
middle_name
employee_id
customer_name
is_active
is_logged_in
has_permission1name
2age
100marks
student-name
student name
student@name
student#name
student$name
student%name
student^name
student&name
student*name
student!name
student?name
student.name
student,name
student:name
student;name
student/name
student\name
student+name
student=name
student(name)
student[name]
student{name}
student|name
student~name
student`name
if
for
while
class
return
try
except
True
False
Nonea = 25
x = 50000
n = "Tony"
d = "2026-01-01"
p = 99.5Hard to understand.
age = 25
salary = 50000
name = "Tony"
joining_date = "2026-01-01"
percentage = 99.5The purpose is immediately clear.
Python recommends snake_case.
first_name
last_name
student_age
employee_salary
phone_number
email_address
product_price
maximum_speed
total_marksFirstName
FIRSTNAME
firstName
First_Namename = "Tony"
age = 53
salary = 90000name
│
▼
"Tony"
age
│
▼
53
salary
│
▼
90000
first_name = "Tony"
last_name = "Stark"
age = 53
salary = 1000000
is_hero = TrueMemory
first_name ─────► "Tony"
last_name ──────► "Stark"
age ────────────► 53
salary ─────────► 1000000
is_hero ────────► True
| Rule | Allowed |
|---|---|
| Letters | ✅ |
| Digits after first character | ✅ |
| Start with digit | ❌ |
Underscore _ |
✅ |
| Space | ❌ |
Hyphen - |
❌ |
| Special characters | ❌ |
| Keywords | ❌ |
| Case-sensitive | ✅ |
| Unicode | ✅ (not recommended for most projects) |
snake_case |
✅ Recommended |
Variable Name Rules
✅ Letters
✅ Digits (not first)
✅ Underscore (_)
❌ Start with digit
❌ Spaces
❌ Special characters
❌ Python keywords
Recommended:
snake_case
Meaningful names
Avoid built-in names
| Variable Name | Valid | Reason |
|---|---|---|
name |
✅ | Starts with a letter |
student |
✅ | Only letters |
student_name |
✅ | Uses underscore |
student1 |
✅ | Number at the end |
student_name1 |
✅ | Letters, underscore, number |
_name |
✅ | Starts with underscore |
_marks |
✅ | Underscore is allowed |
a |
✅ | Single letter |
x |
✅ | Single letter |
age1 |
✅ | Number after letter |
python3 |
✅ | Number at the end |
version2 |
✅ | Valid identifier |
price2025 |
✅ | Numbers allowed after first character |
first_name |
✅ | snake_case |
last_name |
✅ | snake_case |
employee_salary |
✅ | snake_case |
phone_number |
✅ | snake_case |
email_address |
✅ | snake_case |
is_active |
✅ | Boolean naming convention |
has_permission |
✅ | Descriptive name |
MAX_SIZE |
✅ | Constant naming style |
userAge |
✅ | Valid (camelCase, but not PEP 8 recommended) |
UserName |
✅ | Valid (PascalCase, but not recommended for variables) |
π |
✅ | Unicode identifier |
नाम |
✅ | Unicode identifier |
| Variable Name | Invalid | Reason |
|---|---|---|
1name |
❌ | Starts with a number |
2age |
❌ | Starts with a number |
123abc |
❌ | Starts with a number |
student name |
❌ | Contains a space |
student-name |
❌ | Hyphen (-) is subtraction operator |
student@name |
❌ | @ not allowed |
student#name |
❌ | # not allowed |
student$name |
❌ | $ not allowed |
student%name |
❌ | % not allowed |
student^name |
❌ | ^ not allowed |
student&name |
❌ | & not allowed |
student*name |
❌ | * not allowed |
student!name |
❌ | ! not allowed |
student?name |
❌ | ? not allowed |
student.name |
❌ | . not allowed |
student,name |
❌ | , not allowed |
student:name |
❌ | : not allowed |
student;name |
❌ | ; not allowed |
student/name |
❌ | / not allowed |
student\name |
❌ | \ not allowed |
student+name |
❌ | + not allowed |
student=name |
❌ | = is assignment operator |
student(name) |
❌ | Parentheses not allowed |
student[name] |
❌ | Square brackets not allowed |
student{name} |
❌ | Curly braces not allowed |
| `student | name` | ❌ |
student~name |
❌ | ~ not allowed |
student`name |
❌ | Backtick not allowed |
if |
❌ | Python keyword |
for |
❌ | Python keyword |
while |
❌ | Python keyword |
class |
❌ | Python keyword |
return |
❌ | Python keyword |
import |
❌ | Python keyword |
try |
❌ | Python keyword |
except |
❌ | Python keyword |
True |
❌ | Reserved keyword |
False |
❌ | Reserved keyword |
None |
❌ | Reserved keyword |
| Variable Name | Valid | Reason |
|---|---|---|
l |
✅ | Looks like 1 |
O |
✅ | Looks like 0 |
I |
✅ | Looks like 1 or l |
x |
✅ | Too generic |
temp |
✅ | Not descriptive |
data |
✅ | Too generic |
value |
✅ | Too generic |
list |
✅ | Shadows built-in list() |
dict |
✅ | Shadows built-in dict() |
str |
✅ | Shadows built-in str() |
int |
✅ | Shadows built-in int() |
float |
✅ | Shadows built-in float() |
print |
✅ | Shadows built-in print() |
sum |
✅ | Shadows built-in sum() |
max |
✅ | Shadows built-in max() |
min |
✅ | Shadows built-in min() |
type |
✅ | Shadows built-in type() |
| Rule | Example |
|---|---|
| Starts with letter | ✅ name |
| Starts with underscore | ✅ _name |
| Starts with number | ❌ 1name |
| Contains spaces | ❌ student name |
| Contains special characters | ❌ student@name |
| Uses underscore | ✅ student_name |
| Python keyword | ❌ if |
| Case-sensitive | ✅ name, Name, NAME are different |
| Unicode allowed | ✅ π, नाम |
| Recommended style | ✅ snake_case |