Variables in Python
A variable is a name used to store a value in a program. It acts like a container that holds information such as numbers, text, or other data.
It helps programmers save information and reuse it whenever needed in a program.
Variables make coding easier, organized, and flexible by allowing data to be stored and updated.
For example:
x = 10
Here:
- x is the variable name
- 10 is the value stored in it
Real-Life Analogy
A variable is like a labeled water bottle.
You can fill it with water, juice, or milk, and the label tells what is inside.
In the same way, a Python variable stores data with a name.
What is a Variable?
A variable is like a container used to store data in computer memory.
Instead of writing the same value many times, we can store it once and reuse it.
This makes coding:
●Easier
●Faster
●More organized
How Python Handles Variables
One special thing about Python is:
You do not need to declare the data type manually.
Python automatically understands it.
Examples:
- If we assign a number → Python treats it as a number
- If we assign text → Python treats it as a string
This makes Python beginner-friendly.
Syntax
variable_name = value
Example:
age = 12
name = "VMS"
Practical Example
Think of a variable like a box with a label.
You put something inside the box and use it whenever needed.
Example-1
x = 10
name = "VMS"
print(x)
print(name)
Output
10
VMS
Example-2
a = 10
b = 20
total = a + b
print("Total =", total)
Output
Total = 30
Changing Variable Values
Variables can be updated.
x = 10
x = 20
print(x)
Output
20
● The old value is replaced with the new value.
Rules for Naming Variables
Valid Rules
● Must start with a letter or underscore (_)
Examples:
name = "Ravi"
_value = 20
● Can contain letters, numbers, and underscores
Examples:
student1 = "Ram"
total_marks = 90
Invalid Rules
Cannot start with a number
1name (Wrong)
name1(Correct)
No spaces allowed
my name (Wrong)
my_name(Correct)
Important Notes
● Variable names are case-sensitive
name = "Ram"
Name = "Ravi"
These are different variables.
● Avoid using Python keywords as variable names
Do not use:
●print
●if
●for
➤ Key Points
● Variables are used to store data
● No need to declare data type in Python
● Can store numbers, text, and other values
● Makes code reusable and flexible
● Easy to update values in one place
➤ Tips for Students
● Use meaningful names like marks, age, total
● Keep names simple and clear
● Do not use very long or confusing names
● Practice using variables in small programs