Tutorial Study Image

Python divmod()

In Python divmod, the function takes two numbers, divides the first by the second, and returns the tuple of quotient and reminder.


divmod(dividend,divisor) #The dividend will be divided by 

divmod() Parameters:

The function takes two mandatory parameters, the first parameter is treated as the dividend and the second as the divisor. The second parameter cannot be zero, since

Parameter Description Required / Optional
dividend A number, which will be divided Required
divisor A number used to divide. The number cannot be zero Required

divmod() Return Value

The function always returns a tuple of quotients and remainder based on the input. Both the parameters are mandatory.

Input Return Value
Dividend and divisor A tuple of quotient and reminder
String as first parameter and encoding as the second parameter The string encoded to bytes
Iterable Byte as the same size as the iterable
No parameter Creates a byte object with no elements

Examples of divmod() method in Python

Example 1: Passing an integer as a parameter


result = divmod(2,1)
print(“Divmod of 2 and 1 are :”, result)
result = divmod(3.5,2.5)
print(“Divmod of 3.5 and 2.5 are :”, result)
 

Output:

Divmod of 2 and 1 are : (2, 0)
Divmod of 3.5 and 2.5 are : (1.0, 1.0)

Example 2: Passing an float as a parameter


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)