divmod()

One call for both quotient and remainder — with floor semantics that keep the sign of the divisor.

Built-in functionPython 1.0+Live demo
Common call
hours, minutes = divmod(total_min, 60)
Returns
(quotient, remainder) — a 2-tuple
Replaces
writing `a // b, a % b` twice
Watch out
floor-division sign: divmod(-7, 2) is (-4, 1), not (-3, -1)
divmod(aaThe dividend.type: int | float · required, bbThe divisor. Zero raises ZeroDivisionError.type: int | float · required)
tuple[int, int] | tuple[float, float]

Demo

Live evaluation
Try:
Inputs
afloatdividend
bfloatdivisor
Output
divmod(17, 5)
[3, 2]

divmod(a, b) is equivalent to (a // b, a % b) — one call, one pass. Python uses FLOOR division, so the remainder always has the same sign as the divisor: divmod(-7, 2) is (-4, 1), because -4 * 2 + 1 = -7. This differs from C-style truncation (which would give (-3, -1)). Zero divisor raises ZeroDivisionError exactly as / would.

Parameters

NameTypeRequiredDescription
aint | floatyesThe dividend.
bint | floatyesThe divisor. Zero raises ZeroDivisionError.

Return value

tuple[int, int] | tuple[float, float]A 2-tuple (quotient, remainder). Same values as (a // b, a % b) but computed in one step. Types follow the wider operand.

Common patterns

Time unit breakdown
Cascade divmod to split total seconds into hours / minutes / seconds.
h, rem = divmod(total_seconds, 3600)
m, s   = divmod(rem, 60)
Base conversion
Repeatedly divmod against the target base to extract digits (in reverse).
digits = []
while n:
    n, r = divmod(n, base)
    digits.append(r)
Grid coordinates
Flat index → (row, col) in one call.
row, col = divmod(idx, ncols)

Examples

1. Basic integer
divmod(17, 5)
Returns
(3, 2)
2. Exact division
divmod(20, 5)
Returns
(4, 0)
3. Negative dividend
divmod(-7, 2)
Returns
(-4, 1)
4. Negative divisor
divmod(7, -2)
Returns
(-4, -1)
5. Floats
divmod(3.5, 1.2)
Returns
(2.0, 1.0999999999999996)
6. Zero raises
divmod(10, 0)
Returns
ZeroDivisionError: integer division or modulo by zero

Pitfalls

1. Floor division, not truncation
For negatives, Python rounds the quotient DOWN (toward negative infinity), not toward zero. The remainder follows the sign of the divisor. Programmers coming from C, Java, or JavaScript get bitten regularly.
C-style guess
divmod(-7, 2)   # expected (-3, -1)?
(-4, 1)
Verify identity
-4 * 2 + 1  # == -7 ✓
-7
2. Floats drift
Binary floats cannot represent most decimals exactly; the remainder of a float divmod inherits the drift.
Not quite 0.1
divmod(3.5, 1.2)
(2.0, 1.0999999999999996)
Decimal for exactness
from decimal import Decimal
divmod(Decimal("3.5"), Decimal("1.2"))
(Decimal('2'), Decimal('1.1'))
3. Zero divisor raises
divmod(a, 0) is not (inf, nan) or (0, a) — it raises, exactly like a plain division.
Runtime error
q, r = divmod(count, per_page)
ZeroDivisionError when per_page == 0
Guard
q, r = divmod(count, per_page) if per_page else (0, count)
safe default

When to use

Use it
  • Both quotient and remainder needed together
  • Cascading unit breakdowns (time, base conversion, coordinates)
  • Making the "both at once" intent explicit in the code
Reach for something else
  • Only need one → use // or %
  • Exact decimal remainders → Decimal or math.remainder
  • Complex numbers → not supported

Notes

Complexity
O(1)
Return
A 2-tuple; element types match the wider operand
CPython impl
Objects/longobject.c :: long_divmod / floatobject.c :: float_divmod
Memory
Allocates one small tuple
Thread-safe
Yes — a pure computation

FAQ

Python uses floor division: the quotient is rounded toward negative infinity so the identity `q * b + r == a` holds with `0 <= r < b` (when b is positive). C-style truncation breaks that identity for negatives.

History

1.0
divmod() has been a builtin since Python 1.0.
3.0
Division of two ints returns a float (/), but divmod still returns int quotient + int remainder.