divmod() Builtin Function

Python – divmod()

Python divmod() builtin function takes two numeric values (non-complex) as arguments, and returns the pair of quotient and remainder of their integer division.

Syntax

The syntax of divmod() function is

divmod(x, y)

where

  • x, y are numeric values, but not complex numbers.

Examples

Find Quotient and Remainder of x/y

In the following program, we take two integer numbers: x, y; and find the quotient and remainder of their division x/y, using divmod() function.

Python Program

x = 17
y = 5

quotient, remainder = divmod(x, y)

print(f'Quotient : {quotient}')
print(f'Remainder : {remainder}')
Run

Output

Quotient : 3
Remainder : 2

divmod() with divisor as zero

If the divisor is zero, which the second argument to divmod(), then divmod() raises ZeroDivisionError.

Python Program

x = 17
y = 0

quotient, remainder = divmod(x, y)

print(f'Quotient : {quotient}')
print(f'Remainder : {remainder}')
Run

Output

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    quotient, remainder = divmod(x, y)
ZeroDivisionError: integer division or modulo by zero

Summary

In this tutorial of Python Examples, we learned the syntax of divmod() builtin function, and how to find the quotient and remainder of the division, using divmod() function with examples.