pythonoperatorsbitwise-xor

Is the python XOR bitwise operator not just a regular operator?


Other questions on this site suggest that python has no XOR operator, only a bitwise operator ^. But when I try this operator on booleans the result is also a boolean (Python 3.9.12)

True ^ False
>> True

If it was a bitwise operator I would expect it to first cast the inputs to integers, resulting in an integer as output. Is bitwise XOR still an appropriate name for ^? And why doesn't python implement an XOR keyword to make it more python-esque?


Solution

  • Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true). In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. The built-in function bool() can be used to convert any value to a Boolean, if the value can be interpreted as a truth value (see section Truth Value Testing above).

    it is as if you are writing:

    1^0
    #output
    1
    

    https://docs.python.org/3/library/stdtypes.html#boolean-values