AiTechWorlds
AiTechWorlds
Open your phone's contacts app. Somewhere in there is your mum's number — something like 07700 900123. But you don't think of it that way. You think "Mum." You tap "Mum." Her number comes up.
You've been using variables your whole life. A contact name is a label that points to a phone number. "Mum" is the name; the actual digits are the value stored underneath. The name is easy to remember, the raw data doesn't need to be.
That's all a variable is: a name that points to a piece of stored data. And they're the single most fundamental concept in all of programming.
Imagine a row of small boxes in a storage room. Each box can hold exactly one thing. You can put something in a box, look inside to see what's there, or swap the contents for something else. Variables are those boxes, and computer memory is the storage room.
Every variable has:
In Python, you create a variable and give it a value in one simple line:
name = "Sarah"
age = 28
height = 1.65
name is a variable. Its current value is the text "Sarah".age is a variable. Its current value is the number 28.height is a variable. Its current value is the decimal 1.65.= Sign: Not What You ThinkIn school, = means "these two things are equal." In programming, it means something different — it means "take the value on the right and store it in the name on the left."
score = 100
This does NOT say "score equals 100" like a math equation. It says "create a box called score and put 100 inside it." The right side is calculated first, then the result is stored in the left side.
This distinction matters. Later you'll see things like:
score = score + 10
In math, this is nonsense (nothing equals itself plus ten). In programming, it means: "take whatever's in score right now, add 10, and put that new value back into score." So if score was 100, it becomes 110.
Python is flexible about names, but not completely — there are rules. Break them and you'll get an error.
| Rule | Example of VALID name | Example of INVALID name | Why it fails |
|---|---|---|---|
Must start with a letter or _ | age, _total | 1score, 2things | Starts with a number |
| No spaces allowed | first_name, lastName | first name | Space breaks the name |
| Only letters, numbers, underscores | user_id, item2 | user-id, my@var | Special characters not allowed |
| Case-sensitive | score and Score are DIFFERENT | — | score ≠ Score ≠ SCORE |
| Can't use reserved words | my_list, total | list, for, if | These words mean something in Python |
Reserved words (also called keywords) are words Python already uses for its own purposes. You can't use them as variable names:
False None True and as assert async await
break class continue def del elif else except
finally for from global if import in is
lambda nonlocal not or pass raise return try
while with yield
Don't try to memorize these. If you accidentally use one, Python will tell you with a SyntaxError.
Python lets you do several things at once:
# Assign the same value to multiple variables
x = y = z = 0
# Assign different values in one line (unpacking)
name, age, city = "Marcus", 31, "Lagos"
print(name) # Marcus
print(age) # 31
print(city) # Lagos
The second style — name, age, city = "Marcus", 31, "Lagos" — is called unpacking. Python matches each name to each value left to right. Clean and concise.
Variables aren't permanent. You can change what's inside any time:
mood = "tired"
print(mood) # tired
mood = "coffee-powered"
print(mood) # coffee-powered
mood = "unstoppable"
print(mood) # unstoppable
The box called mood got new contents each time. The old value is gone — Python doesn't keep a history. Whatever's in there now is what matters.
This is enormously useful. Your program can track things that change over time — a game score, a user's input, a running total.
Here's variables doing something genuinely useful:
# Shopping cart for an online bookstore
item_name = "Python Book"
price = 29.99
quantity = 2
total = price * quantity
print(f"Buying {quantity}x {item_name}")
print(f"Total cost: ${total}")
Output:
Buying 2x Python Book
Total cost: $59.98
Line by line:
item_name = "Python Book" — stores the product nameprice = 29.99 — stores the unit pricequantity = 2 — stores how many we're buyingtotal = price * quantity — calculates: 29.99 * 2 = 59.98print lines display everything nicelyNotice the f"..." syntax — those are f-strings (formatted strings). The f before the quote tells Python "this string has variables inside it." Anything in {} curly braces gets replaced with the variable's actual value. It's the cleanest way to mix text and data in Python.
f-strings were introduced in Python 3.6 and quickly became the preferred way to format output. Compare these three approaches:
name = "Jordan"
score = 95
# Old way (messy)
print("Player " + name + " scored " + str(score) + " points.")
# Medium way (workable)
print("Player {} scored {} points.".format(name, score))
# f-string (clean and modern)
print(f"Player {name} scored {score} points.")
Output (all three):
Player Jordan scored 95 points.
Use f-strings. They're readable, they're modern, and they're what professionals write today.
Python has a strong convention for variable names: snake_case. That means all lowercase letters with underscores between words:
# Good Python style (snake_case)
first_name = "Alice"
total_price = 49.99
number_of_students = 30
is_logged_in = True
# Not-so-good Python style (but technically works)
firstName = "Alice" # camelCase — used in JavaScript, not Python
TotalPrice = 49.99 # PascalCase — used for class names in Python
totalprice = 49.99 # No separator — hard to read at a glance
Why does naming style matter? Because code is read far more often than it's written. You'll come back to your code days or months later. Other people might read it too. A variable called t tells you nothing; total_price tells you everything. Readable code is a kindness to your future self.
# Student grade tracker
student_name = "Priya Sharma"
math_score = 88
science_score = 92
english_score = 79
average = (math_score + science_score + english_score) / 3
print(f"Student: {student_name}")
print(f"Math: {math_score}")
print(f"Science: {science_score}")
print(f"English: {english_score}")
print(f"Average Score: {average:.1f}")
Output:
Student: Priya Sharma
Math: 88
Science: 92
English: 79
Average Score: 86.3
The :.1f inside the f-string means "show this number with 1 decimal place." More on formatting later — but notice how naturally the code reads even now.
name = value — the = means assignment, not equality_, no spaces, no special characters, case-sensitivef"text {variable}") are the cleanest way to mix text and variablesGet this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises