Python Glossary Русский

all Python 3operatorfunction

operator.xor

bitwise

Bitwise exclusive OR: a ^ b, the bits set in exactly one operand.

Syntax

operator.xor(a, b)

For sets xor is the symmetric difference: items present in exactly one of the two. For bools it means "exactly one is true", a logical operation that has no keyword of its own. A classic trick finds the unpaired number: functools.reduce(operator.xor, xs) leaves the item that occurs an odd number of times.

Examples

import functools
import operator
print(operator.xor(5, 3))   # → 6
print(operator.xor(5, 3) == 5 ^ 3)   # → True
print(operator.xor(operator.xor(5, 3), 3))   # → 5
print(functools.reduce(operator.xor, [4, 7, 4, 9, 7]))   # → 9
print(operator.xor({1, 2}, {2, 3}) == {1, 3})   # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy