Python built-in Method - divmod()
The divmod()
method is a built-in function in Python that takes two numbers as arguments and returns a tuple consisting of their quotient and remainder when using integer division.
Here is the syntax for divmod()
method:
divmod(a, b)
where a
and b
are the two numbers to divide.
The divmod()
function returns a tuple consisting of two values: the quotient of a
divided by b
, and the remainder of that division.
Here is an example of how to use divmod()
:
result = divmod(10, 3) print(result)
In this example, we call divmod()
with the arguments 10
and 3
. The output of the function is a tuple with the values (3, 1)
. The value 3
is the quotient when 10
is divided by 3
, and the value 1
is the remainder.
You can also use the tuple unpacking technique to assign the values of the quotient and remainder to separate variables. Here's an example:
quotient, remainder = divmod(10, 3) print(quotient) print(remainder)
In this example, we use tuple unpacking to assign the values returned by divmod()
to two variables, quotient
and remainder
. The output of the code is:
3 1
This technique can be useful when you need to perform operations on the quotient and remainder separately.