Python divmod()

In this post, we will see about Python divmod function.
divmod(a, b) returns pair of numbers consisting of their quotient and remainder when using integer division.



It returns

  • In case of integer arguements, it returns (a // b, a % b)
  • In case of float arguements, it returns (q, a % b), where q is usually math.floor(a / b)


Let’s understand with help of simple example:

divmod in case of int

a=10 b=3 x,y=divmod(a,b) print("Quotient for 10/3: ",x) print("Remainder for 10%3 :",y)


Quotient for 10/3: 3
Remainder 10%3 : 1

divmod in case of flaot

You need to implement __index__() method which returns an int in case of user defined class.We are simply returning 9 from below __index__() and when you call bin(country), it internally executes __index__() of country class only.

a=13.4 b=3.2 x,y=divmod(a,b) print("Quotient for 13.4/3.2: ",x) print("Remainder for 13.4%3.2 :",y)


Quotient for 13.4/3.2: 4.0
Remainder for 13.4%3.2 : 0.5999999999999996

That’s all about Python divmod function.

Leave a Reply

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