Methods and Functions

Python divmod()

In Python, using the in-built python divmod function, you can find the remainder and the quotient of any two numbers given as the input. 

python divmod

Source

 

Example: 

The input x,y is taken from the user, and the output is derived using the divmod()

                    

Input : x = 16, y = 8

Output :(2, 0)



Input : x = 50, y = 5

Output :(10, 0)



Input : x = 67 y = 5

Output :(13, 2)

What is Divmod in Python?

The divmod function in Python returns the quotient and remainder as output as a tuple.

Syntax:

The syntax of the python divmod function is as follows:

divmod(a, b)

divmod() Parameters:

The syntax takes two arguments a, b.

a- is a non-complex integer & the numerator. This argument is the dividend

b- is a non-complex integer & the denominator. This argument is the divisor.

Return Value from divmod()

When we pass a,b as the parameters, the python divmod function returns a / b and a % b as the output.

If a, b are integers, the return value would be: 

(a / b, a % b)

OR

If a, b are float values, the return value would be: 

 (q, a % b), where q denotes the entire quotient.

Example: How does the divmod() work in Python?

                    

# using the divmod() with integer values

print('(6, 5) = ', divmod(6, 5))

print('(10, 5) = ', divmod(10, 5))

print('(17, 17) = ', divmod(11, 11))

print('(15, 13) = ', divmod(15, 13))

# using the divmod() with integer and float values

print('(6.0, 5) = ', divmod((6.0, 5))

print('(3, 9.0) = ', divmod(3, 9.0))

print('(13.5, 6.2) = ', divmod(13.5, 6.2))

print('(1.6, 10.7) = ', divmod((1.6, 10.7))

Output: 
 
(6, 5) = (1, 1)

(10, 5) = (2, 0)

(17, 17) = (1, 0)

(15, 13) = (1, 2)

(6.0, 5) = (2.0, 2.0)

(3, 9.0) = (0.0, 3.0)

(13.5, 6.2) = (3.0, 0.0)

(1.6, 10.7) = (5.0, 0.10000000000000009)

Exceptions:

Consider a,b as the input, 

If a is zero, then the function will return 0,0 as the output. 

If b is zero, then the function will return Zero Division Error as the output. 

What is the Quotient in Python?

When two numbers divide with each other, the result is known as the quotient. 

For example:

                    

Input:

y = 10

z = 7

Output:

Quotient: 1

Remainder 3


Input

y = 88

z = 5

Output:

Quotient: 17

Remainder 3

We can divide two numbers using the ‘//’ operator one can derive the quotient. The remainder is calculated using the ‘%’ operator in Python.

For example:

Method 1:

Source Code:

                    

def find(n, m):

      

    # for quotient

    q = n//m

    print("Quotient: ", q)

      

    # for remainder

    r = n%m

    print("Remainder", r)

      

# Driver Code

find(10, 3)

find(99, 5)



Output:

Quotient: 3

Remainder 1

Quotient: 19

Remainder 4

Method 2: Using the Divmod() method 

Source Code:

                    

q, r = divmod(10, 3)

print("Quotient: ", q)

print("Remainder: ", r)

  

q, r = divmod(99, 5)

print("Quotient: ", q)

print("Remainder: ", r)



q, r = divmod(99, 5)

print("Quotient: ", q)

print("Remainder: ", r)



Output:

Quotient: 3

Remainder 1

Quotient: 19

Remainder 4

Quotient: 19

Remainder 4

What is Modulus in Python?

The modulus operator (%) is used to find the remainder after the division of 2 numbers.

Syntax:

x % y

Here, x is divided by y. The remainder is found using the modulus operator. The operands of this expression can either be integers or float values. 

Source code to find the remainder using the mod operator

                    

a = 13

b = 5

  

# the remainder is stored in c

c = a % b      

print(a, "mod", b, "=",

      c, sep = " ")

  

# inputs

d = 15.0

e = 7.0



f = d % e

print(d, "mod", e, "=", 

      f, sep = " ")



Output:

13 mod 5 = 3

15.0 mod 7.0 = 1.0

We can also use the python divmod() to find both the quotient and the remainder of the integers.

How do you get a Dividend in Python?

The dividend is the number you divide into equal parts. The dividend and the divisor are used to calculate the quotient and the remainders. The dividend can be taken as input from the user. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.