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.
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.
divmod(a,b)
As you can see in above syntax, divmod() function takes two arguments.
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.
a // b
, a % b
).(q, a % b)
, where q
is usually math.floor(a / b)
which is the whole part of the quotient.>>> 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.
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