Python divmod() Function



The python divmod() function is a built-in function that 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 te divmod() function, we will encounter TypeError and if 0 is passed as second argument, it will return ZeroDivisonError.

In the next few section, we will be learning more about this function.

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..

Example

Now, let's understand the working of divmod() function with the help of examples −

Example 1

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 2

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 3

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 4

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 5

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
python_built_in_functions.htm
Advertisements