12

I am trying to compile an if statement in python where it checks two variables to see if they are <= .05. Now if both variables are True, I just want the code to pass/continue, but if only one of the variables are True, then I want the code to do something. eg:

ht1 = 0.04
ht2 = 0.03

if (ht1 <= 0.05) or (ht2 <= 0.05):
    # do something
else:
    pass

I don't think this example will work the way I would want it as my understanding of OR is 1 condition returns True or both conditions return True. If someone could assit in pointing me in the right direction, it would greatly be apprecaited.

TsvGis
  • 622
  • 2
  • 10
  • 23
  • you can think of xor operation something like this , if bool((ht1 <= 0.05)) != bool((ht2 <= 0.05)): – coder3521 Sep 24 '15 at 04:11
  • Related: [How do you get the logical xor of two variables in Python?](https://stackoverflow.com/q/432842/3357935) – Stevoisiak May 06 '22 at 17:26

4 Answers4

17

What you want is called an "exclusive-OR", which in this case can be expressed as a 'not-equal' or 'is not' relation:

if (ht <= 0.05) is not (ht2 <= 0.05):

The way this works is that the if will only succeed if one of them is True and the other one is False. If they're both True or both False then it'll go to the else block.

tzaman
  • 46,925
  • 11
  • 90
  • 115
  • 1
    Correct, but subtle. Relational expressions return a bool. A bool is one or other of the two singleton objects `True` or `False`. Therefore, `is not` is an appropriate test. Personally I'd prefer `!=`. – nigel222 Sep 24 '15 at 08:10
  • 1
    @nigel222 Agree that it's a little subtle; I only used `is not` specifically due to the fact that it's a comparison of `bool`s. It reads a little cleaner to me this way, but that's definitely a taste thing. In my mind, the `!=` would get a little lost among the other `<=` comparisons even though it's serving a vastly different purpose (i.e. might seem mathematical even though it's a logical operator). – tzaman Sep 24 '15 at 14:20
  • Thanks for that explanation. To me, the brackets in `(ht1 <= 0.05) != (ht2 <= 0.05)` make the `!=` stand apart. Agreed, taste not substance. – nigel222 Sep 24 '15 at 15:32
  • I find the behavior of `if (ht <= 0.05) ^ (ht2 <= 0.05):` more obvious for some reason. Someone used to exclusive-or `^` will immediately see that this condition is only true when exactly one of the members is true. – Guimoute Sep 16 '21 at 11:44
9

Since relational operators always result in a bool, just check to see if they are different values.

if (ht1 <= 0.05) != (ht2 <= 0.05): # Check if only one is true
   ...
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
3

This method is technically slower because it has to calculate the comparisons twice, but I find it slightly more readable. Your mileage may vary.

ht1 = 0.04
ht2 = 0.03

if (ht1 <= 0.05) and (ht2 <= 0.05):
   pass
elif (ht1 <= 0.05) or (ht2 <= 0.05):
    # do something.
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
0

Just another way of doing so:

if  max(ht1, ht2) > 0.05 and min(ht1, ht2) <= 0.05:
luoluo
  • 5,353
  • 3
  • 30
  • 41