What is the divmod() function in Python?

divmod() is a built-in method in Python that takes two real numbers as arguments and returns a pair of numbers (tuple) consisting of quotient q and remainder r values. Although modulus and division are two different operations, they are indirectly related as one returns the quotient, and the other one returns the remainder.

Syntax

The syntax of the divmod() method in Python is as follows.

divmod(dividend, divisor)

Parameters

  • dividend or numerator
  • divisor or denominator

This function takes two non-complex integers as parameters.

Return value

divmod() returns a tuple that contains the quotient and remainder.

  • If x (dividend) and y (divisor) are integers, then the return value is the pair (x / y, x % y).
  • If x (dividend) and y (divisor) are floats, the result is the pair of the whole part of the quotient and x modulus y.

Application: Checking a number, either a Prime number or not.

Question

Check if a number is prime or not by using the divmod() function.

Show Answer

Code

Below are some examples to understand the functionality of the divmod() method.

  • Case #1: both arguments are integer values, which results in a tuple of integer values.
  • Casen#2: both arguments are floating-point or double types, which results in a tuple of float values.
  • Casen#3: one value is a floating-point value and the other is an integer, which results in a tuple of float values.
# Python divmod() function example
# Calling function
result = divmod(20,2)
# Displaying result
print(result)
print(type(result))

Free Resources