Python Variables
Table of Contents
In the last lesson, we learned Python keywords. Now let’s learn how variables help us store and reuse values.
Where do we keep our stuff?
Our clothes go in a cupboard. Our books go on a shelf. Our money goes in a wallet or a bank.
We give each thing a place. And we give that place a name. Like “my cupboard” or “my wallet.”
Python does the same thing with data.
It needs a place to keep numbers, text, and other things. And it needs names for those places.
That is what a variable is.
🤔 What Is a Variable in Python?
A variable is just a name for a place where we store something.
That is it. That is the whole idea.
Let me show us.
name = "Alex"age = 25Here, name is a variable. It holds the text “Alex.”
And age is a variable. It holds the number 25.
Think of it like a box. We put something in the box. We write a label on the box. Later, we can look at the label and find what is inside.
- The box is the variable.
- The label is the variable name.
- The thing inside is the value.
✍️ How to Create a Variable in Python
Creating a variable in Python is very easy.
We just write a name. Then an equals sign. Then the value.
name = "Alex"That’s it. We just created a variable called name.
We did not have to say “create a variable.” We did not have to say “this is a string.” We just wrote it. And Python understood.
This is one reason Python is so easy. It does not make us do extra work.
🔧 How to Use a Variable in Python
Once we create a variable, we can use it anywhere.
name = "Alex"print(name)Output
AlexSee what happened? We put “Alex” inside name. Then we asked Python to print name. And Python looked inside the box, found “Alex,” and printed it.
Let me show us another example.
age = 25print("My age is", age)Output
My age is 25We did not have to write “My age is 25” directly. We wrote age. And Python replaced it with the actual value.
That is the power of variables. We write the name once. We use it many times.
💡 Why Do We Need Variables in Python?
Good question. Why not just write the value directly?
Well, think about this.
print("My age is 25")print("Next year I will be 26")print("In two years I will be 27")Now imagine we want to change our age. We have to change it in three places. And if we forget one, the output will be wrong.
Now look at this:
age = 25print("My age is", age)print("Next year I will be", age + 1)print("In two years I will be", age + 2)Now if we want to change our age, we only change one line. The first line. Everything else updates automatically.
That is why we use variables. They make our code easy to change. Easy to fix. Easy to read.
🏠 Python Variables: A Real-Life Example
Think of a variable like a labeled jar in our kitchen.
We have a jar labeled “sugar.” We put sugar in it. Later, when we need sugar, we look for the jar labeled “sugar.”
We do not need to remember where we kept the sugar. We just remember the name.
Variables are the same. We do not need to remember the value. We just remember the name. Python finds the value for us.
🔄 Variables Can Change in Python
Here is something important. The value inside a variable can change.
That is why it is called a “variable.” Because it can vary. It can change.
score = 10print(score)
score = 20print(score)Output
1020See? First score was 10. Then we changed it to 20. Same variable. New value.
The old value is gone. Replaced by the new one.
Think of it like a whiteboard. We write “10” on it. Then we erase it and write “20.” The whiteboard is the same. The writing changed.
📦 Variables Can Hold Different Types of Things
In Python, a variable can hold any type of value.
name = "Alex" # textage = 25 # whole numberheight = 5.9 # decimal numberis_student = True # true or falsePython does not care. It will figure out the type automatically. We do not have to tell it.
This is different from some other languages. In Java, we have to say what type of value a variable will hold. In Python, we just assign and go.
📝 Multiple Variables at Once in Python
We can create multiple variables in one line.
x, y, z = 10, 20, 30Now x is 10, y is 20, and z is 30.
We can also give the same value to multiple variables:
a = b = c = 100Now a, b, and c are all 100.
These are shortcuts. We use them when they make sense. But let’s not overuse them. Sometimes it is clearer to write one per line.
⚠️ Common Mistakes with Python Variables
🧭 Mistake 1: Putting Variable Names Inside Quotes
Let me save us from a mistake that every beginner makes.
Look at this:
name = "Alex"print("Hello, name")What will this print?
If we said “Hello, Alex,” we are wrong.
It will print:
Hello, nameWhy? Because name is inside quotes. And anything inside quotes is just text. Python does not look inside variables when the text is in quotes.
So what do we do? We have two options.
Option 1: Use a comma.
name = "Alex"print("Hello,", name)Output
Hello, AlexOption 2: Use an f-string. We will learn about that later. But here is a sneak peek.
name = "Alex"print(f"Hello, {name}")Output
Hello, AlexFor now, let’s just remember — if we want the value of a variable, we should not put the variable name inside quotes.
💬 Mistake 2: Using a Variable Before Creating It
Look at this:
print(age)If we run this without creating age first, Python will give an error:
NameError: name 'age' is not definedThis means — we used a variable that does not exist.
So what do we do? We create the variable before using it.
age = 25print(age)Simple rule: create first, use later.
🧪 Let’s Build Something Small with Python Variables
Here is a small program that uses variables.
# My informationname = "Alex"age = 25city = "London"
# Print it outprint("My name is", name)print("I am", age, "years old")print("I live in", city)Output
My name is AlexI am 25 years oldI live in LondonSee how clean that is? All the information is at the top. The printing is at the bottom. If we want to change the name, we just change one line.
That is good code. That is how we should write.
🔢 Variables Can Be Used in Math
Variables can hold numbers. And we can do math with them.
price = 100quantity = 3total = price * quantityprint(total)Output
300We can also update a variable using its own value.
score = 10score = score + 5print(score)Output
15Wait, what happened here?
score = score + 5 means: take the current value of score (which is 10), add 5 to it (which gives 15), and store it back in score.
So score becomes 15.
This is very common in programming. We will see it a lot.
There is even a shortcut for it:
score = 10score += 5print(score)Output
15score += 5 is the same as score = score + 5. Just shorter.
We can do this with other operations too:
x = 10x -= 3 # x = x - 3, now x is 7x *= 2 # x = x * 2, now x is 14x /= 7 # x = x / 7, now x is 2.0These are called augmented assignments. Fancy name. Simple idea.
🖥 A Quick Tip About Variable Names
Variable names should be meaningful.
Bad:
x = "Alex"y = 25z = "London"Good:
name = "Alex"age = 25city = "London"Why? Because when we come back to this code next month, we will understand the good version. The bad version will confuse us.
It takes two extra seconds to type a good name. Let’s do it. Our future self will thank us.
Next, we will learn about Variable Naming Rules so we can continue step by step.