Lessons 28–29 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
28
Practical lesson: arithmetic operations
Textbook: pp. 64–65
GoalUses arithmetic operations, // and % to split quantities into units, works with the digits of a number, and writes calculation programs based on typed data.
New words
whole part of a quotient: found with // · bo‘linmaning butun qismiremainder: found with %; the part that is left over after division · qoldiqexpression: variables, constants, brackets and operations that give a value · ifodasplitting a quantity: the biggest unit with //, then the rest with % · kattalikni ajratish
Explanation
The main method of the practical lesson is using // and % as a pair: // gives the biggest unit and % gives what is left; the same can be repeated for the rest. For example, when splitting seconds into hours, minutes and seconds, t // 3600 is the hours, t % 3600 is the seconds left after the hours; dividing it by 60 gives the minutes and the remainder is the seconds. To get the digits of a three-digit x, x // 100 is the hundreds, x // 10 % 10 the tens and x % 10 the ones. Before writing the program test it by hand with a small number: 7385 seconds must be 2 hours 3 minutes 5 seconds. A comment can be written after the # sign to explain the code, and Python does not run it. Do not forget to give the result with units of measurement.
Worked examples
Splitting seconds: t = int(input()) ⏎ h = t // 3600 ⏎ m = t % 3600 // 60 ⏎ s = t % 60 ⏎ print(h, m, s) If 7385 is typed, 2 3 5 is printed, that is 2 hours 3 minutes 5 seconds.
Digits of a three-digit number: x = int(input()) ⏎ a = x // 100 ⏎ b = x // 10 % 10 ⏎ c = x % 10 ⏎ print(a + b + c, a * b * c) If 372 is typed, the sum 12 and the product 42 are printed.
Class activity
“Chain of units”: the group chooses the length of something in centimetres (for example 538 cm) and splits it into metres and centimetres; then it tests the program by changing the number several times.
Practice
1
How many metres and centimetres is 538 cm? (using // and %)
5 m 38 cm
2
If a = 20 and b = 3, what is a after a -= b, a *= 2, a %= 7?
6
3
A room is 5 m long, 4 m wide and 3 m high. What is the total area of the four walls (m²), ignoring windows?
54
4
Why does 7385 // 3600 give the hours and % 3600 give the leftover seconds?
// gives the whole part of the quotient (how many hours) and % gives the leftover seconds that do not fill a whole hour.