Lesson notes (text version)
1. f-strings: fill in the blanks
Gluing text with + gets messy, and you must remember str() for numbers. An f-string is easier: put f before the quotes and write variables inside { }.
f"Hi {name}, your plan is ₹{price}"
You can even do maths inside the braces: f"A year costs ₹{price * 12}". Python works out the value and drops it into the text. No str() needed.
name = "Meera"
price = 239
print(f"Hi {name}, your plan is ₹{price}")
print(f"A year costs ₹{price * 12}")
Challenge: Mobile recharge message
The program reads a name and a plan price (a number). Using f-strings, print two lines. For Asha and 299 they must be:
Hi Asha, your ₹299 plan is active.12 recharges cost ₹3588 in a year.
Work out 12 × price inside the braces.
2. // and %: boxes and leftovers
Python has two maths operators that are perfect for packing things:
// is floor division: how many whole times. 100 // 12 is 8% is remainder (modulo): what is left over. 100 % 12 is 4
So 100 laddoos make 8 full boxes of 12, with 4 laddoos left over. (Plain / would give 8.333…, which is not much use when packing boxes.)
Other operators: + - *, and ** for power: 2 ** 3 is 8.
print(100 / 12)
print(100 // 12)
print(100 % 12)
print(2 ** 10)
Challenge: Diwali laddoo boxes
Each Diwali box holds 12 laddoos. The program reads how many laddoos were made. Store the number of full boxes in boxes (use //) and the leftovers in left (use %). Print Full boxes: 8 and Left over: 4 for 100 laddoos.
3. round() for tidy decimals
Division with / often gives long decimals: 157 / 20 is 7.85, but 200 / 35 is 5.714285714285714. Nobody wants that on a scoreboard!
round(x, 2) rounds x to 2 decimal places: round(5.714285, 2) is 5.71. Without the second number, round() gives a whole number: round(5.7) is 6.
print(200 / 35)
print(round(200 / 35, 2))
print(round(5.7))
Challenge: Cricket run rate
Run rate = runs ÷ overs. The program reads runs and overs (whole numbers). Store the run rate rounded to 2 decimals in rate, and print Run rate: 7.85 for 157 runs in 20 overs. (For 200 runs in 35 overs it should print Run rate: 5.71.)