For every infix operator, e.g. +
there is a operator
-function (operator.add
for +
):
1 + 1
# Output: 2
from operator import add
add(1, 1)
# Output: 2
even though the main documentation states that for the arithmetic operators only numerical input is allowed it is possible:
from operator import mul
mul('a', 10)
# Output: 'aaaaaaaaaa'
mul([3], 3)
# Output: [3, 3, 3]
See also: mapping from operation to operator function in the official Python documentation.