How to use the XOR operator in Python
Operators serve as the fundamental components of any expression, whether it's in the realm of mathematics or logic. Without operators, articulating the specific operations you intend to execute would be very difficult.
Consider a scenario where you're piloting a light aircraft without a control yoke or joystick – directing its course would be virtually impossible. In a similar vein, the absence of operators would render it impossible for the compiler or interpreter of our code to discern the nature of the operations you aim to execute. This is where our friend the XOR operator comes in hand.
What is XOR?
XOR, also known as Exclusive OR is a bitwise operator (an operator which uses binarynamely 0s and 1s). The XOR operation returns 0 as an output if both inputs are the same (e.g. 0 & 0, or 1 & 1), and 1 if both inputs are different (e.g. 0 & 1, or 1 & 0). The visual representation of this is provided in the below truth table:
XOR Truth Table in Binary
Input 1  Input 2  Output 

0  0  0 
0  1  1 
1  0  1 
1  1  0 
For further simplification, take two binary numbers 0101 and 1010, '⊕' is the mathematical symbol for the Exclusive OR operation so the truth table for 0101⊕1010 would be:
0101⊕1010 Truth Table
Input 1  Input 2  Output 

0  1  1 
1  0  1 
0  1  1 
1  0  1 
The answer for 0101 ⊕ 1010, would be 1111 as no two inputs were the same (hence the name Exclusive OR).
How to use the XOR operator in Python?
Python is one of the easiest languages to learn about logic gates including the bitwise XOR operator due its beginner friendly syntax nature. You can practise this on websites such as Replit.com or directly in your favourite code editor.
Method 1
Implementing XOR in Python is simple, the operator can easily be performed using the “^” character. An example is provided below:
In the above example, Python first converts the values in the variables a & b into binary, then the bitwise operation is calculated and printed out.
Method 2
Another way to use XOR is via the xor() function using the operator module:
Performing XOR on Booleans
Since XOR is a bitwise operator, it conducts a bitbybit comparison of the binary representations of the integers involved. It can therefore be used directly with Boolean values and Integers. False would be converted to 0 and True would be 1. For example:
The above code will print out the following:
 True XOR True = False
 True XOR False = True
 False XOR True = True
 False XOR False = False
Use cases of XOR
1. Encryption
XOR works hand in hand with encryption, it was one of the very first ways in which encryption was used, many advanced encryption strategies today continue to follow the concepts derived from XOR encryption, though by itself it is a meagre form of data encryption.
Take an example of this at play; you have a friend named Dan, and only you and Dan know a key  lets say 8. If at some point in time you were to send him a digit such as 4 without anybody knowing what it was, the number can easily be encrypted using the XOR operator.
Once Dan has received the encrypted number, he would use the key to decode it back to the original value.
2. Swapping two integers
Another use case of the XOR operator is to swap two numbers with each other. A real scenario in which this may come in hand is switching on the lights. Let's say we have two numbers 0 or 1, or in this case False and True, A is False (0) and B is True (1). We perform the swaps using the XOR operation in the following manner:
XOR Lights On or Off example:
Conclusion

XOR, or Exclusive OR, is a powerful bitwise operator that you'll find quite fascinating. It works its magic by returning a value of '1' when one and only one of the operands happens to be '1,' which is essentially the same as saying 'True' in the world of logic.

In the Python programming language, XOR is conveniently represented by the "^" symbol.

You don't have to limit yourself to just the "^" symbol. Python offers you even more flexibility with the XOR operation. You can make use of the xor() function by importing the operator module.

But wait, there's more!  Bonus Tip: XOR also allows you to check for equality. It returns 1 if the two operands are different and 0 if the two operands are the same. For e.g. if you had to compare two values you can XOR the two values and if it returns 1 then they are not equal and if it returns 0 then it is equal.