1
wow = ["wowza this is a really long string wows"]

if "long"  or "is" in wow:
    print("yes")

output is yes

however if I put in

wow = ["wowza this is a really long string wows"]

if "long" in wow:
print("yes")

output is nothing

same with:

 wow = ["wowza this is a really long string wows"]

    if "is" in wow:
    print("yes")

why?

I thought or was supposed to be either one? but if individually inputed both are false but with or it becomes true?

or that's the wrong way to write an if or statement?

should it be written like this?

if "long" in wow or "is" in wow:
Halcyon Abraham Ramirez
  • 1,520
  • 1
  • 16
  • 17
  • [Python 3 Operator Precedence Table](https://docs.python.org/3/reference/expressions.html#operator-precedence) – Shashank May 10 '15 at 23:48
  • `'long'` is `True` and thus the expression will always be `True`. – Malik Brahimi May 11 '15 at 00:01
  • 2
    This question has been asked so many hundreds of times. Why do people keep writing new answers to it instead of just marking it a dup? – abarnert May 11 '15 at 00:02
  • 1
    @abarnert Reputation try-hards. – Malik Brahimi May 11 '15 at 00:04
  • @MalikBrahimi: Don't you lose the rep once the question is closed? (I'm actually not sure about that; I've never bothered to go search on meta, but I always assumed there was no rep gain or loss for answering good duplicates, garbage questions, or anything else that's going to be closed.) – abarnert May 11 '15 at 00:06
  • @abarnert No. You get what you get unless downvoted. – Malik Brahimi May 11 '15 at 00:09
  • @MalikBrahimi: Ah, now I understand a little better the people who downvote everyone who answers a bad question… – abarnert May 11 '15 at 00:10
  • @MalikBrahimi Thanks for rewarding contributors wish such kind words. Perhaps it would make more sense to automatically append their answers to the original post instead, once you mark a question as duplicate? Oh, no, it wouldn't work: then the answers would lose context. But that would mean context is important, and this means it is also useful to answer duplicates within their original context, for the benefit of the person who posted the duplicate. – dmvianna May 11 '15 at 00:18
  • @MalikBrahimi, I don't feel obliged to search for original answers when paying a poster the favour of answering their questions. Thank you for performing that work for me. – dmvianna May 11 '15 at 00:19
  • @dmvianna Actually I didn't downvote or mark as duplicate. – Malik Brahimi May 11 '15 at 11:18

3 Answers3

6
if "long"  or "is" in wow:

is equivalent to

if ("long")  or ("is" in wow):

What you want is:

if "long" in wow or "is" in wow:

The first test is successful, because "long" or "is" in wow returns "long" and the string "long" is truthy*. The following tests fail, because wow is a list with 1 string, and the in operator tests for list containment here, instead of string containment.

*the or "is" in wow part would not get tested here, because the if short-circuits. Has nothing to do with your problem though

Felk
  • 7,720
  • 2
  • 35
  • 65
  • thank you. I got confused with these or statements – Halcyon Abraham Ramirez May 10 '15 at 23:38
  • 1
    I thought `if "long" or "is" in wow:` was returning true because we are checking `if "long" or "is"` which is True. – Scott May 10 '15 at 23:39
  • 3
    @Scott the `in` operator has higher priority than the `or` operator. [Operator precedence](https://docs.python.org/2/reference/expressions.html#operator-precedence) – Felk May 10 '15 at 23:43
  • @Felk `in` over `or` thanks for educating me. So the first line then is really doing `'is' in wow` which is False, then `if 'long' or False:...` which is True. – Scott May 10 '15 at 23:48
4

OK, we have several different problems here.

if "long"  or "is" in wow:

That means this:

if ("long")  or ("is" in wow):

So yes, you need to change it to this:

if "long" in wow or "is" in wow:

But that still doesn't do what you want, because wow is a list. You're asking whether "long" is an element of that list. But "long" isn't an element of the list. The only element of the list is the string "wowza this is a really long string wows", which is not the same as "long". You want to check against the string itself, not the list.

Kevin
  • 28,963
  • 9
  • 62
  • 81
4

How about you send your condition to the interactive prompt (run python or ipython if you have it) to see what it returns?

>>> wow = ["wowza this is a really long string wows"]
>>> "long" in wow
False
>>> "long" or "is" in wow
'long'

You probably want to test the string, not a single item list:

>>> wow = wow[0]
>>> "long" in wow
True
>>> "long" or "is" in wow
'long'
>>> ("long" or "is") in wow
True
>>> wow
'wowza this is a really long string wows'
dmvianna
  • 15,088
  • 18
  • 77
  • 106
  • thanks for that. It's just the or operator I was confused at. and yes I should've used a string instead of a list. thank you. I needed some clarification. thank you very much for taking the time to educate everyone – Halcyon Abraham Ramirez May 11 '15 at 00:09