Python Divmod And Its Application

Hello geeks and welcome to today’s article, we will discuss Python Divmod. Along with that, we will also look at its syntax and different parameters. For an overall better understanding, we will also look at a couple of examples. Let us try to get a general overview of Python Divmod. In elementary school, one of the first operations that we learn is Division.

Python divmod is a built-in function of Python (3.9.1). It takes 2 non-complex numbers as input and returns a tuple consisting of quotient and remainder. We can understand it as it takes dividends and divisors from the user. Whereas returns quotient and remainder. As we move, we will look at different parameters associated with it one by one for better understanding.

Syntax Of Python Divmod

Now let us look at its parameter

divmod(x, y)

We can see that function has a relatively simpler syntax and only 2 parameters associated with it. We will try to understand the parameter up next.

Parameters Of Python Divmod

x: int or float
It represents the numerator or the dividend. It must be a non-complex number.

y: int or float
It represents the denominator or the divisor. Like its counterpart, it also must be non-complex in nature.

Return Type Of Python Divmod

After performing the operation it returns the quotient and remainder.

OUT:(q,r)

Another point to pay emphasis on is the data type of input. If both X and Y have data-type “int,” then output has a similar data type. But in case one has data-type “float” and other “int,” in that case, the output is of data-type ‘float.’

Examples Of Python Divmod

Now let us look at some of the basic examples and play with the syntax for a bit.

#input
print(divmod(45,9))
print(divmod(16.0,4.0))
print(divmod(12.0,5))

Output:

(5, 0)
(4.0, 0.0)
(2.0, 2.0)

In the above example, we have used the standard syntax “divmod(x, y).” In the first 2 examples, our divisor and dividend are of the same type. Whereas in the 3rd, both have different data-types one float and the int. Here we can see that our output is in the form of float.

Let us look at one more example

#input
print(divmod(5,0))
print(divmod(0,5))

Output:

ZeroDivisionError: integer division or modulo by zero
(0, 0)

In the one above example, we have tried to analyze how it behaves if either the divisor or dividend is equal to 0. In the first case where the divisor is equal to zero in output, we get an error. While in the second case, where the dividend is zero, we get an output equal to (0,0).

Now let us try to look into its application. We all know about composite numbers. The numbers that have more than 2 factors. We can check it for any number using the Python Divmod function. Also, if a number is not composite, it must be prime unless and until it is “1”.

Example 1:

n = 18
x = n 
count = 0
while x != 0: 
    p, q = divmod(n, x) 
    x-= 1
    if q == 0: 
        count+= 1
if count>2: 
    print('composite') 
else: 
    print('Prime')

Example 2:

n = 19
x = n
count = 0
while x != 0:
p, q = divmod(n, x)
x-= 1
if q == 0:
count+= 1
if count>2:
print('composite')
else:
print('Prime')
#output
composite
Prime

Explanation:

In the above example, we have used the Divmod function to determine whether the number is composite or prime. This can be used for any number and makes our work a lot easier. Here we have defined a counter =0 and then compared that counter with 2. If greater than 2, then “composite” else “prime.”

Can you use Python Divmod for Negative Numbers?

Yes, you can absolutely use it on negative numbers. a//b will be a negative number if a is negative. Moreover, (a//b)*b will result in a negative step after division. The following example will help you understand –

print('divmod(-8, 5) = ', divmod(-8, 5))
divmod(-8, 5) =  (-2, 2)

Must Read

Conclusion

In this article, we covered Python Divmod. For a better understanding, we looked at its syntax, parameters. We looked at a couple of examples and also looked at their application. In the end, we conclude that Python Divmod is a built-in feature that returns the quotient and remainder for a non-complex input. I hope this article was able to clear all of your doubts. In case if you any doubt feel free to write them below in the comment section. Done reading this, why not read NumPy Digitize next.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments