Python divmod() Function
divmod(a, b) returns the tuple (a // b, a % b): the floor quotient and the remainder from a single division.When you need both how many times one number fits into another and what is left over, divmod() gives you both in one call. Classic uses: converting seconds to minutes and seconds, splitting items across pages, and picking digits off a number.
It is equivalent to using the // and % operators together, but it reads as one intention and does the division once. The result is a tuple, so you normally unpack it: q, r = divmod(17, 5).
| Syntax | divmod(a, b) |
|---|---|
| Parameters | a: dividend; b: divisor (non-zero); both are int or float |
| Returns | A tuple (quotient, remainder), equal to (a // b, a % b) |
| Raises | ZeroDivisionError if b is zero |
| Time complexity | O(1) for normal-sized integers |
How divmod() computes quotient and remainder
For positive integers the result is what you learned in school: divmod(17, 5) is (3, 2) because 5 goes into 17 three times with 2 left over. The two values always satisfy a == q * b + r.
q, r = divmod(17, 5)
print(q, r)
print(q * 5 + r)divmod() with negative numbers and floats
Python's // rounds down (towards negative infinity), and the remainder takes the sign of the divisor. So divmod(-7, 2) is (-4, 1), not (-3, -1). This floor behaviour differs from some other languages, and is useful because the remainder is always in the range between 0 and the divisor.
With floats the same rules apply, and both parts are floats: divmod(7.5, 2) is (3.0, 1.5). Floats can carry tiny rounding errors, so prefer integers when you can.
| Call | Result | Check |
|---|---|---|
divmod(17, 5) | (3, 2) | 3 * 5 + 2 = 17 |
divmod(-7, 2) | (-4, 1) | -4 * 2 + 1 = -7 |
divmod(7, -2) | (-4, -1) | -4 * -2 + -1 = 7 |
divmod(7.5, 2) | (3.0, 1.5) | Float inputs give float parts |
divmod(6, 3) | (2, 0) | Remainder 0 means exact division |
divmod() vs // and %
divmod(a, b) is the same as (a // b, a % b). Use it when you need both values, especially when a is the result of an expensive or changing computation, because you evaluate it once. When you need only one of them, the operators // and % are simpler. See also round() when you want the nearest whole number instead of the floor.
Examples you can run
Press Run, then edit the code and run it again.
Quotient and remainder
Unpack the tuple into two names.
pages, left = divmod(47, 10)
print(pages, left)Expected output4 7
Convert seconds to hours, minutes and seconds
Apply divmod() twice: first split off minutes, then hours.
total = 3725
minutes, seconds = divmod(total, 60)
hours, minutes = divmod(minutes, 60)
print(f"{hours}h {minutes}m {seconds}s")Expected output1h 2m 5s
Count pages for pagination
Round up by adding one page when there is a remainder.
items = 47
per_page = 10
full, rest = divmod(items, per_page)
pages = full + (1 if rest else 0)
print(pages)Expected output5
Sum the digits of a number
Each divmod(n, 10) peels off the last digit.
n = 472
total = 0
while n:
n, digit = divmod(n, 10)
total += digit
print(total)Expected output13
Negative numbers use floor division
The quotient rounds down, not toward zero.
print(divmod(-7, 2))
print(divmod(7, -2))
print(divmod(7.5, 2))Expected output(-4, 1)
(-4, -1)
(3.0, 1.5)
Common mistakes
Dividing by zero
✗ Wrong
print(divmod(10, 0))✓ Right
divisor = 0
if divisor != 0:
print(divmod(10, divisor))
else:
print("cannot divide by zero")Check the divisor first.
Treating the result as a single number
✗ Wrong
print(divmod(7, 2) + 1)✓ Right
q, r = divmod(7, 2)
print(q + 1)Unpack the tuple or index it.
Mixing up the order
✗ Wrong
r, q = divmod(17, 5)
print("quotient", q)✓ Right
q, r = divmod(17, 5)
print("quotient", q)The quotient always comes first.
Python divmod() Function: questions people ask
What does divmod() return in Python?
It returns a tuple (quotient, remainder), which is (a // b, a % b). For example divmod(17, 5) gives (3, 2).
Is divmod() faster than using // and % separately?
Not by a noticeable margin for small numbers. Choose it because it is clearer when you need both values.
How does divmod() work with negative numbers?
The quotient is floored, so divmod(-7, 2) is (-4, 1). The remainder takes the sign of the divisor.
Keep learning
Step-by-step lessons:
Python OperatorsPython NumbersPython Unpacking
Related:
Python pow() FunctionPython abs() FunctionPython round() FunctionPython ZeroDivisionError: Division by Zero
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →