Python 3 reference · Built-in Functions

Python divmod() Function

Divide two numbers and return the quotient and the remainder together as a tuple: (a // b, a % b).

Python 3 Since 3.0

Syntax

divmod(a, b)

Parameters

ParameterDescription
a The dividend.
b The divisor (not zero).

Return value

A tuple (quotient, remainder).

Examples

Quotient and remainderTry it yourself
print(divmod(17, 5))
print(divmod(-17, 5))  # the remainder takes the divisor's sign

Output

(3, 2)
(-4, 3)
Convert seconds to hours, minutes and secondsTry it yourself
seconds = 3725
minutes, secs = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
print(f"{hours}h {minutes}m {secs}s")

Output

1h 2m 5s
How many pages?Try it yourself
items, per_page = 47, 10
pages, rest = divmod(items, per_page)
print(pages + (1 if rest else 0), "pages")

Output

5 pages

Good to know

With negative numbers Python's // rounds down (toward negative infinity), so divmod(-17, 5) is (-4, 3), not (-3, -2) as in C or Java.

Version history

VersionChange
Python 3.0Added