• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python divmod() Function – Get Quotient and Remainder After Division

Python divmod() Function – Get Quotient and Remainder After Division

June 5, 2022 Leave a Comment

The Python divmod() function allows us a way to easily get the quotient and remainder after division of two numbers.

print(divmod(15,4))

#Output:
(3, 3) 

When performing different calculations with numbers in Python, the ability to easily get certain pieces of information about the calculation can be useful.

One such case is when dividing by two numbers in Python. We can easily get the quotient and remainder after division in Python.

To get the quotient and remainder after division of two numbers in Python, the easiest way is with the Python divmod() function.

The Python divmod() function takes two arguments – the two numbers you want to divide, and returns a tuple with the first element being the quotient and the second element being the remainder.

Below is an example showing you how to use divmod() to get a quotient and remainder from two numbers in Python.

print(divmod(15,4))
print(divmod(20,6))
print(divmod(10,3))
print(divmod(19,5))

#Output:
(3, 3) 
(3, 2) 
(3, 1) 
(3, 4) 

You can then verify these are the correct values by multiplying the first returned element by the second parameter and adding the second returned element.

res = divmod(15,4)

print(res[0] * 4 + res[1])

#Output:
15

Calculating Quotient and Remainder with Float Inputs to divmod() in Python

The Python divmod() function allows float inputs as well as integer inputs. You will still get the quotient and remainder from the two floating point numbers in the same way as the integer inputs.

Below are a few examples showing how to find the quotient and remainder after division of two floats with divmod() in Python.

print(divmod(15.5,4.1))
print(divmod(20.3,9.2))
print(divmod(1.3,0.3))
print(divmod(5.4,0.5))

#Output:
(3.0, 3.200000000000001)
(2.0, 1.9000000000000021)
(4.0, 0.10000000000000009)
(10.0, 0.40000000000000036) 

Hopefully this article has been useful for you to learn how to use the divmod() function in Python.

Other Articles You'll Also Like:

  • 1.  Length of Set Python – Get Set Length with Python len() Function
  • 2.  Find Last Occurrence in String of Character or Substring in Python
  • 3.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 4.  How to Write Excel File to AWS S3 Bucket Using Python
  • 5.  Count Number of Files in Directory with Python
  • 6.  Pascal’s Triangle in Python
  • 7.  How to Divide Two Numbers in Python
  • 8.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 9.  Golden Ratio Constant phi in Python
  • 10.  Create Empty String Variable in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy