The divmod()
function in Python returns a tuple containing the quotient and the remainder when dividing two numbers. It takes two arguments, the dividend and the divisor, and performs integer division to calculate the quotient, and returns the quotient and the remainder as a tuple.
Parameter Values
Parameter | Description |
---|---|
a | The number to be divided. |
b | The number to divide by. |
Return Values
The divmod()
function returns a tuple (int
, int
) for integers or (float
, float
) for floats.
How to Use divmod()
in Python
Example 1:
The divmod()
function returns the quotient and the remainder when dividing two numbers.
result = divmod(20, 6)
print(result) # Output: (3, 2)
Example 2:
It can be used to perform integer division and obtain the remainder in one operation.
num1 = 35
num2 = 4
quotient, remainder = divmod(num1, num2)
print(quotient) # Output: 8
print(remainder) # Output: 3
Example 3:
The divmod()
function works with negative numbers as well.
result = divmod(-15, 4)
print(result) # Output: (-4, 1)