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
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
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) print(result)
This script will generate following output.