Python divmod() Function
The Python divmod() function accepts two numbers as parameter values and returns a tuple containing two values namely the quotient and remainder of their division.
If we pass non-numeric parameters such as strings to the divmod() function, we will encounter TypeError and if 0 is passed as the second argument, it will return ZeroDivisonError.
The divmod() function is one of the built-in functions and does not require any module to import.
Syntax
The following is the syntax for the python divmod() function.
divmod(dividend, divisor)
Parameters
The Python divmod() function accepts two parameters which are as follows β
dividend β This parameter specifies the number that is going to divide.
divisor β This parameter represents the number by which the dividend will be divided.
Return Value
The python divmod() function returns quotient and remainder as a tuple.
divmod() Function Examples
Practice the following examples to understand the use of divmod() function in Python:
Example: Use of divmod() Function
The following is an example of the Python divmod() function. In this, we are passing two integers as arguments to the divmod() function which will return a tuple consisting of their quotient and remainder.
output = divmod(18, 5)
print("The output after evaluation:", output)
On executing the above program, the following output is generated β
The output after evaluation: (3, 3)
Example: divmod() With Negative Values
If we pass negative numbers to the divmod() function, it returns the floor value as the quotient and the remainder as illustrated in the below code.
output = divmod(-18, 5)
print("The output after evaluation:", output)
The following is the output obtained by executing the above program β
The output after evaluation: (-4, 2)
Example: divmod() With Float Values
The Python divmod() function is also compatible with floating point numbers. In the following example, we pass the float values as arguments to this function which will return the float in result.
output = divmod(18.5, 5)
print("The output after evaluation:", output)
The following output is obtained by executing the above program -
The output after evaluation: (3.0, 3.5)
Example: Divide By Zero Error With divmod() Function
When the second argument of the divmod() is 0, it will raise a ZeroDivisionError. In the below code, we are passing 0 as divisor, hence, getting ZeroDivisionError.
try:
print(divmod(27, 0))
except ZeroDivisionError:
print("Error! dividing by zero?")
The above program, on executing, displays the following output -
Error! dividing by zero?
Example: Convert Seconds to Hours, Minutes, and Seconds
In the code below, we are demonstrating a practical application of divmod() function in Python. Here, we are converting the seconds into hours and minutes.
secValue = 8762
hours, remainingSec = divmod(secValue, 3600)
minutes, seconds = divmod(remainingSec, 60)
print("The total time after evaluating seconds:")
print(f"{hours} hours, {minutes} minutes, {seconds} seconds"))
The above program, on executing, displays the following output -
The total time after evaluating seconds: 2 hours, 26 minutes, 2 seconds