Get Quotient And Remainder In One Operation

May 5, 2026 ยท View on GitHub

While writing some custom code to transform a number of seconds into the constituent hours, minutes, and seconds, I found myself needing to get both the quotient and remainder from a division between two numbers.

>>> import math
>>> math.floor(3666 / 3600)
1
>>> 3666 % 3600
66

Instead, I can use Python's built-in divmod function to compute both values in one statement.

>>> divmod(3666, 3600)
(1, 66)

The result is a tuple with the first value being my quotient (in this case, the number of hours) and the remainder (the remaining number of seconds).

This kind of operation is known as Euclidian Division.

Here is a snippet of some actual code where I use this in py-vmt:

def format_time_delta(diff: timedelta) -> str:
    total_seconds = int(diff.total_seconds())

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

    # ...