# XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are identical XOR outputs as 0 and when two bits are different then XOR outputs as 1. XOR can even be used on booleans.

XOR is mainly used in situations where we don’t want two conditions to be true simultaneously. In this tutorial, we will look explore multiple ways to perform XOR (exclusive OR) operations in Python with examples.

## Bitwise Operator

Bitwise operators in Python are also called binary operators, and it is mainly used to perform Bitwise calculations on integers, the integers are first converted into binary, and later the operations are performed bit by bit.

## Python XOR Operator

Let’s take a look at using the XOR `^` Operator between 2 integers. When we perform XOR between 2 integers, the operator returns the integer as output.

``````a=  5  #0101
b = 3  #0011

result	= (a ^ b) #0110

print(result)

# Output
# 6 (0110)``````

Let’s take a look at using XOR on two booleans. In the case of boolean, the true is treated as 1, and the false is treated as 0. Thus the output returned will be either true or false.

``````print(True ^ True)
print(True ^ False)
print(False ^ True)
print(False ^ False)``````

Output

``````False
True
True
False``````

## XOR using Operator Module

We can even achieve XOR using the built-in `operator `module in Python. The operator module has a `xor()` function, which can perform an XOR operation on integers and booleans, as shown below.

``````import operator

print(operator.xor(5,3))
print(operator.xor(True,True))
print(operator.xor(True,False))
print(operator.xor(False,True))
print(operator.xor(False,False))

``````

Output

``````6
False
True
True
False``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## Python type()

Table of Contents Hide type() Syntaxtype() Parameterstype() Return ValueDifference between type() function and isinstance() functionExample of isinstance() method in PythonExample 1: Finding the type of a Python objectExample 2: type()… ## How to check if a file exists in Python?

Table of Contents Hide Python check if a file exists using OS ModuleUsing os.path.exists()Using os.path.isfile()Using os.path.isdir()Using pathlib.Path.exists() When you perform a file operation such as reading from a file or… ## ModuleNotFoundError: No module named ‘xlsxwriter’

Table of Contents Hide What is ModuleNotFoundError: No module named ‘xlsxwriter’?How to fix ModuleNotFoundError: No module named ‘xlsxwriter’?Solution 1 – Installing and using the xlsxwriter module in a proper waySolution… ## Python string to datetime Conversion

Table of Contents Hide Convert Python String to datetime using datetime ModulePython String to datetimePython String to datePython String to timeConvert Python String to datetime using dateutil There are several… ## [Solved] Python can’t Multiply Sequence by non-int of type ‘float’

Table of Contents Hide TypeError: can’t multiply sequence by non-int of type ‘float’Multiplying string with an integerMultiplying string with a floating-point Solution TypeError: can’t multiply sequence by non-int of type ‘float’Conclusion… ## How to detect if document has loaded using Javascript or Jquery

A page can’t be manipulated safely until the document is “ready.” You might come across various scenarios where you need to call some function or perform some tasks only after…