Python divmod() Method
In this tutorial we will learn about the python divmod() method and how to use it.
Python divmod() Method
##
What is Python divmod() Method?
The divmod() method takes two numbers and returns a pair of numbers inside a tuple consisting of their quotient and remainder.
The syntax of divmod() is:
1
2
divmod(x,y)
divmod() Parameters
divmod() takes two parameters:
x - a non-complex number that can be referred to as a numerator.
y - a non-complex number that can be referred to as denominator
Let see an example of divmod() method.
Example: How divmod() works in python?
1
2
3
4
5
6
7
8
9
print('divmod(1, 3) = ', divmod(1, 3))
print('divmod(5, 8) = ', divmod(5, 8))
print('divmod(4, 4) = ', divmod(4, 4))
# divmod() with Floats
print('divmod(4.0, 3) = ', divmod(4.0, 3))
print('divmod(7, 9.0) = ', divmod(7, 9.0))
print('divmod(2.6, 0.5) = ', divmod(2.6, 0.5))
The output will be as follows.
1
2
3
4
5
6
7
divmod(1, 3) = (0, 1)
divmod(5, 8) = (0, 5)
divmod(4, 4) = (1, 0)
divmod(4.0, 3) = (1.0, 1.0)
divmod(7, 9.0) = (0.0, 7.0)
divmod(2.6, 0.5) = (5.0, 0.10000000000000009)
Rules of divmod()
(q, r) - a pair of numbers will be a tuple consisting of quotient q and a remainder r.
If x and y are integers, the return values from divmod() is same as (a //b , x % y).
This post is licensed under CC BY 4.0 by the author.