divmod(a, b)

The divmod() function takes two numbers as arguments and returns their quotient and remainder as a tuple.

It returns a tuple with the quotient-remainder pair from dividing a by b. For integers, the result is similar to (a // b, a % b). For floating-point numbers, the result is similar to (q, a % b), where q is typically equal to math.floor(a / b), but it can be one less. In any case, q * b + a % b is approximately equal to a. If a % b is not zero, it has the same sign as b, and 0 <= abs(a % b) < abs(b).

Parameters

The divmod() function takes two parameters:

  • number1: the numerator, which can be an integer or a floating-point number
  • number2: the denominator, which can be an integer or a floating-point number

Return Value

The divmod() function returns:

  • (quotient, remainder): a tuple containing the quotient and remainder of the division
  • TypeError: for any non-numeric argument

Examples

divmod(7, 5)  # (1, 2)
divmod(7.5, 5)  # (1.0, 2.5)
divmod(7.5, 2)  # (3.0, 1.5)