Python divmod() function
The divmod() function in Python returns a tuple containing the quotient and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor).
Syntax:
1 2 3 |
divmod(a, b) |
Parameter
a: This parameter represents a number you want to divide i.e., the dividend.
b: This parameter represents a number that you want to divide with i.e., the divisor.
Return
This function returns a pair of numbers consisting of their quotient and remainder.
Example 1
1 2 3 4 5 6 |
# Python Program explaining # divmod() function x = divmod(52, 2) print('divmod() function will return: ',x) |
Output
1 2 3 |
divmod() function will return: (26, 0) |
Example 2
1 2 3 4 5 6 7 8 9 |
# Python Program explaining # divmod() function print('(15, 14) = ', divmod(5, 4)) print('(140, 16) = ', divmod(10, 16)) # divmod() with int and Floats print('(62.0, 45) = ', divmod(8.0, 3)) print('(33, 29.0) = ', divmod(3, 8.0)) |
Output
1 2 3 4 5 6 |
(15, 14) = (1, 1) (140, 16) = (0, 10) (62.0, 45) = (2.0, 2.0) (33, 29.0) = (0.0, 3.0) |