Python Variables
Table of Contents + −
In the last lesson you learned about Python Keywords. Those are the special words Python keeps for itself. Now we hit a real wall. The moment your program has more than one line, you need to remember a value. A name. A score. A price. Where do you keep it so you can use it again later? That is exactly what a variable is for.
🤔 Why Do We Need Variables?
Say you are building a small game. The player scores some points. A few lines later you want to add more points. A few lines after that you want to print the total.
Without a place to keep that number, you are stuck. You would have to type the same value again and again. And if it changes, you would have to find every copy and fix it by hand. That is painful and easy to get wrong.
A variable solves this in one line. You give the value a name once. Then you use the name everywhere else. Change it in one place and the new value flows through your whole program.
📦 What Is a Variable?
Think of a variable as a labelled box. You put something inside the box. You write a name on the front. Later, when you want what is inside, you just call out the name.
In Python, a variable is a name that points to a value. The value can be a number, some text, a list, anything. The name is how you reach it again.
You create one with the = sign. This is the part that trips people up, so read it slowly. In Python, = does not mean “is equal to” like in math. It means “put this value into this name”. We call it assignment.
Here is the shape of it. The name goes on the left. The value goes on the right.
name = valueLet’s make a real one. This stores the number 10 under the name score.
score = 10print(score)We make a box called score, put 10 in it, then ask Python to print what score holds.
Output
10Notice we never told Python “this is a number”. We just gave it 10 and Python worked the rest out. More on that in a moment.
👀 Reading a Variable
Once a variable exists, you read it by writing its name. That is it. Anywhere you would write the value, you can write the name instead. Python swaps in whatever is inside.
This example saves a player name, then uses it in a message.
player = "Alex"print("Welcome,", player)print("Good luck,", player)We store the text "Alex" in player. Each time we write player after that, Python reads the box and gives back "Alex".
Output
Welcome, AlexGood luck, AlexHere is the benefit. The name lives in one place. If Riya logs in instead of Alex, you change that one line. Both messages update on their own.
🔄 Changing a Variable
Here is where variables earn their keep. The value in a box is not stuck there forever. You can put something new in whenever you like. This is called reassignment.
You reassign the same way you assigned. You use =. The old value is dropped and the new one takes its place.
Back to our game. The score starts at 0. The player collects a coin, then collects another.
score = 0print(score)
score = score + 10print(score)
score = score + 10print(score)Look at the line score = score + 10. It reads strange at first. But it is simple once you split it. Python first works out the right side. It takes the current score and adds 10. Then it puts that result back into score. So the box updates to its new total.
Output
01020The same name, three different values over time. That is the whole point of a variable. It can hold a value that moves.
Tip
score = score + 10 is so common that Python gives you a short form: score += 10. It does the exact same thing. You will see += everywhere, so it is good to recognise it early.
🧠 Python Picks the Type for You
In many languages you have to announce what kind of value a variable will hold before you use it. You write something like “this is a whole number” or “this is text”. That is called declaring a type.
Python skips all of that. You just assign a value. Python looks at the value to decide its type on its own. This is called dynamic typing. The type is figured out while the program runs, from the value you gave.
So all of these just work, no type announcement needed:
score = 100 # a whole number (int)price = 9.99 # a number with a decimal (float)player = "Alex" # some text (str)game_over = False # True or False (bool)You can even check what type a value is using the built-in type() function. You pass it a variable and it tells you what is inside.
score = 100player = "Alex"price = 9.99
print(type(score))print(type(player))print(type(price))Output
<class 'int'><class 'str'><class 'float'>int means integer, a whole number. str means string, which is text. float is a number with a decimal point. You did not set any of these. Python read the value and labelled it for you.
Caution
Because Python decides the type from the value, the type can change when you reassign. If you write score = 100 and later score = "done", the same name now holds text. Python allows it. But a variable that flips between a number and text is usually a sign of a bug. Keep each variable holding one kind of thing.
🎯 Assigning Many Variables at Once
Sometimes you need to set up a few variables together. Python has two neat shortcuts for this.
The first lets you give several names their own values on one line. The names go on the left. The values go on the right, matched up in order.
x, y, z = 1, 2, 3print(x)print(y)print(z)Python pairs them up left to right. x gets 1, y gets 2, z gets 3.
Output
123The second shortcut gives the same value to several names at once. Chain them with =.
lives = health = shield = 100print(lives)print(health)print(shield)All three start at 100 from one line.
Output
100100100Note
With a, b = 1, 2 the count must match. Three names need three values. If they do not line up, Python stops with an error. So keep both sides the same length.
⚠️ Common Mistakes
A few things catch people out early. Watch for these.
- Using a variable before you make it. You have to assign a name before you read it. If you print
scoreand never set it, Python says it does not know that name.
# ❌ Avoid: reading a name that was never assignedprint(score) # NameError: name 'score' is not defined
# ✅ Good: assign first, then readscore = 0print(score)- Getting the sides of
=backwards. The name goes on the left and the value on the right, always.10 = scoreis not valid.
# ❌ Avoid: value on the left10 = score
# ✅ Good: name on the left, value on the rightscore = 10-
Thinking
=checks equality. A single=stores a value. Checking if two things are equal is a different operator,==, which you will meet later. Do not mix them up. -
Mismatched multiple assignment. With
a, b = 1, 2, 3the two sides do not match, so Python errors. Use the same number of names and values.
✅ Best Practices
Small habits that keep your variables easy to read.
- Name the box for what it holds.
scoreandplayer_nametell the reader everything.sandxtell them nothing. - Use snake_case. Lowercase words joined with underscores, like
high_scoreorplayer_name. This is the standard Python style. - One job per variable. Let each variable hold one kind of thing the whole way through. Do not start it as a number and later put text into it.
- Set a starting value. Give a variable a sensible start, like
score = 0, before you begin changing it.
🧩 What You’ve Learned
✅ A variable is a name that points to a value, like a labelled box.
✅ You create one with =, reading it as “put this value into this name”, not “is equal to”.
✅ You read a variable by writing its name, and you change it by assigning again (reassignment).
✅ Python uses dynamic typing. It picks the type from the value, so you never declare one yourself.
✅ type() shows you what a variable holds, like int, str, or float.
✅ You can assign many at once with a, b = 1, 2 or give the same value to several with x = y = 0.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
In Python, what does the = sign do in score = 10?
Why: A single = is assignment: it stores the value on the right into the name on the left.
- 2
What does dynamic typing mean in Python?
Why: With dynamic typing, Python reads the value you assign and decides the type for you.
- 3
After score = 5 then score = score + 10, what does print(score) show?
Why: Python works out the right side first (5 + 10 = 15), then stores 15 back into score.
- 4
What does a, b = 1, 2 do?
Why: Multiple assignment pairs names with values left to right, so a gets 1 and b gets 2.
🚀 What’s Next?
You can make a variable now. But Python is fussy about what you are allowed to call one. Some names work, some cause errors, and a few are best avoided even when they are legal. Let’s sort out the rules next.