225

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

1
  • I'd suggest looking up the modulo operator
    – yurib
    Apr 7, 2011 at 16:49

13 Answers 13

272

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.

2
  • 16
    Note that the modulo operator always returns a positive number, so for negative numbers it might not be what you would expect when talking about the remainder: -10 % 3 == 2. However a/b*b + a%b == a still holds true, since python always rounds towards -Infinity, unlike some other languages, which round towards 0 but would return -1.
    – marcelj
    Oct 1, 2014 at 12:54
  • 6
    That's because Python's % performs a true modulus, which returns values on the range [0, divisor) and pairs well with floored division (towards negative infinity). C languages use the % operator for remainder operations which returns values on the range (-divisor, divisor) and pairs well with standard division (towards zero). May 19, 2018 at 6:53
239

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)
0
75

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)

2
  • 4
    Is it possible to get both in a single line? divisor, remainder = 26 <operation symbol> 7
    – Guimoute
    Mar 15, 2022 at 17:50
  • 2
    @Guimoute please see this answer in this same question.
    – tzot
    Nov 23, 2022 at 17:01
41

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)
1
  • An easier way to compute quotient is to use the //. quotient = dividend // divisor
    – Pedram
    Jan 17, 2021 at 10:52
25

From Python 3.7, there is a new math.remainder() function:

from math import remainder
print(remainder(26,7))

Output:

-2.0  # not 5

Note, as above, it's not the same as %.

Quoting the documentation:

math.remainder(x, y)

Return the IEEE 754-style remainder of x with respect to y. For finite x and finite nonzero y, this is the difference x - n*y, where n is the closest integer to the exact value of the quotient x / y. If x / y is exactly halfway between two consecutive integers, the nearest even integer is used for n. The remainder r = remainder(x, y) thus always satisfies abs(r) <= 0.5 * abs(y).

Special cases follow IEEE 754: in particular, remainder(x, math.inf) is x for any finite x, and remainder(x, 0) and remainder(math.inf, x) raise ValueError for any non-NaN x. If the result of the remainder operation is zero, that zero will have the same sign as x.

On platforms using IEEE 754 binary floating-point, the result of this operation is always exactly representable: no rounding error is introduced.

Issue29962 describes the rationale for creating the new function.

1
  • 3
    math.remainder may not work the way you expect example for python 3.7 >>> math.remainder(-2.99,2) -0.9900000000000002 >>> math.remainder(-3,2) 1.0 Apr 14, 2020 at 17:05
20

If you want to avoid modulo, you can also use a combination of the four basic operations :)

26 - (26 // 7 * 7) = 5
5

Use the % instead of the / when you divide. This will return the remainder for you. So in your case

26 % 7 = 5
5

We can solve this by using modulus operator (%)

26 % 7 = 5;

but 26 / 7 = 3 because it will give quotient but % operator will give remainder.

1
  • 4
    If you use Python 2, 26 / 7 = 3 , for Python 3, 26/7 is about 3.7142857142857144
    – Antti A
    May 14, 2018 at 7:00
4

Modulo would be the correct answer, but if you're doing it manually this should work.

num = input("Enter a number: ")
div = input("Enter a divisor: ")

while num >= div:
    num -= div
print num
1
3

You can find remainder using modulo operator Example

a=14
b=10
print(a%b)

It will print 4

1
  • This answer is misleading, the question was explicitly asking "remainder". Python uses module for % operator, what is mathematically different. More info here: stackoverflow.com/questions/4432208/…
    – mTheSame
    Oct 4, 2021 at 12:29
0

If you want the remainder of your division problem, just use the actual remainder rules, just like in mathematics. Granted this won't give you a decimal output.

valone = 8
valtwo = 3
x = valone / valtwo
r = valone - (valtwo * x)
print "Answer: %s with a remainder of %s" % (x, r)

If you want to make this in a calculator format, just substitute valone = 8 with valone = int(input("Value One")). Do the same with valtwo = 3, but different vairables obviously.

1
  • you have to cast valone/valtwo to int. else it will result in a float number. Jan 2, 2019 at 9:02
0

Here's an integer version of remainder in Python, which should give the same results as C's "%" operator:

def remainder(n, d):
    return (-1 if n < 0 else 1) * (abs(n) % abs(d))

Expected results:

remainder(123, 10)   ==  3
remainder(123, -10)  ==  3
remainder(-123, 10)  == -3
remainder(-123, -10) == -3
-1

you can define a function and call it remainder with 2 values like rem(number1,number2) that returns number1%number2 then create a while and set it to true then print out two inputs for your function holding number 1 and 2 then print(rem(number1,number2)

1
  • 2
    Please add a working example. Explaining some theoretical code might be hard to understand.
    – mischva11
    Jun 25, 2020 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.