Python divmod Function: Quotient and Remainder Together
Learn how Python's built-in divmod function returns both the quotient and the remainder in one call, with practical examples, edge cases, and performance notes.

Python's built-in divmod() function returns a tuple containing the quotient and remainder of a division operation. It combines the functionality of the floor division operator // and the modulo operator % into a single call. This article explains how divmod() behaves with different numeric types, where it is useful in real code, and when you might prefer to use // and % separately.
Syntax and Return Value
divmod(a, b) takes two numbers and returns a tuple (a // b, a % b). For integer inputs, both elements are integers. For floating-point inputs, the quotient is the floor of the division and the remainder is a float.
result = divmod(17, 5)
print(result) # (3, 2)The first element is the floor quotient, and the second is the modulo remainder. For positive inputs, this means the first value is the number of times b fits entirely into a, and the second is what remains. This is equivalent to writing (a // b, a % b), but with a single function call.
How divmod Handles Different Numeric Types
divmod works with integers and floats. With integers, the result is always a tuple of two integers. With floats, the quotient is the floor of the division, and the remainder is a float that satisfies the relationship a = b * quotient + remainder.
print(divmod(17.5, 3.2)) # (5.0, 1.5)Here, 5.0 * 3.2 + 1.5 = 17.5. Note that floating-point arithmetic may introduce tiny rounding errors, so the remainder may not be exact in the mathematical sense. divmod does not support complex numbers; passing a complex argument raises a TypeError.
Practical Use Cases: Converting Units
A common use of divmod is converting a value expressed in a smaller unit into a larger unit plus a remainder. For example, converting seconds to minutes and seconds:
total_seconds = 137
time = divmod(total_seconds, 60)
print(f"{time[0]} minutes and {time[1]} seconds") # 2 minutes and 17 secondsThe same pattern applies to converting cents to dollars, bytes to kilobytes, or any base-unit conversion where you need both the whole part and the leftover.
Using divmod in Loops and Iteration
divmod is useful when you need to repeatedly extract digits from a number. For instance, converting a decimal integer to another base such as binary:
def to_base(n, base):
digits = []
while n > 0:
n, remainder = divmod(n, base)
digits.append(str(remainder))
return ''.join(reversed(digits))
print(to_base(42, 2)) # '101010'In each iteration, divmod gives both the next quotient and the digit to record, avoiding two separate operations and making the loop body clearer. This simple version works for positive inputs and for bases up to 10; larger bases require symbols for digit values above 9.
Edge Cases and Common Mistakes
divmod follows Python's floor division semantics, which means the remainder has the same sign as the divisor. This is important when dealing with negative numbers.
print(divmod(-7, 3)) # (-3, 2)Here, -7 // 3 is -3 and -7 % 3 is 2, because -3 * 3 + 2 = -7. This behavior is consistent with the % operator, but it may surprise developers coming from languages where modulo follows the sign of the dividend.
Dividing by zero raises a ZeroDivisionError, just like // and %:
try:
divmod(10, 0)
except ZeroDivisionError:
print("Cannot divide by zero")When using floats, be aware that the remainder is subject to floating-point precision limits. For exact rational arithmetic, consider using the fractions module or integer-based math.
Performance and Maintainability Considerations
divmod computes both the quotient and the remainder in a single operation. In CPython, this can be slightly more efficient than calling // and % separately because the division is performed once internally. However, the difference is usually negligible for typical workloads. The larger benefit is code clarity: when you need both values, divmod expresses the intent directly and avoids accidental divergence if you later change the divisor.
If you only need one of the two results, using the dedicated operator is clearer and avoids constructing a tuple. For example, a // b is more readable than divmod(a, b)[0]. Reserve divmod for cases where both values are immediately used.
Compatibility and Alternatives
divmod() is available in Python 3 and remains a standard built-in. It works with int and float arguments, and also with objects that implement the __divmod__ special method, allowing custom numeric types to define their own behavior.
An alternative is to use // and % separately, which gives you more flexibility if you need to handle the quotient and remainder independently. For example, you might want to check the remainder before using the quotient, or you might need to apply different rounding logic. In those cases, separate operators are more appropriate.
For most situations where both results are needed together, divmod is the idiomatic choice. It reduces repetition and makes the relationship between the quotient and remainder explicit, which improves readability and maintainability over time.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.