divmod()
divmod(a, b) Returns:
tuple · Added in v3.0 · Updated March 13, 2026 · Built-in Functions built-in arithmetic division
The divmod() function takes two numbers (not complex numbers) as arguments and returns a tuple containing the quotient and remainder from integer division. It is equivalent to (a // b, a % b) but computes both values in a single operation, which can be more efficient.
Syntax
divmod(a, b)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
a | int | float | — | The dividend. Position-only. |
b | int | float | — | The divisor. Position-only. Cannot be zero. |
Returns: A tuple of two numbers (quotient, remainder).
Examples
Basic usage with integers
# divmod returns (quotient, remainder)
result = divmod(10, 3)
print(result)
# (3, 1)
# Equivalent to:
result = (10 // 3, 10 % 3)
print(result)
# (3, 1)
Working with floats
# Works with floats too
result = divmod(7.5, 2.5)
print(result)
# (3.0, 0.0)
# When divisor is float, result is float
result = divmod(10, 3.5)
print(result)
# (2.0, 3.0)
Negative numbers
# Negative dividend
result = divmod(-10, 3)
print(result)
# (-4, 2)
# Negative divisor
result = divmod(10, -3)
print(result)
# (-4, -2)
# Both negative
result = divmod(-10, -3)
print(result)
# (3, -1)
Common Patterns
Batch processing with remainders
def distribute_items(total, per_box):
"""Distribute items into boxes, return (full_boxes, remainingItems)."""
return divmod(total, per_box)
full_boxes, leftover = distribute_items(100, 8)
print(f"Full boxes: {full_boxes}, Remaining: {leftover}")
# Full boxes: 12, Remaining: 4
Pagination calculation
def calculate_pages(total_items, items_per_page):
"""Calculate total pages and items on last page."""
return divmod(total_items, items_per_page)
total_pages, last_page_items = calculate_pages(127, 10)
print(f"Total pages: {total_pages + (1 if last_page_items else 0)}")
print(f"Items on last page: {last_page_items or items_per_page}")
# Total pages: 13
# Items on last page: 7
Time conversion
def seconds_to_hours_minutes_seconds(seconds):
"""Convert seconds to hours, minutes, and remaining seconds."""
hours, remainder = divmod(seconds, 3600)
minutes, secs = divmod(remainder, 60)
return hours, minutes, secs
h, m, s = seconds_to_hours_minutes_seconds(3665)
print(f"{h}h {m}m {s}s")
# 1h 1m 5s
Errors
ZeroDivisionError
# Division by zero raises ZeroDivisionError
divmod(10, 0)
# ZeroDivisionError: integer division or modulo by zero
TypeError with complex numbers
# Complex numbers are not supported
divmod(5, 2+1j)
# TypeError: can't take divmod of complex numbers
See Also
- built-in::pow — Power function
- built-in::round — Round numbers