Arithmetic Operators
For positive whole numbers, integer division (written //) divides one whole number by another and keeps only the whole-number part of the answer, discarding the remainder. Modulus (written %) gives you that remainder. Both are arithmetic operators in Python 3, and they are closely linked: one gives the quotient and the other gives the remainder from the same division.
Picture 29 sweets shared equally between 6 people. Each person gets 29 // 6 = 4 sweets, because 6 fits into 29 four whole times. There are 5 sweets left over, so 29 % 6 = 5. The % symbol means modulus here — it has nothing to do with percentages.
With positive whole numbers, integer division does not round to the nearest whole number. 29 // 6 is 4, even though 29 ÷ 6 is about 4.83, which is nearer to 5. The remainder is simply discarded. If you used / instead, you would get 4.8333…, which keeps the fractional part.
The quotient and the remainder both come from the same division, so they always link back to the number you started with:
quotient × divisor + remainder = dividend
For the sweets: 4 × 6 + 5 = 29. You can use this to check either answer. If dividing by 8 gives a quotient of 7 and a remainder of 3, the original number must have been 7 × 8 + 3 = 59.
sweets = 29
people = 6
each = sweets // people
leftover = sweets % people
print("Each person gets " + str(each))
print("Left over: " + str(leftover))Output:
Each person gets 4
Left over: 5
The program prints the whole-number share and the leftover amount, which are the quotient and the remainder of the same division.
Edexcel GCSE Computer Science · Arithmetic Operators
Edexcel GCSE Computer Science · Arithmetic Operators