Python divmod() function

Hey guys, in this tutorial, we are going to learn how we can use Python divmod() function in our programs to find the quotient and remainder of a division operation. Go through this tutorial closely to understand the functioning of divmod() method.

Python divmod() function: Syntax and working

The syntax for divmod() in python is given here:

divmod(x, y)

In the above statement, x is numerator and y is the denominator.

This function returns a tuple with a pair of values consisting of the quotient and the remainder. So divmod(x, y) will give the output as– (x//y, x%y)

If both x and y are integers then the output tuple will contain integers. If either x or y is a float then the output tuple will contain floating-point numbers.


divmod(23, 4):

The output of the above statement would be: (23//4, 23% 4). That is  (5, 3).


The above statement will give the output as: (7//2.0, 7%2.0). That is (3.0, 1.0)

divmod(12.0, 6):

The above statement will give the output as: (12.0//6, 12.0%6). That is (2.0, 0.0)

divmod(12.0, 2.0):

The output for the above will be as given here: (12.0//2.0, 12.0%2.0). That is (6.0, 0.0)

The code implementation of the above has been given below. Go through it to understand the concept of divmod() function in python in a better way.

Below is our program to explain the working of divmod() function:-

# divmod() function with integers
print('divmod(23, 5) : ', divmod(23, 5))
print('divmod(5, 23) : ', divmod(5, 23))

# divmod()function with floats
print('divmod(7, 2.0) = ', divmod(7, 2.0))
print('divmod(12.0, 6) = ', divmod(12.0, 6))
print('divmod(12.0, 2.0) = ', divmod(12.0, 2.0))

The output of the program will be:

divmod(23, 5) : (4, 3)
divmod(5, 23) : (0, 5)
divmod(7, 2.0) = (3.0, 1.0)
divmod(12.0, 6) = (2.0, 0.0)
divmod(12.0, 2.0) = (6.0, 0.0)

As you can observe in the output, the divmod() function returns a tuple of the quotient and remainder values of division operation for the passed values of parameters.

Thank you.

Also, read: Tuples in Python with examples

Leave a Reply

Your email address will not be published. Required fields are marked *