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

# Return quotient and remainder when 5 is divided by 2
x = divmod(5, 2)
print(x)
# Prints (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))
# Prints (2, 1)

More Examples

# divmod() on Integers

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

x = divmod(2, 5)
print(x)
# Prints (0, 2)

x = divmod(3, 3)
print(x)
# Prints (1, 0)
# divmod() on Floating-point Numbers

x = divmod(5.0, 2)
print(x)
# Prints (2.0, 1.0)

x = divmod(2, 5.0)
print(x)
# Prints (0.0, 2.0)

x = divmod(4.5, 1.5)
print(x)
# Prints (3.0, 0.0)