Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
divmod() in Python and its application
The divmod() function is part of Python's standard library which takes two numbers as parameters and returns the quotient and remainder of their division as a tuple. It is useful in many mathematical applications like checking for divisibility of numbers and establishing if a number is prime or not.
Syntax
divmod(a, b)
Parameters:
-
a? The dividend (number to be divided) -
b? The divisor (number that divides a) - Both
aandbcan be integers or floats
Return Value: Returns a tuple (quotient, remainder)
Examples with Integers and Floats
In the below example, see the cases of both integers and floats. The divmod() function returns a tuple containing the quotient and remainder ?
# with integers
print("5 and 2 give:", divmod(5, 2))
print("25 and 5 give:", divmod(25, 5))
# with floats
print("5.6 and 2 give:", divmod(5.6, 2))
print("11.3 and 9.2 give:", divmod(11.3, 9.2))
5 and 2 give: (2, 1) 25 and 5 give: (5, 0) 5.6 and 2 give: (2.0, 1.5999999999999996) 11.3 and 9.2 give: (1.0, 2.1000000000000014)
Using Zero as Arguments
If the first argument is zero, then we get (0, 0). If the second argument is zero, then we get a ZeroDivisionError as expected ?
# With first argument as zero
print("0 and 8 give:", divmod(0, 8))
# With second argument as zero (this will raise an error)
try:
print("8 and 0 give:", divmod(8, 0))
except ZeroDivisionError as e:
print("ZeroDivisionError:", e)
0 and 8 give: (0, 0) ZeroDivisionError: integer division or modulo by zero
Checking Divisibility
If the second value (remainder) of the tuple is 0, then the first number is divisible by the second. Otherwise, it is not divisible ?
m = 12
n = 4
quotient, remainder = divmod(m, n)
print("Quotient:", quotient)
print("Remainder:", remainder)
if remainder == 0:
print(f"{m} is divisible by {n}")
else:
print(f"{m} is not divisible by {n}")
Quotient: 3 Remainder: 0 12 is divisible by 4
Checking if a Number is Prime
We can use divmod() to check if a number is prime by counting how many divisors it has. A prime number should only be divisible by 1 and itself ?
def is_prime(num):
if num < 2:
return False
# Count divisors
divisor_count = 0
for i in range(1, num + 1):
quotient, remainder = divmod(num, i)
if remainder == 0:
divisor_count += 1
# A prime number has exactly 2 divisors: 1 and itself
return divisor_count == 2
# Test with different numbers
test_numbers = [11, 15, 17, 20]
for number in test_numbers:
if is_prime(number):
print(f"{number} is Prime")
else:
print(f"{number} is not Prime")
11 is Prime 15 is not Prime 17 is Prime 20 is not Prime
Comparison with Other Division Methods
| Method | Returns | Example |
|---|---|---|
/ |
Float quotient | 7 / 3 = 2.333... |
// |
Integer quotient | 7 // 3 = 2 |
% |
Remainder | 7 % 3 = 1 |
divmod() |
Both quotient and remainder | divmod(7, 3) = (2, 1) |
Conclusion
The divmod() function is efficient when you need both quotient and remainder from division. It's particularly useful for mathematical operations, divisibility checks, and number theory applications like prime checking.
