The divmod() method in python takes two numbers and returns a pair of numbers consisting of their quotient and remainder.
divmod(x, y) x and y : x is numerator and y is denominator x and y must be non complex
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)
Explanation : The divmod() method takes two parameters x and y, where x is treated as numerator and y is treated as denominator. The method calculates both x / y and x % y and returns both the values.
- 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.
(5, 4) = (1, 1) (10, 16) = (0, 10) (11, 11) = (1, 0) (15, 13) = (1, 2) (6.0, 5) = (2.0, 2.0) (3, 9.0) = (0.0, 3.0) (13.5, 6.2) = (3.0, 0.0) (1.6, 10.7) = (5.0, 0.10000000000000009)
Errors And Exceptions
- 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
- 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
Co-Author : Chinmoy Lenka
- Python | sympy.divmod() method
- isprintable() in Python and its application
- Python String isdigit() and its application
- Hierarchical treeview in Python GUI application
- Birthday Reminder Application in Python
- GUI chat application using Tkinter in Python
- Python | ToDo GUI Application using Tkinter
- Python String isnumeric() and its application
- Python String isalpha() and its application
- Create First GUI Application using Python-Tkinter
- Python String isspace() and its application
- English dictionary application using Python
- Python | Create simple animation for console-based application
- Python3 for GUI application | An Overview
- Timer Application using PyQt5
- PyQt5 - GUI Application to get IP address
- PyQt5 - Application to get the price of BitCoin
- PyQt5 - Create Paint Application
- Flask - (Creating first simple application)
- Tkinter Application to Switch Between Different Page Frames
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.