Python divmod()

The divmod() in Python is a built-in function that takes two numbers as input parameters and returns a pair of numbers (a tuple) consisting of their quotient and remainder.

divmod() Syntax 

The syntax of divmod() is:

divmod(dividenddivisor)

divmod() Parameters

divmod() takes two parameters as input

  • dividend – The number you want to divide. A non-complex number (numerator)
  • divisor – The number you want to divide with. A non-complex number (denominator)

divmod() Return Value

  • divmod() returns a pair of numbers (a tuple) consisting of quotient and remainder.

If x and y are integers, the return value from divmod() 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.

Example: How divmod() works in Python?


# Python3 code to demonstrate divmod()
 
# divmod() on integers 
print('divmod(4, 2) = ', divmod(4, 2))
print('divmod(5, 3) = ', divmod(5, 3))
print('divmod(6, 6) = ', divmod(6, 6))

# divmod() with Floats
print('divmod(4.0, 2) = ', divmod(4.0, 2))
print('divmod(5.5, 3.3) = ', divmod(5.5, 3.3))
print('divmod(6.5, 6.5) = ', divmod(6.5, 6.5))

Output

divmod(4, 2) =  (2, 0)
divmod(5, 3) =  (1, 2)
divmod(6, 6) =  (1, 0)

divmod(4.0, 2) =  (2.0, 0.0)
divmod(5.5, 3.3) =  (1.0, 2.2)
divmod(6.5, 6.5) =  (1.0, 0.0)

Errors And Exceptions 

  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)
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Isprintable()

Python String isprintable()

Table of Contents Hide What are printable characters in Python?isprintable() Syntaxisprintable() Parametersisprintable() Return ValueExample 1: Working of isprintable() method Example 2: How to use isprintable() in real world? The Python…
View Post
How To Copy A File In Python

How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess…
View Post
How To Rename A File In Python

How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…
View Post
Syntaxerror Unexpected Eof While Parsing

SyntaxError: unexpected EOF while parsing

Table of Contents Hide What is unexpected EOF while parsing error?Scenario 1 – Incomplete parameters may cause this kind of errors.Scenario 2: Incomplete functions along with statements, loops, try and…
View Post