Python divmod() Function
Returns the quotient and the remainder when the first parameter (the dividend) is divided by the second parameter (the divisor).
Syntax
Python
Copy Code
divmod(x, y)
Parameters
Parameter | Description |
---|---|
x |
Required. The dividend (the 'x' in x/y) |
y |
Required. The divisor (the 'y' in x/y) |
Example
Python
Copy Code
# 10 / 2 = 5 with no remainder print(f'10 / 2 = {10 / 2}') qr = divmod(10, 2) print(f'Quotient and remainder of 10 / 2 = {qr}') # 10 / 4 = 2.5 The quotient is 2 and the remainder 2, as is two-fourth's. print(f'10 / 4 = {10 / 4}') qr = divmod(10, 4) print(f'Quotient and remainder of 10 / 4 = {qr}')
Output
10 / 2 = 5.0 Quotient and remainder of 10 / 2 = (5, 0) 10 / 4 = 2.5 Quotient and remainder of 10 / 4 = (2, 2)
Notes
You can access the quotient and remainder using an index.
Example:Python
Copy Code
qr = divmod(12, 7) print(qr) print(f'Quotient = {qr[0]}') print(f'Remainder = {qr[1]}')Output:
(1, 5) Quotient = 1 Remainder = 5