Python

# Python Divmod

Almost all programming tasks need the use of mathematical operations. When addressing a problem, we often need to execute simple arithmetic operations, and Python provides several functions to help us accomplish these operations more efficiently. The divmod() function, for example, accepts two non-complex numbers as parameters and returns a pair of numbers containing their quotient and remainder. The divmod() function has the syntax divmod(val1,val2). The divmod() method accepts two parameters: ‘val1’ is a non-complex number (numerator or dividend) and ‘val2’ is a non-complex number (numerator or dividend) (denominator or divisor). The divmod() method, when executed, returns a tuple. It is significant to note that it contains the quotient and the remainder. If ‘val1’ and ‘val2’ are both integers, the divmod() return value is (val1 / val2, val1%val2). If either ‘val1’ or ‘val2’ is a float value, the function’s result is (q, val1 percent val2), where q indicates the entire quotient and is generally similar to math.floor(val1/ val2), but may be one less. In the example below, both integers and floats are used.

## Example 1:

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("2 and 3 give:",divmod(2,3))
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. ## Example 2:

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("0 and 6 give:",divmod(0,6))
print("6 and 0 give:",divmod(6,0)) We get the following output when we execute the code written above. ## Example 3:

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.

a = 8
b = 2
quotient,remainder = divmod(a,b)
print(quotient)
print(remainder)
if (remainder==0):
print(a,' is divisible by ',b)
else:
print(a,' is not divisible by ',b) Here is the output of the code of our third Python program. ## Example 4:

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.

val = 9
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')
else:
print(val, 'is Prime') Here is the result of the code to check if the integer is prime is mentioned below. ## Example 5:

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.

print(divmod(-20, -2))
print(divmod(-20, 2)) We receive the following result when we run the Python code mentioned above. ## Conclusion:

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. 