divmod() in Python and its application
Last Updated :
29 Nov, 2023
In Python, divmod() method takes two numbers and returns a pair of numbers consisting of their quotient and remainder. In this article, we will see about divmod() function in Python and its application.
Python divmod() Function Syntax
divmod(x, y)
x and y : x is numerator and y is denominator
x and y must be non complex
What is the divmod() function in Python?
In Python, divmod()
is a built-in function that takes two numbers as arguments and returns a tuple containing the quotient and the remainder of the division operation.
divmod() in Python Working and Examples
Example: The divmod() method takes two parameters x and y, where x is treated as the numerator and y is treated as the denominator. The method calculates both x // y and x % y and returns both the values.
Input : x = 9, y = 3
Output :(3, 0)
Input : x = 8, y = 3
Output :(2, 2)
Explanation
- If x and y are integers, the return value is
(x // y, x % y)
- If x or y is a float, the result is
(q, x % y), where q is the whole part of the quotient.
In this example, we are using divmod()
function, which returns a tuple containing the quotient and remainder of division. It shows examples of using divmod()
with integers and floating-point numbers, showcasing its functionality for both data types.
Python3
print ( '(5, 4) = ' , divmod ( 5 , 4 ))
print ( '(10, 16) = ' , divmod ( 10 , 16 ))
print ( '(11, 11) = ' , divmod ( 11 , 11 ))
print ( '(15, 13) = ' , divmod ( 15 , 13 ))
print ( '(8.0, 3) = ' , divmod ( 8.0 , 3 ))
print ( '(3, 8.0) = ' , divmod ( 3 , 8.0 ))
print ( '(7.5, 2.5) = ' , divmod ( 7.5 , 2.5 ))
print ( '(2.6, 10.7) = ' , divmod ( 2.6 , 0.5 ))
|
Output
(5, 4) = (1, 1)
(10, 16) = (0, 10)
(11, 11) = (1, 0)
(15, 13) = (1, 2)
(8.0, 3) = (2.0, 2.0)
(3, 8.0) = (0.0, 3.0)
(7.5, 2.5) = (3.0, 0.0)
(2.6, 10.7) = (5.0, 0.10000000000000009)
Exceptions of Python divmod() Function
- If either of the arguments (say x and y), is a float, the result is (q, x%y). Here, q is the whole part of the quotient.
- If the second argument is 0, it returns Zero Division Error
- If the first argument is 0, it returns (0, 0)
Practical Application: Check if a number is prime or not using divmod() function.
Input : n = 7
Output :Prime
Input : n = 15
Output :Not Prime
Examples: Initialise a new variable, say x with the given integer and a variable counter to 0. Run a loop till the given integer becomes 0 and keep decrementing it. Save the value returned by divmod(n, x) in two variables, say p and q. Check if q is 0, this will imply that n is perfectly divisible by x, and hence increment the counter value. Check if the counter value is greater than 2, if yes, the number is not prime, else it is prime
PYTHON3
n = 15
x = n
count = 0
while x ! = 0 :
p, q = divmod (n, x)
x - = 1
if q = = 0 :
count + = 1
if count > 2 :
print ( 'Not Prime' )
else :
print ( 'Prime' )
|
More Applications:
In this example, we are calculating the sum of digits of a number using divmod
while also using it to calculate the quotient and remainder in Python.
Python3
num = 86
sums = 0
while num ! = 0 :
use = divmod (num, 10 )
dig = use[ 1 ]
sums = sums + dig
num = use[ 0 ]
print (sums)
|
In this example, we are reversing a number using divmod
while also utilizing it to calculate the quotient and remainder in Python.
Python3
num = 132
pal = 0
while num ! = 0 :
use = divmod (num, 10 )
dig = use[ 1 ]
pal = pal * 10 + dig
num = use[ 0 ]
print (pal)
|
Similar Reads
Python Built in Functions
Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
abs() in Python
The Python abs() function return the absolute value. The absolute value of any number is always positive it removes the negative sign of a number in Python. Example: Input: -29Output: 29Python abs() Function SyntaxThe abs() function in Python has the following syntax: Syntax: abs(number) number: Int
3 min read
Python - all() function
The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True otherwise it returns False. It also returns True if the iterable object is empty. Sometimes while working on some code if we want to ensure that user has not entered a False v
3 min read
Python any() function
Python any() function returns True if any of the elements of a given iterable( List, Dictionary, Tuple, set, etc) are True else it returns False. Example Input: [True, False, False]Output: True Input: [False, False, False]Output: FalsePython any() Function Syntaxany() function in Python has the foll
5 min read
ascii() in Python
Python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes. It's a built-in function that takes one argument and returns a string that represents the object using only ASCII characters. Exa
3 min read
bin() in Python
Python bin() function returns the binary string of a given integer. bin() function is used to convert integer to binary string. In this article, we will learn more about Python bin() function. Example In this example, we are using the bin() function to convert integer to binary string. C/C++ Code x
2 min read
bool() in Python
In Python, bool() is a built-in function that is used to convert a value to a Boolean (i.e., True or False). The Boolean data type represents truth values and is a fundamental concept in programming, often used in conditional statements, loops and logical operations. bool() function evaluates the tr
4 min read
Python bytes() method
bytes() method in Python is used to create a sequence of bytes. In this article, we will check How bytes() methods works in Python. [GFGTABS] Python a = "geeks" # UTF-8 encoding is used b = bytes(a, 'utf-8') print(b) [/GFGTABS]Outputb'geeks' Table of Content bytes() Method SyntaxUs
3 min read
chr() Function in Python
chr() function returns a string representing a character whose Unicode code point is the integer specified. chr() Example: C/C++ Code num = 97 print("ASCII Value of 97 is: ", chr(num)) OutputASCII Value of 97 is: a Python chr() Function Syntaxchr(num) Parametersnum: an Unicode code integer
3 min read
Python dict() Function
dict() function in Python is a built-in constructor used to create dictionaries. A dictionary is a mutable, unordered collection of key-value pairs, where each key is unique. The dict() function provides a flexible way to initialize dictionaries from various data structures. Example: [GFGTABS] Pytho
4 min read