The built-in divmod()
function takes two numbers as arguments and returns a tuple containing the quotient and remainder from the integer division of the input numbers:
divmod()
Signature Arguments Argument Description a
The dividend b
The divisor Return Value
(a // b, a % b)
.(q, a % b)
, where q
is usually math.floor(a / b)
, but it may be lower by 1
.divmod()
Examples
With integers as arguments:
With floating-point numbers as arguments:
divmod()
Common Use Cases
The most common use cases for the divmod()
function include:
divmod()
Real-World Example
Suppose you need to convert a time value in milliseconds to a string formatted as "hh:mm:ss"
. You can achieve this using the divmod()
function:
In this example, divmod()
helps you break down the time into hours, minutes, and seconds so that you can generate the desired output string.
Tutorial
Python Modulo in Practice: How to Use the % OperatorIn this tutorial, you'll learn about the Python modulo operator (%). You'll look at the mathematical concepts behind the modulo operation and how the modulo operator is used with Python's numeric types. You'll also see ways to use the modulo operator in your own code.
For additional information on related topics, take a look at the following resources:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4