Python divmod() Method
The divmod()
method takes two non-complex numbers as arguments and returns a tuple of two numbers where the first number is the quotient and the second is the remainder.
Syntax:
divmod(number1, number2)
Parameters:
- number1: Numerator.
- number2: Denominator.
Return Value:
Returns a tuple (quotient, remainder) by calculating (number1 // number2, number1 % number2).
The following example returns quotient and remainder of various numbers.
print("Divmod of (6,2) is: ", divmod(6,2))
print("Divmod of (8,3) is: ", divmod(8,3))
print("Divmod of (7,2) is: ", divmod(7,2))
print("Divmod of (3,10) is: ", divmod(3,19))
Divmod of (6,2) is: (3, 0)
Divmod of (8,3) is: (2, 2)
Divmod of (7,2) is: (3, 1)
Divmod of (3,10) is: (0, 3)
If you pass floating point numbers, the result would be (math.floor(number1 / number2), number1 % number2), as shown below.
print("Divmod of (6.5, 2) is: ", divmod(6.5, 2))
print("Divmod of (6, 2.5) is: ", divmod(6, 2.5))
Divmod of (6.5, 2) is: (3.0, 0.5)
Divmod of (6, 2.5) is: (2.0, 1.0)
Passing complex numbers would throw an error.
print("Divmod of (6.5, 2) is: ", divmod(3 + 2j, 6 + 8j))
TypeError: can't take floor or mod of complex number.