W3docs

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

You can use the ^ operator to get the logical XOR of two variables in Python.

You can use the ^ operator to get the XOR of two variables in Python. It performs a bitwise XOR on integers and a logical XOR on boolean values. Here is an example:

Get the XOR of two variables in Python

python— editable, runs on the server
Info

Watch a video course: Python - The Practical Guide

For boolean values, ^ evaluates to True only when the operands differ, matching the standard logical XOR truth table.