What is the divmod() function in Python?
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.
Syntax
The syntax of the divmod() method in Python is as follows.
divmod(dividend, divisor)
Parameters
dividendornumeratordivisorordenominator
This function takes two non-complex integers as parameters.
Return value
divmod() returns a tuple that contains the quotient and remainder.
- If
x(dividend) andy(divisor) are integers, then the return value is the pair(x / y, x % y). - If
x(dividend) andy(divisor) are floats, the result is the pair of the whole part of the quotient andxmodulusy.
Question
Check if a number is prime or not by using the divmod() function.
Show Answer
Code
Below are some examples to understand the functionality of the divmod() method.
- Case #1: both arguments are integer values, which results in a tuple of integer values.
- Casen#2: both arguments are floating-point or double types, which results in a tuple of float values.
- Casen#3: one value is a floating-point value and the other is an integer, which results in a tuple of float values.
# Python divmod() function example# Calling functionresult = divmod(20,2)# Displaying resultprint(result)print(type(result))