divmod() is a built-in Python function that returns a tuple containing the quotient and remainder of dividing two numbers — equivalent to (x // y, x % y) but computed in a single operation for efficiency. It works with integers and floats (though floats return floats), and supports negative numbers with the same sign as the divisor (Python’s floor division behavior). In 2026, divmod() remains essential in algorithms (base conversion, modular arithmetic), data science (binning, cyclic features), cryptography (modular exponentiation helpers), and performance-critical code — offering atomic division/modulus, readable output, and slight efficiency gains over separate // and %.
Here’s a complete, practical guide to using divmod() in Python: basic division & remainder, handling negatives & floats, real-world patterns (earthquake magnitude binning, time conversion, cyclic features), and modern best practices with type hints, performance, edge cases, and integration with NumPy/Dask/Polars/pandas.
Basic divmod() — quotient and remainder in one call.
print(divmod(25, 7)) # (3, 4) ? 25 = 3×7 + 4
print(divmod(100, 10)) # (10, 0)
print(divmod(-25, 7)) # (-4, 3) ? floor division: -25 = -4×7 + 3
print(divmod(25, -7)) # (-4, -3) ? sign follows divisor
print(divmod(-25, -7)) # (3, -4)
Floats & mixed types — returns floats when needed.
print(divmod(10.5, 3)) # (3.0, 1.5)
print(divmod(10, 3.0)) # (3.0, 1.0)
print(divmod(3.14159, 1)) # (3.0, 0.14158999999999988)
Real-world pattern: earthquake magnitude binning & remainder analysis.
import pandas as pd
import numpy as np
# Sample earthquake DataFrame
df = pd.DataFrame({
'mag': [4.2, 5.8, 6.5, 7.1, 8.0, 5.5, 9.1]
})
# Bin magnitudes into integer parts and decimal remainders
df[['mag_int', 'mag_frac']] = np.column_stack(divmod(df['mag'], 1))
df['mag_category'] = df['mag_int'].astype(int).map({
4: 'Light', 5: 'Moderate', 6: 'Strong', 7: 'Major', 8: 'Great', 9: 'Great'
})
# Time example: convert seconds to hours/minutes/seconds
total_seconds = 3665 # 1 hour 1 min 5 sec
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
print(f"{hours}h {minutes}m {seconds}s") # 1h 1m 5s
print(df)
Best practices for divmod() in Python & data workflows. Use divmod(x, y) — when both quotient and remainder are needed (more efficient than separate // and %). Modern tip: use NumPy np.divmod(arr, divisor) — vectorized for arrays; Polars pl.col('x').divmod(7) for columnar. Prefer divmod() in loops — avoids redundant division. Handle division by zero — raises ZeroDivisionError. Add type hints — def split_time(seconds: int) -> tuple[int, int, int]. Use for base conversion — divmod(n, base) in digit extraction. Use with negative numbers — remember floor division sign follows divisor. Use divmod() in cyclic features — divmod(day_of_year, 7) for weekday encoding. Profile performance — divmod() is slightly faster than two ops. Use // and % — when only one is needed. Use math.fmod() — for float remainder with sign of dividend. Use divmod() with big integers — handles arbitrary precision. Use in algorithms — Euclidean GCD, modular inverse helpers. Use divmod(a, b) — for consistent quotient/remainder in math libraries.
divmod() returns (quotient, remainder) tuple — efficient for division & modulus in one operation. In 2026, use for binning, time conversion, cyclic features, and integrate with NumPy/Polars/Dask for vectorized/columnar workflows. Master divmod(), and you’ll handle integer/float division cleanly and efficiently in any numerical or data pipeline.
Next time you need both quotient and remainder — use divmod(). It’s Python’s cleanest way to say: “Divide and get both results — in one fast call.”