-1

I'm having difficulty understand this logic:

>>> text = 'ay_118724981.jpg'
>>> 'jpg' in text
True
>>> 'png' in text
False
>>> if 'png' not in text or 'jpg' not in text or 'jpeg' not in text:
...     print('True')
... else:
...     print('False')
... 
True
>>> 

I'm confused 'coz the if statement should result in False since 'jpg' is in the text. It should give me True only when none of them is in the text. Correct ?

Mark Alexa
  • 11
  • 3
  • 1
    In that case you should use`and`. Such that from the moment one of the conditions is `False`, it fails. – Willem Van Onsem Dec 28 '17 at 20:34
  • not all the strings are in the text: True. Try avoid the negations and check out De Morgan's law – Jean-François Fabre Dec 28 '17 at 20:34
  • `'png' not in text` is `True`, everything after that is not even evaluated. `a or b` is true if `a` is true or b is true or both are true. – timgeb Dec 28 '17 at 20:35
  • python conditional statement are like plain english. convert your statement to english equivalent and ask yourself "why the result from the program is different than your expectation" and you'll get your answer – Moinuddin Quadri Dec 28 '17 at 20:36
  • I don't think so mate. Here I'm checking whether at least one is True. I'm not checking if all strings are true or false. – Mark Alexa Dec 28 '17 at 20:36
  • Great, you understood it. – timgeb Dec 28 '17 at 20:37
  • If `a or b` is true, return `True`. Here `a` is true, so `or` shortcuts after the first true statement, e.g. `'png' not in text`. You want to use `and`. – pylang Dec 28 '17 at 20:38
  • @Moinuddin Quadri I did that lol. 'jpg' is clearly there and I proved it in the line above it. – Mark Alexa Dec 28 '17 at 20:39
  • 1
    At this point I begin to question whether you understand the meaning of the word "or" in the english language. – timgeb Dec 28 '17 at 20:40
  • @MarkAlexa Need to work on your boolean math a bit. – tonypdmtr Dec 28 '17 at 20:52
  • Possible duplicate of [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Barmar Dec 28 '17 at 21:17

2 Answers2

3

It parses out as ('png' not in text) or ('jpg' not in text) or ('jpeg' not in text).

One of those conditions is true ('png' is not in text), so it evaluates as true. You can get the behavior you are expecting with an and

Beefster
  • 723
  • 7
  • 19
0

I'm confused [because] the if statement should result in False since 'jpg' is in the text. It should give me True only when none of them is in the text. Correct ?

No, the or operator is True if one or both of the operands is True. So from the moment, 'jpg' is not in the text, or 'png' is not in the text, or jpeg is not in the text, the test succeeds.

What you want here is the and operator. x and y is True, only when both operands (x and y) are not in the text. So we can use:

if 'png' not in text and 'jpg' not in text and 'jpeg' not in text:
    print('True')
else:
    print('False')

Since it can be confusing, and since this is a long expression, we can also use the all(..) builtin function here:

if all(part not in s for part in ['png', 'jpg', 'jpeg']):
    print('True')
else:
    print('False')

So only if all these parts are not in s, then the condition succeeds.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555