The divmod()
function in Python is a built-in function that takes two numbers as arguments and returns a pair of values. It calculates both the quotient and the remainder when the first number is divided by the second number.
Here's the syntax of the divmod()
function:
divmod(x, y)
The divmod()
function divides x
by y
and returns a tuple containing two values: the quotient and the remainder.
Here's an example to demonstrate the usage of divmod()
:
result = divmod(10, 3)
print(result)
(3, 1)
In the example above, divmod(10, 3)
returns a tuple (3, 1)
. The quotient is 3, and the remainder is 1 when 10 is divided by 3.
The divmod()
function is often used in scenarios where you need to perform division and also want to obtain the remainder in a single operation.