In Python, the divmod() function is a built-in function used to divide two numbers and return both the quotient and remainder.
The divmod() function takes two parameters, the dividend and divisor, and returns a tuple containing the quotient and remainder. The quotient is the result of dividing the two numbers, while the remainder is the leftover part after division.
divmod() Function Examples
Here's an example using the divmod() function:
x = 10
y = 3
result = divmod(x, y)
print(result)
Output
(3, 1)
The divmod() function cannot handle complex numbers.
The divmod() function can also handle floating-point numbers. If both parameters are integers, the quotient and remainder returned by divmod() will also be integers. If one of the parameters is a float, the returned quotient and remainder will be of the corresponding type.
# Integer division
print(divmod(10, 3)) # Output: (3, 1)
# Floating-point division
print(divmod(5.5, 2)) # Output: (2.0, 1.5)
Note that if the divisor is 0, the divmod() function will raise a ZeroDivisionError exception.