Python
XOR operation
Logical Operations
Programming
Code Implementation

How do you get the logical xor of two variables in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Logical XOR means "exactly one of these conditions is true." Python does not have a dedicated keyword named xor, but it is still easy to express once you decide whether you are working with boolean logic, integer bit operations, or general truthy and falsy values.

For Booleans, != Is Often The Clearest

If both values are already booleans, inequality expresses logical XOR directly:

python
1a = True
2b = False
3
4result = a != b
5print(result)

That works because XOR is true when the operands differ and false when they are the same.

For plain boolean conditions, many Python developers prefer this over a more explicit bitwise operator because it reads naturally as logic rather than low-level bit manipulation.

^ Works On Booleans Too

Python booleans are a subtype of integers, so the bitwise XOR operator also works:

python
1a = True
2b = False
3
4result = a ^ b
5print(result)

This also produces True. The difference is mostly about readability and intent. ^ is perfectly valid, but it signals "bitwise XOR" first, which can be less clear if the variables are meant as logical conditions.

Convert Truthy Values When Needed

Sometimes the inputs are not literal True and False, but values that behave truthily or falsily. In that case, convert them to booleans explicitly before applying XOR logic.

python
1left = "hello"
2right = ""
3
4result = bool(left) != bool(right)
5print(result)

This is safer than applying ^ directly to arbitrary objects, because ^ is not a generic logical operator for all Python types. On integers it means bitwise XOR, and on unsupported objects it may fail entirely.

Write The Full Logic When Clarity Matters

If you want the code to spell out the truth table directly, combine and, or, and not:

python
1a = True
2b = False
3
4result = (a or b) and not (a and b)
5print(result)

This is longer, but it makes the rule explicit: at least one must be true, but not both. It is useful in teaching code or in places where you want the boolean logic to be unmistakable.

A Small Helper Function Keeps Intent Clear

If XOR appears in several places, a helper function can improve readability:

python
1def logical_xor(a, b):
2    return bool(a) != bool(b)
3
4
5print(logical_xor(False, False))
6print(logical_xor(False, True))
7print(logical_xor(True, False))
8print(logical_xor(True, True))

Using bool(...) inside the function ensures the helper behaves as logical XOR rather than bitwise XOR on non-boolean inputs.

Separate Logical XOR From Bitwise XOR

This distinction matters. If you run:

python
print(5 ^ 3)

The result is not a boolean. It is the integer bitwise XOR of 5 and 3. That is correct behavior, but it is a different operation from asking whether exactly one condition is true.

When the code is about conditions, think in booleans. When the code is about bit masks or binary protocols, ^ on integers may be exactly what you want.

Common Pitfalls

  • Using ^ on integers and expecting a boolean logical answer.
  • Forgetting that truthy objects should often be converted with bool(...) first.
  • Writing a custom XOR helper that behaves differently for booleans and integers without documenting that fact.
  • Choosing a clever expression when a != b would be clearer for simple boolean variables.
  • Mixing logical XOR and bitwise XOR concepts in the same explanation or code path.

Summary

  • For boolean values, a != b is often the clearest logical XOR in Python.
  • 'a ^ b also works for booleans, but it is the bitwise XOR operator.'
  • Convert non-boolean values with bool(...) when you want logical rather than numeric behavior.
  • Keep the difference between logical XOR and integer bitwise XOR explicit in your code.

Course illustration
Course illustration

All Rights Reserved.