Python divmod() Function

Returns quotient and remainder of division

Usage

The divmod() function returns a tuple containing the quotient and the remainder when dividend is divided by divisor.

Syntax

divmod(dividend,divisor)

Python divmod() function parameters
ParameterConditionDescription
dividendRequiredThe number being divided
divisorRequiredThe number doing the dividing

Basic Example

Example: Return quotient and remainder when 5 is divided by 2

x = divmod(5, 2)
print(x)  # (2, 1)

The result of divmod(a, b) is the same as a // b, a % b

a = 5
b = 2
print((a // b, a % b))  # (2, 1)

More Examples

Example: divmod() on Integers

x = divmod(5, 2)
print(x)	# (2, 1)
x = divmod(2, 5)
print(x)	# (0, 2)
x = divmod(3, 3)
print(x)	# (1, 0)

Example: divmod() on Floating-point Numbers

x = divmod(5.0, 2)
print(x)	# (2.0, 1.0)
x = divmod(2, 5.0)
print(x)	# (0.0, 2.0)
x = divmod(4.5, 1.5)
print(x)	# (3.0, 0.0)