📅  最后修改于: 2023-12-03 15:00:26.115000             🧑  作者: Mango
In Python, divmod()
is a built-in function that takes two numbers as arguments and returns the tuple (a, b) containing the quotient and remainder when the first argument is divided by the second. In other words, the result of the expression divmod(a, b)
is (a // b, a % b)
.
divmod(a, b)
a
: the numeratorb
: the denominatorThe return value of divmod()
is a tuple (a // b, a % b)
, where a // b
is the quotient and a % b
is the remainder.
>>> divmod(10, 3)
(3, 1)
>>> divmod(7, 2)
(3, 1)
>>> divmod(18, 4)
(4, 2)
divmod()
function can be used to perform integer division and obtain the remainder in a single step.divmod()
function can also be used to implement a custom __divmod__()
method for a class, allowing instances of the class to be used with the built-in divmod()
function.In conclusion, divmod()
is a useful and versatile function in Python that can simplify certain arithmetic operations and implementation details.