To get the quotient and remainder after division of two numbers in Python, the easiest way is with the Python divmod() function.
print(divmod(10,3))
#Output:
(3, 1)
You can also create your own function and use integer division and the % operator to get the quotient and remainder afster division.
def quo_rem(a,b):
return a // b, a % b
print(quo_rem(10,3))
#Output:
(3, 1)
When performing different calculations with numbers in Python, the ability to easily get certain pieces of information about the calculation can be useful.
One such case is when dividing by two numbers in Python. We can easily get the quotient and remainder after division in Python.
To get the quotient and remainder after division of two numbers in Python, the easiest way is with the Python divmod() function.
The Python divmod() function takes two arguments – the two numbers you want to divide, and returns a tuple with the first element being the quotient and the second element being the remainder.
Below is an example showing you how to use divmod() to get a quotient and remainder from two numbers in Python.
print(divmod(10,3))
#Output:
(3, 1)
You can then verify these are the correct values by multiplying the first returned element by the second parameter and adding the second returned element.
res = divmod(10,3)
print(res[0] * 3 + res[1])
#Output:
10
Getting Quotient and Remainder after Division Using Integer Division and %
You can also write your own function which will calculate the quotient and remainder for you using integer division and the % operator.
With integer division, you will get the quotient and with %, you will get the remainder.
Below is a function which can return the quotient and remainder after the division of two integers with Python.
def quo_rem(a,b):
return a // b, a % b
print(quo_rem(10,3))
#Output:
(3, 1)
Hopefully this article has been useful for you to learn how to get the quotient and remainder after division of two numbers in Python.