Python Glossary Русский

all Python 3Arithmetic & operatorsfunction

divmod()

numbers

Built-in function that returns the tuple (quotient, remainder) — the results of floor division and of the modulo. More efficient than two separate operations.

Syntax

divmod(a, b) -> (a // b, a % b)

The surprise is negative operands: division floors toward minus infinity, so divmod(-7, 2) is (-4, 1), not (-3, -1) as in C or Java, and the remainder always carries the sign of the divisor. Reach for divmod when you genuinely need both parts at once (seconds to hours and minutes, paging); for a single value plain // or % reads better.

Examples

print(divmod(10, 3))  # → (3, 1)
h, m = divmod(150, 60)
print(f"{h} ч {m} мин")  # → 2 ч 30 мин
pages, rem = divmod(105, 10)
print(f"{pages} страниц, остаток {rem}")  # → 10 страниц, остаток 5
q, r = divmod(17, 5)
print(f"17 = 5 * {q} + {r}")  # → 17 = 5 * 3 + 2
print(divmod(-7, 2))  # → (-4, 1) (знак остатка — как у делителя)
print(divmod(3.5, 1.5))  # → (2.0, 0.5) (работает и с float)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy