The divmod() method takes two numbers as arguments and returns their quotient and remainder as a tuple.
Syntax
divmod(dividend, divisor)divmod() Parameters
Python divmod() function parameters:
| Parameter | Condition | Description |
|---|---|---|
dividend |
Required | The number being divided |
divisor |
Required | The number doing the dividing |
divmod() Return Value
Python divmod() function returns a tuple (quotient, remainder) that contains quotient and remainder of the division
Examples
Example 1: divmod() with Integer Arguments
divmod() function with integer arguments:
print('divmod(8, 3) = ', divmod(8, 3))
print('divmod(3, 8) = ', divmod(3, 8))
print('divmod(5, 5) = ', divmod(5, 5))
print('divmod(5, 5) = ', divmod(2, 5))output
divmod(8, 3) = (2, 2)
divmod(3, 8) = (0, 3)
divmod(5, 5) = (1, 0)
divmod(5, 5) = (0, 2)Example 2: divmod() with Float Arguments
divmod() function with float arguments:
print('divmod(8.0, 3) = ', divmod(8.0, 3))
print('divmod(3, 8.0) = ', divmod(3, 8.0))
print('divmod(7.5, 2.5) = ', divmod(7.5, 2.5))
print('divmod(2.6, 0.5) = ', divmod(2.6, 0.5))output
divmod(8.0, 3) = (2.0, 2.0)
divmod(3, 8.0) = (0.0, 3.0)
divmod(7.5, 2.5) = (3.0, 0.0)
divmod(2.6, 0.5) = (5.0, 0.10000000000000009)Example 3: divmod() with Non-Numeric Arguments
a = divmod("Elon", "Musk")
print('The divmod of Elon and Musk is = ', a)output
Traceback (most recent call last):
File "main.py", line 1, in <module>
a = divmod("Elon", "Musk")
TypeError: unsupported operand type(s) for divmod(): 'str' and 'str'