The Wayback Machine - https://web.archive.org/web/20240717091104/https://www.geeksforgeeks.org/divmod-python-application/
Open In App

divmod() in Python and its application

Last Updated : 29 Nov, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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




# Python3 code to illustrate divmod()
# divmod() with int
print('(5, 4) = ', divmod(5, 4))
print('(10, 16) = ', divmod(10, 16))
print('(11, 11) = ', divmod(11, 11))
print('(15, 13) = ', divmod(15, 13))
 
# divmod() with int and Floats
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

  1. 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.
  2. If the second argument is 0, it returns Zero Division Error
  3. 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




# Python code to find if a number is
# prime or not using divmod()
 
# Given integer
n = 15
x = n
 
# Initialising counter to 0
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')


Output

Not 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




# Sum of digits of a number using divmod
num = 86
sums = 0
while num != 0:
    use = divmod(num, 10)
    dig = use[1]
    sums = sums + dig
    num = use[0]
print(sums)


Output

14


In this example, we are reversing a number using divmod while also utilizing it to calculate the quotient and remainder in Python.

Python3




# reversing a number using divmod
num = 132
pal = 0
while num != 0:
    use = divmod(num, 10)
    dig = use[1]
    pal = pal*10+dig
    num = use[0]
print(pal)


Output

231




Previous Article
Next Article

Similar Reads

Python | sympy.divmod() method
With the help of sympy.divmod() method, we can find the modulus and quotient of the given values. Syntax: sympy.divmod(val1, val2) Return: (quotient, modulo) Example #1 : # import sympy from sympy import * # Using sympy.divmod() method gfg = divmod(Integer(48), Integer(7)) print(gfg) Output: (6, 6) Example #2 : # import sympy from sympy import * #
1 min read
Build an Application to Search Installed Application using Python
In this article, we are going to write python scripts to search for an installed application on Windows and bind it with the GUI application. We are using winapps modules for managing installed applications on Windows. Prerequisite - Tkinter in Python To install the module, run this command in your terminal: pip install winapps Below is what the GU
6 min read
Login Application and Validating info using Kivy GUI and Pandas in Python
Prerequisites : Kivy, Pandas Kivy is a multiplatform GUI library, known for being responsive. It provides management of multiple screens in a single application. In this application we will be using multiple screens to log in user's info and validate it. We will save the information in a csv file and use pandas to validate the information inside of
5 min read
Create And Deploy A Stock Price Web Application using Python and Streamlit
In this article, we are going to see how to create and deploy a stock price web application. To create an amazing Web Application that deals with Data Science, we have a perfect platform to carry out this task. Streamlit and Python package builds and shares the data application in the fastest way possible. Streamlit is open source anyone can contri
4 min read
Profile Application using Python Flask and MySQL
A framework is a code library that makes a developer's life easier when building web applications by providing reusable code for common operations. There are a number of frameworks for Python, including Flask, Tornado, Pyramid, and Django. Flask is a lightweight web application framework. It is classified as a micro-framework because it does not re
10 min read
Build an Application to extract URL and Metadata from a PDF using Python
The PDF (Portable Document Format) is the most common use platform-independent file format developed by Adobe to present documents. There are lots of PDF-related packages for Python, one of them is the pdfx module. The pdfx module is used to extract URL, MetaData, and Plain text from a given PDF or PDF URL. Features: Extract references and metadata
3 min read
Python | Create simple animation for console-based application
As we know Python is a scripting language, and can be easily used to automate simple tasks. In this article, we will learn how to create a simple console-based animation, which can be used while developing a console based project as a utility. We will try to replicate the loading animation as shown below: We will be using following modules - sys mo
2 min read
Python | ToDo GUI Application using Tkinter
Prerequisites : Introduction to tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. In this article, we will learn how to create a ToDo GUI application using Tkinter, with a step-by-step guide. To create a tkinter : Importing the module – tkinter
5 min read
Hierarchical treeview in Python GUI application
Python uses different GUI applications that are helpful for the users while interacting with the applications they are using. There are basically three GUI(s) that python uses namely Tkinter, wxPython, and PyQt. All of these can operate with windows, Linux, and mac-OS. However, these GUI applications have many widgets i.e, controls that are helpful
3 min read
GUI chat application using Tkinter in Python
Chatbots are computer program that allows user to interact using input methods. The Application of chatbots is to interact with customers in big enterprises or companies and resolve their queries. Chatbots are mainly built for answering standard FAQs. The benefit of this type of system is that customer is no longer required to follow the same tradi
7 min read
Practice Tags :
three90RightbarBannerImg