The divmod() function in Python is a built-in function used to perform division and modulus operations at the same time. It returns a pair of numbers consisting of the quotient and the remainder.
In other words, the divmod() function combines the operations of integer division (//) and modulus (%) into a single function.
The result is returned as a tuple containing two values:
divmod(a, b)
a The dividend (the number to be divided).
b The divisor (the number by which the dividend is divided).
The function returns a tuple containing two values:
[ (quotient, remainder) ]
Where:
print(divmod(10, 3))
Output
(3, 1)
Explanation
So the result is (3, 1).
a = 20
b = 6
result = divmod(a, b)
print(result)
Output
(3, 2)
Explanation
q, r = divmod(15, 4)
print("Quotient:", q)
print("Remainder:", r)
Output
Quotient: 3
Remainder: 3
Explanation
divmod() is unpacked into two variables.print(divmod(9.5, 2))
Output
(4.0, 1.5)
Explanation
The divmod() function is equivalent to performing two operations:
a = 10
b = 3
print(a // b)
print(a % b)
Output:
3
1
Using divmod():
print(divmod(10, 3))
Output:
(3, 1)
total_minutes = 130
hours, minutes = divmod(total_minutes, 60)
print("Hours:", hours)
print("Minutes:", minutes)
Output
Hours: 2
Minutes: 10
Explanation
divmod() is a built-in Python function that performs division and modulus operations together.// and % operators together.