What is a variable in Python, and how is it used to store data?
What is a variable in Python?
In Python, a variable is a name that refers to a value stored in memory.
It lets you store, reuse, and change data while your program runs.
Simple example
age = 25
name = "Mohi"
price = 99.50
Here:
agestores an integer (25)namestores a string ("Mohi")pricestores a float (99.50)
Key points about Python variables
- No type declaration needed
x = 10 # integer x = "ten" # now a stringPython decides the type automatically.
- Variables are case-sensitive
Age = 20 age = 25Ageandageare different variables. - Must start with a letter or underscore
✔ Valid:total = 100 _count = 5✘ Invalid:
1total = 100 - Cannot use Python keywords
# invalid class = 10
Why variables are important
- Store user input
- Perform calculations
- Make programs readable and flexible
Example:
length = 10
width = 5
area = length * width
print(area)
How is it used to store data?
A Python variable stores data by creating a name that points to a value in the computer’s memory.
Think of it as labeling a box so you can find and use what’s inside later.
Step-by-step: how data is stored
1️⃣ Assignment (=)
x = 10
What happens internally:
- Python creates the value
10in memory - The name
xis linked (points) to that value
x ───▶ 10
2️⃣ Using the stored data
print(x)
Python:
- Looks up what
xpoints to - Retrieves
10 - Prints it
3️⃣ Storing different data types
name = "Rice"
price = 45.75
available = True
name ───▶ "Rice"
price ──▶ 45.75
available ─▶ True
Each variable points to a different type of data.
4️⃣ Changing stored data
x = 10
x = 20
Now:
x ───▶ 20
The variable name stays the same, but it points to a new value.
5️⃣ Variables can store results of calculations
length = 8
width = 5
area = length * width
area ───▶ 40
6️⃣ Variables can store user input
crop = input("Enter crop name: ")
Whatever the user types is stored in crop.
Important concept (simple version)
- Variables don’t “contain” data
- They reference (point to) data in memory
This is why you can do:
a = 10
b = a
Both a and b point to the same value:
a ─┐
└──▶ 10
b ─┘
Real-life example (business/agriculture)
crop_name = "Wheat"
yield_kg = 2500
price_per_kg = 35
total_value = yield_kg * price_per_kg
Here, variables store farm data so Python can calculate, analyze, and report results.