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.

In this tutorial, you will learn the syntax of divmod() function, and then its usage with the help of example programs.

Syntax

The syntax of divmod() function is

divmod(x, y)

where

Parameter Description
x A number, but not complex number.
y A number, but not complex number.

Examples

1. 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 Code

Output

Quotient : 3
Remainder : 2

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 Code

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.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us