How do you get the logical xor of two variables in Python?
Categories:
Logical XOR in Python: Understanding and Implementing Exclusive OR

Explore various methods to achieve a logical XOR operation in Python, from built-in operators to custom functions, and understand their applications.
The logical exclusive OR (XOR) operation is a fundamental concept in computer science and boolean algebra. Unlike standard OR, which returns true if any operand is true, XOR returns true only if exactly one of its operands is true. In Python, there isn't a direct xor
keyword for logical operations on boolean values, but you can achieve this functionality using several techniques. This article will guide you through different ways to implement logical XOR in Python, explain the underlying principles, and provide practical examples.
Understanding Logical XOR
Before diving into implementation, let's clarify what logical XOR means. Given two boolean variables, A and B, the XOR operation (often denoted as A ⊕ B) evaluates to true if A is true and B is false, or if A is false and B is true. If both A and B are true, or both are false, the result is false. This is distinct from the bitwise XOR operator (^
), which operates on the binary representations of numbers.
Here's a truth table for logical XOR:
graph TD subgraph Inputs A[Variable A] B[Variable B] end subgraph Output XOR[A XOR B] end A -- True --> XOR_T1 B -- False --> XOR_T1 XOR_T1["True"] A -- False --> XOR_T2 B -- True --> XOR_T2 XOR_T2["True"] A -- True --> XOR_F1 B -- True --> XOR_F1 XOR_F1["False"] A -- False --> XOR_F2 B -- False --> XOR_F2 XOR_F2["False"] XOR_T1 --> XOR XOR_T2 --> XOR XOR_F1 --> XOR XOR_F2 --> XOR
Truth Table Representation of Logical XOR
Methods for Logical XOR in Python
Python offers several ways to achieve logical XOR, each with its own advantages. We'll explore using standard logical operators, the bitwise XOR operator, and defining a custom function.
1. Using Logical Operators (and
, or
, not
)
The most straightforward way to implement logical XOR for boolean values in Python is by combining the and
, or
, and not
operators, directly reflecting its truth table definition. The expression (A and not B) or (not A and B)
perfectly captures the XOR logic.
def logical_xor_operators(a, b):
return (a and not b) or (not a and b)
print(f"True XOR True: {logical_xor_operators(True, True)}")
print(f"True XOR False: {logical_xor_operators(True, False)}")
print(f"False XOR True: {logical_xor_operators(False, True)}")
print(f"False XOR False: {logical_xor_operators(False, False)}")
Implementing logical XOR using standard boolean operators.
True
and False
values.2. Using the Bitwise XOR Operator (^
)
Python's bitwise XOR operator (^
) can also be used for logical XOR, especially when dealing with integer values that can be implicitly converted to booleans (where 0 is False
and any non-zero integer is True
). When True
and False
are treated as 1
and 0
respectively, the bitwise XOR ^
behaves exactly like logical XOR.
def logical_xor_bitwise(a, b):
# Python treats True as 1 and False as 0 in arithmetic/bitwise operations
return bool(a ^ b)
print(f"True XOR True: {logical_xor_bitwise(True, True)}")
print(f"True XOR False: {logical_xor_bitwise(True, False)}")
print(f"False XOR True: {logical_xor_bitwise(False, True)}")
print(f"False XOR False: {logical_xor_bitwise(False, False)}")
# Example with integers (where 0 is False, non-zero is True)
print(f"1 XOR 0: {logical_xor_bitwise(1, 0)}")
print(f"5 XOR 0: {logical_xor_bitwise(5, 0)}")
print(f"1 XOR 1: {logical_xor_bitwise(1, 1)}")
Using the bitwise XOR operator for logical XOR.
bool()
if you need a strict boolean output, as a ^ b
will return an integer (0 or 1).3. Using Inequality (!=
)
A very simple and often overlooked way to achieve logical XOR for boolean values is to use the inequality operator (!=
). If two boolean values are different (one True
and one False
), then their XOR is True
. If they are the same (both True
or both False
), their XOR is False
. This is precisely what !=
checks.
def logical_xor_inequality(a, b):
return a != b
print(f"True XOR True: {logical_xor_inequality(True, True)}")
print(f"True XOR False: {logical_xor_inequality(True, False)}")
print(f"False XOR True: {logical_xor_inequality(False, True)}")
print(f"False XOR False: {logical_xor_inequality(False, False)}")
Implementing logical XOR using the inequality operator.