Search code examples
pythonmodulointeger-division

Find the division remainder of a number


How could I go about finding the division remainder of a number in Python?

For example:
If the number is 26 and divided number is 7, then the division remainder is 5.
(since 7+7+7=21 and 26-21=5.)


For simple divisibility testing, see How do you check whether a number is divisible by another number?.


Solution

  • 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.