English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonction en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python divmod() usage and example

Python built-in functions

The divmod() method takes two numbers and returns a pair of numbers (tuple) consisting of their quotient and remainder.

The syntax of divmod() is:

divmod(x, y)

divmod() parameters

divmod() has two parameters:

  • x-a non-complex number (numerator)

  • y-a non-complex number (denominator)

divmod() return value

divmod() returns

  • (q, r)- The quotientqand the remainderra pair of numbers consisting of (tuple

IfxandyIs an integer, then the return value of divmod() is the same as (a // b, x % y).

IfxoryIf it is a floating-point type, the result is (q, x%y). Here,qIs the entire part of the quotient.

Example: How does divmod() work in Python?

print('divmod(8, 3) = ', divmod(8, 3))
print('divmod(3, 8) = ', divmod(3, 8))
print('divmod(5, 5) = ', divmod(5, 5))
# divmod() with floating-point numbers
print('divmod(8.0, 3) = ', divmod(8.0, 3))
print('divmod(3, 8.0) = ', divmod(3, 8.0))
print('divmod(7.5, 2.5) = ', divmod(7.5, 2.5))
print('divmod(2.6, 0.5) = ', divmod(2.6, 0.5))

When running this program, the output is:

divmod(8, 3) = (2, 2)
divmod(3, 8) = (0, 3)
divmod(5, 5) = (1, 0)
divmod(8.0, 3) = (2.0, 2.0)
divmod(3, 8.0) = (0.0, 3.0)
divmod(7.5, 2.5) = (3.0, 0.0)
divmod(2.6, 0.5) = (5.0, 0.10000000000000009)

Python built-in functions