August Williams August Williams - 1 month ago 10
Python Question

Is there a neat way to perform XOR operation on two conditions in IF statement?

I am currently creating a binary calculator which allows for both positive and negative binary inputs. I have the following code regarding my question:

if (firstvalue[0] == "-" and not secondvalue[0] == "-") or (secondvalue[0] == "-" and not firstvalue[0] == "-"):

So obviously, if either number is negative but not both then the final string will have a negative sign. Otherwise, both will be positive and there will be no negative sign on the string.

I'm just wondering if there is a neater way to do this? I tried using
but I guess its only a bitwise operator.

if firstvalue[0] == "-" ^ secondvalue[0] == "-":

I also tried
incase of the off chance but obviously no luck. Any suggestions on a a neater way to do this?


^ will work just fine if you use parentheses:

if (firstvalue[0] == "-") ^ (secondvalue[0] == "-"):

You can also use != in the place of ^. It works the exact same way here, but may be a little more clear.