In the abstract, the classic remainder formula is: Dividend/Divisor = Quotient + Remainder/Divisor.
The quotient (integer division) of 599/9 equals 66; the remainder (“left over”) is 5.
The quotient (integer division) of 3300/19 equals 173; the remainder (“left over”) is 13.
you are looking for the modulo operator:
a % b
for example:
>>> 26 % 7
5
Of course, maybe they wanted you to implement it yourself, which wouldn't be too difficult either.
The remainder of a division can be discovered using the operator %
:
>>> 26%7
5
In case you need both the quotient and the modulo, there's the builtin divmod
function:
>>> seconds= 137
>>> minutes, seconds= divmod(seconds, 60)
26 % 7
(you will get remainder)
26 / 7
(you will get divisor, can be float value)
26 // 7
(you will get divisor, only integer value)
If you want to get quotient and remainder in one line of code (more general usecase), use:
quotient, remainder = divmod(dividend, divisor)
#or
divmod(26, 7)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With