22
Python divmod()
ItsMyCode |
The
divmod()
in Python is a built-in function that takes two numbers as input parameters and returns a pair of numbers ( a tuple ) consisting of their quotient and remainder.The syntax of
divmod()
is:**divmod(_dividend_, _divisor_)**
divmod() takes two parameters as input
divmod()
returns a pair of numbers (a tuple) consisting of quotient and remainder.If
x
and y
are integers, the return value from divmod()
is (x / y, x % y)
If
x
or y
is a float, the result is*(q, x % y)
*, where q
is the whole part of the quotient.# Python3 code to demonstrate divmod()
# divmod() on integers
print('divmod(4, 2) = ', divmod(4, 2))
print('divmod(5, 3) = ', divmod(5, 3))
print('divmod(6, 6) = ', divmod(6, 6))
# divmod() with Floats
print('divmod(4.0, 2) = ', divmod(4.0, 2))
print('divmod(5.5, 3.3) = ', divmod(5.5, 3.3))
print('divmod(6.5, 6.5) = ', divmod(6.5, 6.5))
Output
divmod(4, 2) = (2, 0)
divmod(5, 3) = (1, 2)
divmod(6, 6) = (1, 0)
divmod(4.0, 2) = (2.0, 0.0)
divmod(5.5, 3.3) = (1.0, 2.2)
divmod(6.5, 6.5) = (1.0, 0.0)
(q, x % y)
. Here, q is the whole part of the quotient.The post Python divmod() appeared first on ItsMyCode.
22