Variables in Python
What is a Variable?
A variable is a name that refers to a value stored in the computer’s memory.
You can think of a variable as a labeled box. You put a value inside the box and give the box a name so you can use that value later.
name = "Kundan"
age = 25
city = "Hyderabad"
print(name)
print(age)
print(city)
Kundan 25 Hyderabad
In the above example, name, age and city are variables.
Why Do We Need Variables?
Without variables, we would have to write the same value again and again.
Without Variables
print("Student name is Kundan")
print("Kundan is learning Python")
print("Kundan lives in Hyderabad")
With Variables
name = "Kundan"
city = "Hyderabad"
print("Student name is", name)
print(name, "is learning Python")
print(name, "lives in", city)
Student name is Kundan Kundan is learning Python Kundan lives in Hyderabad
If the name changes, you only need to update it in one place.
Creating a Variable
In Python, you create a variable by simply assigning a value to a name using the = operator.
x = 10
message = "Hello Python"
price = 99.50
is_active = True
print(x)
print(message)
print(price)
print(is_active)
10 Hello Python 99.5 True
Syntax: variable_name = value
Rules for Naming Variables
Python has strict rules for variable names:
- A variable name can contain letters (a-z, A-Z), digits (0-9) and underscore (_)
- A variable name cannot start with a digit
- Variable names are case-sensitive (
age,AgeandAGEare different) - You cannot use Python keywords as variable names (
if,for,while,class, etc.) - No spaces are allowed in variable names
Valid Variable Names
name = "Ravi"
student_name = "Priya"
age2 = 22
_total = 500
myVariable = 100
Invalid Variable Names
# 2name = "Ravi" → cannot start with number
# student-name = "Amit" → hyphen not allowed
# my name = "Kiran" → space not allowed
# if = 10 → 'if' is a keyword
Multiple Assignment
Python allows you to assign values to multiple variables in a single line.
Same value to multiple variables
a = b = c = 30
print(a, b, c)
30 30 30
Different values to multiple variables
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
10 20 30
Unpacking a list / tuple
name, age, city = ["Kundan", 25, "Hyderabad"]
print(name)
print(age)
print(city)
Kundan 25 Hyderabad
Changing Variable Values
You can change the value of a variable at any time by assigning a new value to it.
score = 50
print("Initial score:", score)
score = 75
print("Updated score:", score)
score = score + 10
print("Final score:", score)
Initial score: 50 Updated score: 75 Final score: 85
The variable still has the same name, but the value it refers to has changed.
Dynamic Typing in Python
Python is a dynamically typed language. This means you do not need to declare the type of a variable. The type is decided automatically based on the value you assign.
x = 10
print(x, type(x))
x = "Hello"
print(x, type(x))
x = 3.14
print(x, type(x))
x = True
print(x, type(x))
10 <class 'int'> Hello <class 'str'> 3.14 <class 'float'> True <class 'bool'>
The same variable x can hold different types of values at different times.
Deleting a Variable
You can delete a variable using the del keyword. After deletion, the variable no longer exists.
name = "Kundan"
print(name)
del name
# print(name) → This will cause NameError
Kundan
After del name, using name again will raise NameError: name 'name' is not defined.
Good Naming Practices
Writing clear variable names makes your code easier to read and maintain.
Recommended Practices
- Use meaningful names →
student_nameis better thansn - Use lowercase letters with underscores (snake_case) →
total_marks - Avoid single letter names except in loops →
i,j,kare okay in loops - Be consistent with naming style
Good Examples
student_name = "Ravi"
total_marks = 450
is_passed = True
average_score = 89.5
Bad Examples
a = "Ravi" # not clear
tm = 450 # unclear abbreviation
x1 = True # meaningless
AverageScore = 89.5 # better to use snake_case
Common Mistakes Beginners Make
1. Using a variable before creating it
print(score) # NameError
NameError: name 'score' is not defined
2. Case sensitivity mistake
Age = 25
print(age) # NameError
NameError: name 'age' is not defined
3. Using keywords as variable names
# if = 10 → SyntaxError
# for = "loop" → SyntaxError
# class = "A" → SyntaxError
4. Starting variable name with a number
# 1name = "Ravi" → SyntaxError
Summary – Variables in Python
- A variable is a name that refers to a value in memory.
- Create a variable using
name = value. - Variable names are case-sensitive and cannot start with a number.
- Python is dynamically typed – type is decided by the value.
- You can assign multiple variables in one line.
- Use meaningful names (snake_case is preferred).
- Use
delto delete a variable.
Next Topic → Data Types in Python
Variables in Python
Learn how to store and manage data using variables.
Select any topic from the left sidebar to begin.
Start Learning →
