Practical lesson: input and output
Most tasks of this lesson are alike: an amount is shared into equal portions and the question is what is left over. Here // gives each portion or the number of whole groups, and % gives the leftover part. In a money example we find the number of the biggest banknotes with //, and then repeat the same thing for the remaining sum with smaller banknotes. If several numbers are typed in one line, input().split() separates them, and then each part must be converted with int(). To find the arithmetic mean of several numbers, put the sum in brackets and divide by how many numbers there are. Always test the program on 2–3 examples worked out by hand, especially in the case where the remainder is zero.
Organisers of a school evening needed to seat 130 guests in rows of 12. Division with remainder gave 10 full rows and 10 guests left over, so they planned one more short row.
Storekeepers, organisers and programmers split items into equal portions and account for the remainder.
On paper, change 200 minutes into hours and minutes using whole quotient and remainder, then check by adding back.
1n = int(input()) 2a = n // 50000 3r = n % 50000 4b = r // 10000 5c = r % 10000 6print(a, b, c)
3 3 7000
1a = input().split() 2k = int(a[0]) 3m = int(a[1]) 4print(k // m, k % m)
4 5
“Shop cashier”: a pair chooses the price of an item and the money paid; it writes a program giving the change in the largest banknotes (// and %), and the partner checks it by hand.