Python divmod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, divmod(a, b) divides a by b and returns a tuple of two numbers: the quotient a // b and the remainder a % b. It is handy whenever you need both, such as converting seconds into minutes and seconds.

The quotient uses floor division: it rounds toward negative infinity, not toward zero. The remainder therefore always has the same sign as b, and q * b + r == a holds.

divmod() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Unpack the returned tuple into two names.

python
quotient, remainder = divmod(a, b)
CallWhat it does
divmod(17, 5)Returns (3, 2).
divmod(-7, 2)Returns (-4, 1): the quotient is floored.
divmod(7, -2)Returns (-4, -1): remainder takes the sign of b.
divmod(7.5, 2)Returns (3.0, 1.5) for floats.

With ints you get ints; if either argument is a float, both results are floats.

Library declarations
python
divmod(a, b, /)
# same as (a // b, a % b) for int and float

Parameters

ParameterDescription
aRequired. The dividend (the number being divided). An int or float.
bRequired. The divisor. Must not be zero.

Return Value

Return ValueDescription
tuple(a // b, a % b): quotient and remainder.

Python Examples

Example 1 — Convert Seconds into Hours, Minutes, and Seconds

Call divmod() twice to break a duration into clock parts.

python
total_seconds = 3725

minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)

print(f"{total_seconds} seconds = {hours}h {minutes}m {seconds}s")
print(f"{hours:02d}:{minutes:02d}:{seconds:02d}")

How It Works

  1. First split. divmod(3725, 60) returns (62, 5): 62 whole minutes and 5 seconds left over.
  2. Second split. divmod(62, 60) returns (1, 2): 1 hour and 2 minutes.
  3. Tuple unpacking. Each call's pair goes straight into two variables, so no index access is needed.
  4. Clock format. :02d pads each part to two digits, giving 01:02:05.

Example 2 — divmod() with Negative Numbers and Floats

Print the result for four sign and type combinations, and check the rule q * b + r == a.

python
pairs = [(17, 5), (-17, 5), (17, -5), (7.5, 2)]

for a, b in pairs:
    q, r = divmod(a, b)
    print(f"divmod({a}, {b}) = {(q, r)}  check: {q} * {b} + {r} = {q * b + r}")

How It Works

  1. Floored quotient. -17 / 5 is -3.4; flooring goes down to -4, not toward zero.
  2. Remainder follows b. With b = 5 the remainder is 3; with b = -5 it is -3.
  3. The identity holds. Every row's check expression rebuilds the original a from q * b + r.
  4. Floats give floats. divmod(7.5, 2) returns (3.0, 1.5), with the quotient as a whole-number float.

Example 3 — Count Pages and Handle Division by Zero

Use the remainder to decide whether a final, partly filled page is needed.

python
def page_count(items, per_page):
    full_pages, leftover = divmod(items, per_page)
    return full_pages + (1 if leftover else 0)


print(page_count(45, 10))
print(page_count(40, 10))
print(page_count(3, 10))

try:
    divmod(10, 0)
except ZeroDivisionError:
    print("Cannot divide by zero")

How It Works

  1. Full pages first. divmod(45, 10) gives 4 full pages and 5 leftover items.
  2. Leftover means one more page. A non-zero remainder is truthy, so 1 is added: 45 items need 5 pages.
  3. Exact fit. 40 items leave remainder 0, so exactly 4 pages; 3 items still need 1 page.
  4. Zero divisor. divmod(10, 0) raises ZeroDivisionError, just like 10 // 0.

Frequently Asked Questions

A tuple (quotient, remainder), the same as (a // b, a % b). For example divmod(17, 5) returns (3, 2).
Python floors the quotient toward negative infinity, so -3.5 becomes -4. The remainder is then 1, because -4 * 2 + 1 equals -7.
Yes. divmod(7.5, 2) returns (3.0, 1.5). Both parts are floats whenever either argument is a float.
It does one call instead of two, which is slightly faster for big integers and reads more clearly. For small ints the difference is tiny.
ZeroDivisionError, the same as // and %. Check the divisor first or catch the exception.

Next: enumerate()

Loop with a counter: (index, item) pairs.

enumerate() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful