all Python 3operatorfunction
operator.mod
arithmetic
Return a % b — the remainder (the functional form of `%`).
Syntax
operator.mod(a, b)
The sign of the result follows the divisor, not the dividend: mod(-7, 2) is 1, mod(7, -2) is -1. This keeps it consistent with floordiv: a == (a // b) * b + a % b. For strings % is old-style formatting, not a remainder: mod('%s!', 'hi') returns 'hi!'. For floats with negative operands math.fmod is more precise.
Examples
import operator
print(operator.mod(7, 3)) # → 1
print(operator.mod(7, 3) == 7 % 3) # → True
print(operator.mod(-7, 3)) # → 2
print(list(map(operator.mod, [10, 11, 12], [3, 3, 3]))) # → [1, 2, 0]
print(operator.mod('%s-%s', ('a', 'b'))) # → a-b
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy