How to use XOR in Python

XOR, short for exclusive OR, is a logical operation that takes two operands and returns a boolean value of True if and only if exactly one of the operands is True.

In Python, we can perform the XOR operation on two or more values using a variety of methods/operators, including the ^ operator. In the following sections we'll see this operator and a number of different functions that can help us achieve the same XOR result, some of which are better for certain use-cases than others.

Using the ^ Operator

Here is an example of using the ^ operator to perform XOR on two boolean values:

>>> True ^ False
>>> False ^ False
>>> True ^ True

As you can see, when exactly one of the operands is True, the XOR operation returns True. Otherwise, it returns False.

Using the xor Function

We can also use the xor function from the operator module to perform XOR on two values. Here is an example:

>>> from operator import xor
>>> xor(True, False)
>>> xor(False, False)
>>> xor(True, True)

As you can see, the xor function works in the same way as the ^ operator, but it provides an alternative syntax for performing XOR operations in Python.

Using Logical and and or Operators

In addition to the ^ operator and the operator.xor function, we can also use the and and or operators to achieve the same result as XOR. This can be done using the following logic:

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

xor = lambda a, b: (a and not b) or (not a and b)

Here is an example of using this logic to perform XOR in Python:

>>> (True and not False) or (not True and False)
>>> (False and not False) or (not False and False)
>>> (True and not True) or (not True and True)

As you can see, this method produces the same result as the ^ operator and the xor function. However, it may be less efficient because it involves more operations.

Using the functools.reduce Function

Another way to perform XOR in Python is to use the functools.reduce function, which allows us to apply a binary operation to a sequence of values. Here is an example of using reduce to perform XOR on a list of boolean values:

>>> from functools import reduce
>>> from operator import xor
>>> reduce(xor, [True, False, False, True])

As you can see, the reduce function applies the xor function to each element of the list, starting from the first two elements and then continuing with the result of the previous operation and the next element in the list. In this case, the final result is False, because the number of True values in the list is even.

One of the advantages of this is that we can more easily use it to perform XOR on an arbitrary number of values.


In this article, we have seen four different ways to perform XOR in Python: using the ^ operator, using logical and/or operators, using the xor function from the operator module, and using the functools.reduce function. All four ways of doing this provide a simple and concise syntax for performing XOR operations in Python.

Last Updated: June 16th, 2023
Was this helpful?

Building Your First Convolutional Neural Network With Keras

# python# artificial intelligence# machine learning# tensorflow

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup

Ā© 2013-2024 Stack Abuse. All rights reserved.