In this short snippet post we're going to briefly talk about one of Python's less well-known built in functions: divmod. It's a quick and easy way to perform Euclidean division with a single operation. Stick around to the end of the post, as we're going to show you how to use divmod to turn a time in second into hours, minutes, and seconds for easier reading.

We've talked about Euclidean division on the blog a few times, most notably in our post on the modulo and floor division operators. It's the kind of division we do in school before we learn about decimals and fractions.

Instead of performing 5 / 2 and getting 2.5, we say that 2 goes into 5 two times, and we have 1 left over. Usually this is written as 2 remainder 1, or 2r1.

If we plug these numbers into divmod, this is exactly what we get:

euclidean_divison = divmod(5, 2)
print(euclidean_divison)  # (2, 1)

divmod returns a tuple, where the first value is the quotient (the whole number result), and the second value is the remainder. Since we have this strict ordering of the results, we can destructure the tuple if we need to use both parts independently:

quotient, remainder = divmod(5, 2)

One thing to keep in mind is that negative numbers might produce some unexpected results, and I'd recommend reviewing the modulo and floor division post for a detailed explanation as to why this occurs.

One situation we haven't mentioned is floats. It's perfectly possible to use floating point numbers with divmod, and both numbers in the resulting tuple will also be floats:

print(divmod(5.5, 2))  # (2.0, 1.5)
print(divmod(6.0, 2))  # (3.0, 0.0)

A time converter

As I mentioned at the start of this post, we can use divmod to turn a large seconds value like 8594 into hours, minutes, and seconds, which is much easier for normal humans to parse. We can use the same method for other units as well, such as feet and inches.

The way we're going to tackle this problem is by taking our initial seconds value, which we're going to call raw_time and first breaking it up into minutes and seconds. We're going to do this by using divmod to perform a Euclidean division, where the raw_time is divided by 60. We can then destructure the resulting tuple into a minutes value and seconds value:

raw_time = 8594
minutes, seconds = divmod(raw_time, 60)  # (143, 14)

Now that we have a value for minutes we can perform the same operation, this time using minutes instead of raw_time. This will yield a tuple containing the hours and minutes, which we can destructure just like before:

Once we're done, we can just print some message to our users, letting them know the result of the conversion:

raw_time = 8594

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

print(f"{raw_time}s is {hours}h {minutes}m {seconds}s")
# 8594s is 2h 23m 14s

Wrapping up

That's it for today! divmod isn't a function that you'll use often, but if you need the quotient and remainder for some operation, it's a great deal cleaner than using the floor division and modulo operators independently.

If you're new to Python or you're just looking to improve your Python knowledge, you might be interested in our Complete Python Course. You might also want to subscribe to our mailing list below, as we'll be sharing discount codes with our subscribers each month.

We also recently did a free 6 hour live stream where we started from absolute basics and worked all the way up to object oriented programming! If that sounds like something you're interested in, you can check it out at the link below: