How to use XOR in Python

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
True
>>> False ^ False
False
>>> True ^ True
False

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)
True
>>> xor(False, False)
False
>>> xor(True, True)
False

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)
True
>>> (False and not False) or (not False and False)
False
>>> (True and not True) or (not True and True)
False

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])
False

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.

Conclusion

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: December 13th, 2022
Was this helpful?
Project

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
Details
Course

Data Visualization in Python with Matplotlib and Pandas

# python# pandas# matplotlib

Data Visualization in Python with Matplotlib and Pandas is a course designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and...

David Landup
David Landup
Details

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

DisclosurePrivacyTerms