Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

divmod

divmod() returns a pair containing the quotient and remainder for two non-complex numbers. For integers, it is equivalent to (a // b, a % b), while floating-point inputs have documented rounding nuances.

How it works

divmod(a, b) performs division and returns both results as a two-item tuple: the quotient from integer division and the remainder. For integers, it is equivalent to (a // b, a % b); for floating-point operands, the quotient generally follows floor division but can differ by one because of rounding.

Syntax

divmod(a, b, /)

Parameters

NameTypeDescription
anumberThe dividend, a non-complex number to divide.
bnumberThe divisor, a non-complex number used for integer division and the remainder.

Returns

A pair of numbers containing the quotient and remainder from integer division.

Key points

  • The result is a two-item pair: the quotient comes first and the remainder second.
  • For integer operands, it combines floor-division and modulo into one operation.
  • When operand types differ, Python resolves the operation using its standard binary-arithmetic rules.
  • With floating-point operands, the quotient generally follows floor division but can be one smaller.
  • The quotient and remainder approximately reconstruct the original dividend.

Common mistakes

  • Both arguments must be non-complex numbers; complex operands are not supported.
  • For a nonzero remainder, its sign follows the divisor, which matters when operands can be negative.
  • The remainder's absolute value is always less than the divisor's absolute value.

Example: Convert seconds into hours, minutes, and seconds

total_seconds = 3671
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
print(hours, minutes, seconds)  # 1 1 11

The first call splits seconds into whole minutes and leftover seconds; the second splits minutes into hours and leftover minutes.

When to use it

Unit conversion

A duration must be decomposed into larger and smaller units.

Chunk indexing

A sequence is processed in fixed-size groups and both the group index and position are needed.

Cycle and offset calculations

A cyclic position must be represented as a cycle count plus an offset.

Interview questions

QWhat does divmod() return for integers?

For integer operands, divmod(a, b) returns exactly the same pair as (a // b, a % b). It packages the quotient and remainder together, which is useful when both values are needed.

Follow-up: How does this differ for floats? The returned pair is (q, a % b); q is usually math.floor(a / b), but floating-point behavior can make it one less.

QWhat inputs does divmod() accept?

The function returns a pair containing the quotient and remainder from integer division. With operands of mixed types, Python applies the normal rules for binary arithmetic operators.

Follow-up: What restriction applies regardless of numeric type? The inputs must be non-complex numbers.

QHow does divmod() behave with floating-point numbers?

For floating-point inputs, the quotient is usually the floor of the mathematical division, while the remainder is computed as a % b. The documentation notes that floating-point rounding can make the quotient one less than the usual floor result.

Follow-up: What relationship should still approximately hold? q * b + a % b should be very close to a.

  • //
  • %
  • math.floor

Source: Built-in Functions. Checked 2026-10-15.