Python divmod() Function


The Python divmod() is a built-in function that takes two (non-complex) numbers as arguments and returns a pair of numbers consisting of their quotient and remainder when using integer division.

python divmod() function

Division and Modulo are two different but related operations as one returns the quotient and another returns the remainder. But Python divmod() integrates these both operations within a function and returns quotient and remainder as a pair in a tuple.

Python divmod() Syntax

divmod(a,b)

As you can see in above syntax, divmod() function takes two arguments.

  • a (required) – numerator (non-complex)
  • b (required) – denominator (non-complex)

What does Python divmod() returns?

As we mentioned earlier, divmod() function returns a tuple which contains a pair of the quotient and the remainder like (quotient, remainder).

Note: When we use mixed operands, the rules of binary arithmetic operators apply.

  • For integers, the return value is the same as (a // b, a % b).
  • For floating point numbers the return value is (q, a % b), where q is usually math.floor(a / b) which is the whole part of the quotient.

How does Python divmod() works?

>>> divmod(10,7)
(1,3)
>>> divmod(13.5,7.8)
(1.0,5.7)

As you can see in above example divmod() combines two division operators. It performs an integral division(/) and a modulo division(%) and returns a tuple containing the result of both integral division and modulo division.

Here is an example to clear it.

Python divmod() Function Example

x = 5
y = 3

result = divmod(x,y)

#Printing both elements
print(result[0])
print(result[1])

This script will generate following output.

1
2