In this example, we will use the divmod() function on the integers 2,3,12 and 32. Following that, we use the divmod() technique on the float values. These are 4.8, 4, 10.1, and 7.4, respectively. They provide us with a tuple as a result of using divmod(), which can contain integers and float values.
print("12 and 32 give:",divmod(12,32))
print("4.8 and 4 give:",divmod(5.6,2))
print("10.1 and 7.4 give:",divmod(11.3,9.2))
When we execute the code above, we get the following result.
We are going to use zero in this case. Remember that if the first argument is zero, the result is (0,0). And, as expected, if the second input is zero, we receive a Zerodivision error. As you can see in the first line of code, the first argument is zero, and the second argument is six. The (0.0) result is obtained when the divmod() method is used in these inputs, as seen in the output screenshot. Pay attention to the second line of code; you’ll notice that the first input is 6, and the second is 0. The ZeroDivisionError is thrown by these input parameters.
print("6 and 0 give:",divmod(6,0))
We get the following output when we execute the code written above.
Now we will look into divisibility. We say the first integer is divisible by the second if the next value of the tuple afterwards division is 0. It isn’t divisible if it isn’t. This is demonstrated in the example below. As you can see, we have set the values of two variables, ‘a’ and ‘b,’ to 8 and 2. The divmod() method was then applied to these variables. The 2nd value of the tuple after division is 0, as seen in the output screenshot. It signifies that the first and second numbers are divisible by each other.
b = 2
quotient,remainder = divmod(a,b)
print(a,' is divisible by ',b)
print(a,' is not divisible by ',b)
Here is the output of the code of our third Python program.
In the second to final example, we will see if the integer is prime. When we begin distributing a number by each number initializing with itself until 1, we can use divmod() to keep track of the reminders it creates. Because no integer other than itself divides a prime number correctly, the count of zero remainders is only one. The number is not prime if the total of zero remainders is larger than one.
x = val
count = 0
while x != 0:
q, remainder = divmod(val, x)
x -= 1
if remainder == 0:
count += 1
if count > 2:
print(val, 'is not Prime')
print(val, 'is Prime')
Here is the result of the code to check if the integer is prime is mentioned below.
We will utilize the divmod() method on negative values in our final example. For negative input arguments val1, val2, or both, use divmod(val1, val2). If both parameters are integers, Python divides the first element of the returned tuple using integer division val1 / val2 and val1% val2 to get the second element. Negative val1 or val2 inputs are allowed in both operations. The tuple (a, b) that is returned is calculated so that a * val2 + b = val1. The example Python program code for two scenarios is shown below, where the input is -20, -2, -20, and 2.
We receive the following result when we run the Python code mentioned above.
In this post, we have gone over all of the features and the working of the Python divmod() function and some interesting uses. The divmod() method in Python takes two values as a parameter list and divides and modulates them. The divmod() function as a pair returns the quotient and remainder. When a float value is specified to the function, it yields the pair of quotient and remainder. It performs this by deleting the decimal component from the subsequent values. If the second argument provided to the divmod() method is zero, a ZeroDivisionError is thrown. If a complex integer is supplied as an argument to the function, it throws a TypeError exception. As a result of this guide, we now know how the Python divmod() function works.