Learning Goals
3 minBy the end of this lesson you can:
- Tell the difference between an
intand afloatin Python. - Use
+,-,*,/,//,%, and**with confidence. - Combine numbers and variables to solve everyday problems.
Warm-Up
5 minLast lesson we gave Python a memory. Variables hold values; one tiny change at the top can update everything below.
Quick puzzle. Imagine you have RM 10. You buy a roti canai for RM 3.50 and a teh tarik for RM 1.20.
- How much do you have left?
- If you split that left-over money equally between 3 friends, how much does each friend get?
Work both out on paper before we ask Python to do them.
If you can solve it with a pencil, Python can solve it ten thousand times faster — and never makes a typo.
New Concept · Two Number Types & Seven Tricks
12 minPart 1 · Two flavours of number
Python has two main number types:
- int — a whole number, like
5,0, or-7. - float — a number with a decimal point, like
3.14or5.0.
print(5) print(5.0) print(-2.7)
If any decimal sneaks into the maths, the answer becomes a float.
Part 2 · The four classic operators
print(7 + 3) print(7 - 3) print(7 * 3) print(7 / 3)
The slash / always gives a float — even when the answer is whole. 10 / 2 is 5.0, not 5.
Part 3 · Two new friends — // and %
Sometimes we don't want the decimal version. Two operators help:
- // floor division — divide and drop the decimals.
- % modulo — divide and keep only the remainder.
print(7 // 3) print(7 % 3)
Together they answer one classic question: how many full groups fit, and what is left over?
Part 4 · ** for powers
Two stars ** means "to the power of":
print(2 ** 3) print(5 ** 2) print(10 ** 6)
Part 5 · Order of operations
Python uses the same order you learned in maths class. Brackets win first, then powers, then × ÷ // %, then + −.
print(2 + 3 * 4) print((2 + 3) * 4) print(2 ** 3 + 1)
When in doubt, add brackets. Brackets cost nothing and make your maths obvious to other readers.
Worked Example · A Lunchtime Calculator
12 minStep 1 · Total a mamak order
Three prices, three quantities, one total. Save this as lunch.py.
Code
# lunch.py — work out a mamak lunch total # 1. Prices in ringgit nasi_lemak_price = 5.50 teh_tarik_price = 2.50 kuih_price = 1.50 # 2. How many of each nasi_lemak_qty = 2 teh_tarik_qty = 1 kuih_qty = 3 # 3. Maths total = (nasi_lemak_price * nasi_lemak_qty + teh_tarik_price * teh_tarik_qty + kuih_price * kuih_qty) print("Subtotal: RM", total)
Output
Subtotal: RM 18.0
Try changing the quantities at the top and re-run — the total updates without touching the maths line.
Step 2 · Sharing sweets — // and % together
You have 17 sweets and 4 friends. How many sweets each, and how many left over for you?
sweets = 17 friends = 4 each_gets = sweets // friends left_over = sweets % friends print("Each friend gets", each_gets, "sweets") print("Sweets left over:", left_over)
Output
Each friend gets 4 sweets Sweets left over: 1
Step 3 · Squaring a number with **
side = 5 area = side ** 2 print("A square with side", side, "has area", area)
Single * is multiply. Double ** is power. Mixing them up is the most common bug of this lesson!
Try It Yourself
13 minType each task into your editor. Run it. Fix any typos before moving on.
Two friends each eat three pieces of roti canai. Use one variable for the number of friends, one for pieces each, and multiplication to print the total pieces.
Hint
Two variables, then total = friends * pieces_each, then print(...).
You walk 1.5 km to school, twice a day, five days a week. Use multiplication to print the kilometres walked in one full week.
Hint
1.5 * 2 * 5. Because 1.5 is a float, the answer is a float.
There are 50 days until your birthday. Use // and % to work out how many full weeks and how many extra days that is. Print one tidy line: "X weeks and Y days".
Hint
One week is 7 days. 50 // 7 gives the weeks. 50 % 7 gives the leftover days.
Mini-Challenge · Pocket Money Planner
8 minBuild a small planner that tells you how many treats your savings can afford.
Your file must include:
- Three variables:
weekly_ringgit,weeks_saved,treat_price. - A
total_savedvariable using multiplication. - A
treats_affordablevariable using//. - A
money_leftvariable using%. - A friendly four-line print-out reporting all four numbers (in
RM).
Stretch goal. Add a big_treat_price using ** — for example treat_price ** 2 for a fancy double-treat. Print whether you can afford one.
Show one possible solution
# pocket_money.py — savings + treat planner # Inputs weekly_ringgit = 10 weeks_saved = 8 treat_price = 6 # Maths total_saved = weekly_ringgit * weeks_saved treats_affordable = total_saved // treat_price money_left = total_saved % treat_price # Report print("Total saved: RM", total_saved) print("Treats: ", treats_affordable) print("Money left: RM", money_left) print("Per-treat cost: RM", treat_price) # Stretch — a fancy double-treat big_treat_price = treat_price ** 2 print("A double-treat costs: RM", big_treat_price) print("Can I afford one?", total_saved >= big_treat_price)
The last line uses >= — a sneak preview of booleans, which we'll meet properly in a later lesson. For now, just enjoy that Python answers True or False.
Recap
3 minPython is now a calculator that remembers. We met seven operators and two number types, and used variables to keep our maths tidy.
Vocabulary Card
- int
- A whole number, like
5or-7. - float
- A number with a decimal point, like
3.14. - // (floor division)
- Divide and drop the decimals — keeps the whole-number part.
- % (modulo)
- Divide and keep only the remainder.
- ** (power)
- Raise to a power.
2 ** 3is8.
Homework
4 minCreate a new file called garden.py for a square garden.
Rules:
- One variable for the side length in metres (you choose the number).
- Use
**to calculate the area in square metres. - Use
*to calculate the perimeter. - Grass costs RM 8.50 per square metre — print the total cost to lay grass.
- Stretch goal — paving slabs cover the perimeter at 2 slabs per metre. Print the total slabs needed.
Bring the file to the next lesson — we'll compare gardens!
Sample · garden.py
# garden.py — measurements and cost for a square garden in PJ # Inputs side_m = 6 grass_per_m2 = 8.50 # Maths area_m2 = side_m ** 2 perimeter_m = side_m * 4 total_cost = area_m2 * grass_per_m2 # Report print("Garden side: ", side_m, "m") print("Garden area: ", area_m2, "m²") print("Garden perimeter: ", perimeter_m, "m") print("Cost of grass: RM", total_cost) # Stretch — paving slabs (2 per metre of perimeter) slabs_needed = perimeter_m * 2 print("Paving slabs needed:", slabs_needed)
Yours can use any side length. As long as you use ** for area and * for perimeter and total cost, you've nailed it.