Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

divmod()

For integers, computes the floor quotient and the corresponding remainder as a pair.

Version: Python 3.14; repository image, production runtime not attested

Syntax

divmod(x, y, /)

For integers, computes the floor quotient and the corresponding remainder as a pair.

With a negative dividend the quotient rounds down. For integers, multiplying the quotient by the divisor and adding the remainder recovers the dividend exactly.

For integers the pair consists of x // y and x % y. With −17 and 5 the quotient rounds down to −4, leaving 3. The identity q*y+r = x reconstructs −17 exactly for integers. With floating-point operands the pair consists of floats; rounding can affect both the quotient relative to floor(x/y) and the reconstructed dividend. A nonzero remainder has the sign of y, and zero as divisor raises ZeroDivisionError.

assert divmod(1.0, 0.1)[0] == 9.0
q, r = divmod(-17, 5)
assert (q, r) == (-4, 3) and q * 5 + r == -17
assert divmod(17, -5) == (-4, -3)

Examples

q, r = divmod(-17, 5)
print(q, r)
print(q * 5 + r)

Result

-4 3
-17

For integers the pair consists of x // y and x % y. With −17 and 5 the quotient rounds down to −4, leaving 3. The identity q*y+r = x reconstructs −17 exactly for integers. With floating-point operands the pair consists of floats; rounding can affect both the quotient relative to floor(x/y) and the reconstructed dividend. A nonzero remainder has the sign of y, and zero as divisor raises ZeroDivisionError.

Sources