Arithmetic Operators
When you divide one whole number by another you can pick out two useful pieces of information: how many whole times the second number fits, and what is left over. Python has an operator for each of them.
The quotient, also called integer division, uses // and gives the whole number of times one number divides into another. With positive whole numbers the fractional part is discarded rather than rounded, so 38 // 4 gives 9: 4 fits into 38 nine whole times (4 × 9 = 36) and there is not enough left to make a tenth group.
Modulo uses % and gives the remainder left behind by that whole-number division, so 38 % 4 gives 2. When the first number divides exactly by the second, nothing is left over and modulo gives zero, so 48 % 6 gives 0.
| Operator | What it gives | 74 books packed in boxes of 8 |
|---|---|---|
/ | the full answer, including any fractional part | 9.25 |
// | the whole-number part only | 9 |
% | the remainder | 2 |
You can always rebuild the number you started with (the dividend): the quotient multiplied by the number you divided by (the divisor), plus the remainder, equals the dividend. With 74 and 8 that is 9 × 8 = 72 and 72 + 2 = 74, which in Python is written (74 // 8) * 8 + (74 % 8). This check also reminds you that // is not rounding to the nearest whole number — it keeps only the whole-number part.
If the divisor is larger than the dividend, no complete group can be made, so the quotient is 0 and the remainder is the dividend itself: 5 // 8 gives 0 and 5 % 8 gives 5.
Integer division and modulo are used with whole numbers, and exam questions use positive whole numbers so the results stay predictable.
This program works out both parts of a division from two values you type in.
dividend = int(input("Number to divide: "))
divisor = int(input("Divide by: "))
quotient = dividend // divisor # whole-number part
remainder = dividend % divisor # what is left over
print("Quotient:", quotient)
print("Remainder:", remainder)Output when the user types 94, 8:
Number to divide: 94
Divide by: 8
Quotient: 11
Remainder: 6 OCR GCSE Computer Science · Trace Tables
OCR GCSE Computer Science · Arithmetic Operators
OCR GCSE Computer Science · Arithmetic Operations