AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Arithmetic practice and strings

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.

We findExpression
hourst // 3600
minutest % 3600 // 60
secondst % 60
💡 Where will I use this?
In real life

At a school evening a video’s running time of 4500 seconds had to be shown in hours, minutes and seconds. An organiser used division and remainder and wrote it clearly as 1 hour 15 minutes.

Who uses it

Programmers, technicians and analysts show a large amount in hours, minutes or other units.

Try it today

Work out hours, minutes and seconds for another number, such as 5400 seconds, on paper. Use division and remainder in turn.

Worked examples
Python
1t = int(input())
2h = t // 3600
3m = t % 3600 // 60
4s = t % 60
5print(h, m, s)
Output
2 3 5
Splitting seconds – code above. If 7385 is typed, 2 3 5 is printed, that is 2 hours 3 minutes 5 seconds.
Python
1x = int(input())
2a = x // 100
3b = x // 10 % 10
4c = x % 10
5print(a + b + c, a * b * c)
Output
12 42
Digits of a three-digit number – code above. 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 %)
2
If a = 20 and b = 3, what is a after a -= b, a *= 2, a %= 7?
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?
4
Why does 7385 // 3600 give the hours and % 3600 give the leftover seconds?
Story of the Week · The Khwarizmi WayThe first unhappy customerClose the week: all of this week’s subjects in one storyRead →