Table of contents
1.
Introduction
2.
What is XOR in Python?
3.
How to perform the bitwise XOR in Python?
3.1.
Python
4.
Bitwise Python XOR operator Table
4.1.
Example of Python XOR operator of Two Variables
4.2.
Program in  Python
4.3.
Python
5.
Swapping two integers using XOR without a temporary variable
5.1.
Python
6.
XOR in Python using Operator Module
6.1.
Python
7.
Frequently Asked Questions
7.1.
What are operators?
7.2.
What are the types of operators?
7.3.
Why are operators used in Python?
7.4.
Are operators present in all programming languages?
8.
Conclusion
Last Updated: Aug 7, 2025
Easy

What is XOR in Python?

Introduction

Hello there,

Over here, you will be learning about the bitwise operator xor of two variables using Python. We all know what logical operators are, the AND, OR, NOT, and XOR. If you are unaware of what operators are, you can refer to this article, Operators. This article will discuss one of the Bitwise operators in Python, i.e., the Python XOR operator.

“ Recommended Topic, Python Round Function, Swapcase in Python”.

What is XOR in Python?

In Python, XOR (exclusive OR) is a logical operation that returns true if and only if exactly one of the operands is true. It is represented by the caret symbol ^. XOR returns false if both operands have the same boolean value (both true or both false). In Python, XOR can be applied to boolean values as well as integers.

How to perform the bitwise XOR in Python?

To perform bitwise XOR in Python, you can use the ^ operator. This operator takes two integers and performs the XOR operation on each pair of corresponding bits. The result is a new integer where each bit is the result of the XOR operation between the corresponding bits of the two input integers.

For example:

  • Python

Python

a = 5  # 0101 in binary
b = 3 # 0011 in binary

result = a ^ b
print(result)
You can also try this code with Online Python Compiler
Run Code

Output

6

 

In this example, the XOR operation is performed on each pair of corresponding bits of a and b, resulting in 6 which is 0110 in binary.

You can also check out the other Bitwise operators and Fibonacci Series in Python

Bitwise Python XOR operator Table

Here is a table to portray how the Python XOR operator works for binary values. 

X

Y

X ^ Y

0

0

0

0

1

1

1

0

1

1

1

0

Example of Python XOR operator of Two Variables

a = 14 = 1110(Binary)
b = 6 = 0110(Binary)
a ^ b = 1110^0110
         = 1000
         = 8 (Decimal)

Program in  Python

  • Python

Python

a = 14
b = 6
#Printing bitwise Python XOR operator
print("a ^ b =", a ^ b)
You can also try this code with Online Python Compiler
Run Code

Output:

a ^ b = 8

In this program above:

The value of a 14 is converted into a binary number "1110," and 6 is converted into the binary number "0110". The Bitwise operators are applied here to get the value "1000," giving the decimal value 8.

You can compile it with online python compiler.

Swapping two integers using XOR without a temporary variable

XOR can be used to swap two integers without using a temporary variable. This is based on the property that XORing a value with itself results in 0, and XORing a value with 0 leaves it unchanged. Therefore, by XORing the values appropriately, we can achieve the swapping.

  • Python

Python

a = 5
b = 7

print("Before swapping:")
print("a =", a)
print("b =", b)

a = a ^ b
b = a ^ b
a = a ^ b

print("\nAfter swapping:")
print("a =", a)
print("b =", b)
You can also try this code with Online Python Compiler
Run Code

Output

Before swapping:
a = 5
b = 7
After swapping:
a = 7
b = 5

In this example, a and b are swapped without using a temporary variable. The XOR operations ensure that the original values of a and b are correctly swapped.

XOR in Python using Operator Module

Python provides a module named operator which includes functions corresponding to the bitwise operations, including XOR. This module can be used to perform XOR operations in a more readable and expressive way.

  • Python

Python

import operator

a = 5
b = 3

result = operator.xor(a, b)

print("Result of XOR operation:", result)
You can also try this code with Online Python Compiler
Run Code

Output

Result of XOR operation: 6

In this example, we import the operator module and use the xor() function to perform the XOR operation between a and b. This provides a more explicit and readable way to perform XOR operations in Python.

Now, let's discuss some FAQs related to Python XOR operators.

Frequently Asked Questions

What are operators?

Operators are specific symbols that provide logical and computational functions in the program.

What are the types of operators?

There are 7 types of operators: arithmetic operators, assignment operators, comparison operators, logical operators, identity operators, membership operators, and bitwise operators.

Why are operators used in Python?

Operators are used for applying a logical sense in the programs. It is usually taken in binary values or true/false values.

Are operators present in all programming languages?

Yes, since it is the one that provides logical computation sense to the programs, it is not only present; it is essential while programming.

Conclusion

In this article, we have discussed what is XOR in Python. XOR (exclusive OR) in Python is a logical operation that returns true if and only if exactly one of the operands is true. It is represented by the caret symbol ^ and can be applied to Boolean values as well as integers. Understanding XOR is crucial in various programming contexts, including cryptography, data manipulation, and algorithm design.

Recommended Reading: 

Live masterclass