all Python 3operatorfunction
operator.and_
bitwise
Bitwise AND: a & b (the trailing underscore avoids clashing with the keyword `and`).
Syntax
operator.and_(a, b)
This is bitwise &, not logical and: both arguments are always evaluated, there is no short-circuit, and the result differs — and_(2, 1) is 0 while 2 and 1 is 1. For sets and_ is intersection, for bools a non-short-circuit logical AND. To check that all items are true use all(), not reduce with and_.
Examples
import operator
import functools
print(operator.and_(5, 3)) # → 1
print(operator.and_(5, 3) == 5 & 3) # → True
print(operator.and_(2, 3) == (2 and 3)) # → False
print(functools.reduce(operator.and_, [0b1110, 0b1011, 0b0111])) # → 2
print(operator.and_(True, False)) # → False
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy