Python divmod()

Python divmod() builtin function takes two numbers as arguments, computes division operation, and returns the quotient and reminder.

In this tutorial, we will learn about the syntax of Python divmod() function, and learn how to use this function with the help of examples.

Syntax

The syntax of divmod() function is

divmod(a, b)

where

ParameterRequired/OptionalDescription
aRequiredAn integer/float value.
bRequiredAn integer/float value.

Note: a or b cannot be complex numbers.

Returns

The function returns two numbers of type integer/float based on the arguments.

Example – divmod(22, 4)

In this example, we will find the quotient and reminder for the dividend 22 and divisor 4.

Python Program

a = 22
b = 4
q, r = divmod(a, b)
print(f'Quotient is {q}')
print(f'Reminder is {r}')
Try Online

Output

Quotient is 5
Reminder is 2

Example – divmod(22, 0) – ZeroDivisionError

In this example, we will find the quotient and reminder for the dividend 22 and divisor 0. Since, the divisor is zero, divmod() throws ZeroDivisionError.

Python Program

a = 22
b = 0
q, r = divmod(a, b)
print(f'Quotient is {q}')
print(f'Reminder is {r}')
Try Online

Output

Traceback (most recent call last):
  File "d:/workspace/python/example.py", line 3, in <module>
    q, r = divmod(a, b)
ZeroDivisionError: integer division or modulo by zero

Example – divmod() with Floating Point Number

In this example, we will find the quotient and reminder for the dividend 2.2 and divisor 0.4.

Python Program

a = 2.2
b = 0.4
q, r = divmod(a, b)
print(f'Quotient is {q}')
print(f'Reminder is {r}')
Try Online

Output

Quotient is 5.0
Reminder is 0.20000000000000007

Conclusion

In this Python Tutorial, we have learnt the syntax of Python divmod() builtin function, and also learned how to use this function, with the help of Python example programs.