# 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(dividend, divisor)`

## 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)

Get notified of the best deals on our WordPress themes.

## JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Table of Contents Hide JSONDecodeError: Expecting value: line 1 column 1 (char 0)Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)Solution Other Possible SolutionsEnsure HTTP 200 status code and valid…

## Python ValueError: could not convert string to float

Table of Contents Hide ValueError: could not convert string to floatException could not convert string to float Fix ValueError: could not convert string to floatSolution 1: Ensure the string has…

## ImportError: attempted relative import with no known parent package

Table of Contents Hide How does module import work in Python?Absolute vs. Relative importsHow to fix ImportError: attempted relative import with no known parent package?Option 1 – Use absolute importsOption…

## Python FileNotFoundError: [Errno 2] No such file or directory Solution

Table of Contents Hide Python FileNotFoundError: [Errno 2] No such file or directoryExample FileNotFoundErrorMisspelled file nameInvalid file path or directory pathUsing a relative pathSolution to FileNotFoundError: [Errno 2] No such…

## TypeError: list indices must be integers or slices, not tuple

If you are accessing the list elements in Python, you need to access it using its index position. If you specify a tuple or a list as an index, Python…

## List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…