divmod()
is a built-in method in Python that takes two real numbers as arguments and returns a pair of numbers (tuple
) consisting of quotient q
and remainder r
values. Although modulus and division are two different operations, they are indirectly related as one returns the quotient
, and the other one returns the remainder
.
The syntax of the divmod()
method in Python is as follows.
divmod(dividend, divisor)
dividend
or numerator
divisor
or denominator
This function takes two non-complex integers as parameters.
divmod()
returns a tuple
that contains the quotient and remainder.
x
(dividend) and y
(divisor) are integers, then the return value is the pair (x / y, x % y)
.x
(dividend) and y
(divisor) are floats, the result is the pair of the whole part of the quotient and x
modulus y
.Application: Checking a number, either a Prime number or not.
Check if a number is prime or not by using the divmod()
function.
Below are some examples to understand the functionality of the divmod()
method.
# Python divmod() function example# Calling functionresult = divmod(20,2)# Displaying resultprint(result)print(type(result))