Variables: names for values
A variable is a name you attach to a value so you can use it later:
price = 1500
quantity = 3
print(price * quantity)
4500
The = means "store the right side under the name on the left". Read price = 1500 as "price gets 1500", not "price equals 1500".
Naming rules
- Letters, digits, underscores — but cannot start with a digit:
item2is fine,2itemis not. - Case matters:
Priceandpriceare different variables. - Style: lowercase with underscores —
total_cost, notTotalCostortc. Name things so future-you understands them.
The four core types
age = 25 # int — whole numbers
price = 499.99 # float — decimal numbers
name = "Chidinma" # str — text, in quotes
is_paid = True # bool — True or False (capitalised!)
Ask Python what type something is with type():
>>> type(1500)
<class 'int'>
>>> type("1500")
<class 'str'>
Notice: 1500 and "1500" are different things. One is a number you can do maths with; the other is text that happens to look like a number.
Arithmetic
print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.3333333333333335 (/ always gives a float)
print(10 // 3) # 3 (floor division — whole part only)
print(10 % 3) # 1 (remainder / modulo)
print(10 ** 3) # 1000 (power)
When types collide: your first traceback
Type this and run it:
price = "1500"
total = price * 3 + 100
Traceback (most recent call last):
File "shop.py", line 2, in <module>
total = price * 3 + 100
~~~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to str
Do not panic — read the error first, and read it bottom-up. The last line names the problem: TypeError — you tried to add an int to a str. The line above it shows exactly where. ("1500" * 3 is "150015001500" — Python repeats strings — and then adding 100 to text fails.)
This bottom-up habit is the most valuable debugging skill in this course. The last line says what went wrong; the lines above say where.
Try it now
Create budget.py. Store your monthly data bundle cost, transport cost, and food budget in three well-named variables. Print the total and print what type each variable is using type(). Then deliberately put quotes around one of the numbers, run it, and read the traceback bottom-up before fixing it.