divmod() in Python 2026: Quotient & Remainder in One Call + Modern Use Cases
The built-in divmod() function returns a pair (quotient, remainder) when dividing two numbers — essentially combining integer division and modulo in a single, efficient operation. In 2026 it continues to be a small but powerful tool for algorithms, time/date calculations, unit conversions, cryptography (modular arithmetic), paging, and performance-sensitive numeric code.
With Python 3.12–3.14+ delivering faster integer arithmetic, free-threading support for concurrent math ops, and growing use in high-performance computing and ML preprocessing, divmod() remains one of the most optimized built-ins. This March 23, 2026 update covers how divmod() behaves today, real-world patterns, performance advantages, and best practices when used with large integers, NumPy/JAX, or in algorithmic code.
TL;DR — Key Takeaways 2026
divmod(x, y)→ returns tuple (x // y, x % y)- Works with integers (including arbitrary precision) — raises ZeroDivisionError if y == 0
- 2026 best practice: Use divmod() instead of separate // and % for clarity & minor speed gain
- Main use cases: time conversion (hours/minutes/seconds), modular arithmetic, paging, hashing
- Performance: Slightly faster than two separate operations (single division pass)
1. Basic Usage — Quotient & Remainder
q, r = divmod(12345, 60)
print(q, r) # 205 45 → 205 minutes + 45 seconds
print(divmod(-12345, 60)) # (-206, 15) (sign follows dividend)
print(divmod(100, 7)) # (14, 2)
print(divmod(0, 5)) # (0, 0)
2. Real-World Patterns in 2026
Time / Duration Conversion
def seconds_to_hms(seconds: int) -> tuple[int, int, int]:
minutes, sec = divmod(seconds, 60)
hours, min_ = divmod(minutes, 60)
return hours, min_, sec
print(seconds_to_hms(3665)) # (1, 1, 5)
Paging / Pagination Logic
def get_page_info(total_items: int, page_size: int, page_num: int):
total_pages, last_page_items = divmod(total_items, page_size)
total_pages += 1 if last_page_items else 0
return total_pages, last_page_items if page_num == total_pages else page_size
nprint(get_page_info(105, 20, 6)) # (6, 5) → 6 pages, last page has 5 items
Modular Arithmetic / Cryptography
def modular_inverse(a: int, m: int) -> int:
# Extended Euclidean algorithm using divmod
m0, x0, x1 = m, 0, 1
if m == 1:
return 0
while a > 1:
q, r = divmod(a, m)
m, a = r, m
x0, x1 = x1 - q * x0, x0
return x1 + m0 if x1 < 0 else x1
print(modular_inverse(3, 11)) # 4 (because 3*4 ≡ 1 mod 11)
3. divmod() vs Separate // and % – Comparison 2026
| Approach | Operations | Performance | Readability | Best For |
|---|---|---|---|---|
| divmod(x, y) | Single call | Slightly faster (one division) | Very clear intent | Most cases |
| x // y, x % y | Two operations | Microscopically slower | Explicit | When you need only one result |
| numpy.mod / jax.numpy.mod | Array ops | Fast on GPU/TPU | Vectorized | Large numeric arrays |
4. Best Practices & Performance in 2026
- Always use divmod() when you need both quotient and remainder — clearer and slightly faster
- Type hints 2026:
from typing import Tuple def divmod_positive(n: int, d: int) -> Tuple[int, int]: q, r = divmod(n, d) if r < 0: r += d q -= 1 return q, r - Performance: divmod() is C-optimized — one division pass instead of two
- Free-threading (3.14+): Safe — pure function, no shared state
- Avoid: divmod(0, 0) → ZeroDivisionError — validate divisor first
Conclusion — divmod() in 2026: Efficient Numeric Duo
divmod() is a small but elegant built-in that saves you from writing two operations when you need both quotient and remainder. In 2026, use it for time conversions, pagination, modular math, and any algorithm requiring both results. It’s fast, clear, and one of Python’s most useful numeric helpers — especially when paired with modern libraries like NumPy/JAX for vectorized work.
Next steps:
- Replace any // + % pairs with divmod() in your next numeric function
- Related articles: Efficient Python Code 2026 • Python Built-ins Overview 2026