divmod() Quotient and remainder
Last updated: 2026-09-22
divmod() Quotient and remainder
divmod(a, b) returns (a // b, a % b); works for floats too.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
divmod(a, b)
⚙️ Parameters
| a Required | The dividend (numeric). |
|---|---|
| b Required | The divisor (numeric, non-zero). |
Returns:tuple. The pair (a // b, a % b).
💥 Raises
ZeroDivisionError— Raised when b is zero.TypeError— Raised when the operand types do not support floor division.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
(3, 2)
(3.0, 2.5)
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is divmod(a, b) equivalent to?
AIt is equivalent to (a // b, a % b) — one call gives you both quotient and remainder, and they are meant to be used together.
QWhy does divmod(17, 5) return (3, 2)?
ABecause 17 // 5 = 3 and 17 % 5 = 2, and they satisfy the identity 17 == 3 * 5 + 2.
QWhat happens when the divisor is 0?
AIt raises a ZeroDivisionError, the same as computing 17 // 0 directly.
QHow is the sign of the remainder determined with negative numbers?
AIt follows Python's % rule: the remainder takes the sign of the divisor, e.g. divmod(-17, 5) returns (-4, 3).