0

I want to do something if "val" is "a" or "as" or "asd" I write this:

if val == "a" or val == "as" or val == "asd":
    print("aaa")

but, is there any way to whrite "val" once?, like this:

if val == "a" or "as" or "asd":
    print("aaa")

if I write that, the "if", always is true:

val=123
if val == "a" or "as" or "asd":
    print("aaa")

and the console print "aaa"

and if I write this:

val="as"
if val == ("a" or "as" or "asd"):
    print("aaa")

the console print nothing the if is true only if val == "a", the first

I do not want to write "val==" for each of the possible 1000 outcomes of "val"...

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Camilo
  • 21
  • 1
  • 1
  • 3
  • 6
    You can do `if val in ['a', 'as', 'asd']` but im sure that has been asked and answered before, too – Tim May 13 '14 at 14:28
  • 3
    @arshajii Umm "*By the same token, many questions are asked using very different wordings but seek to solve identical questions - closing these helps folks find their way to a solution even when they don't know what terms to search for.*" Is this not a case? IMO debatable to say the least. – luk32 May 13 '14 at 14:40
  • 1
    @arshajii I believe OP wants to check whether an item belongs to set, just does not the right words for his question. What problem do you think he wants to solve? Make the "or or or" syntax work? "*I do not want to write "val==" for each of the possible 1000 outcomes of "val"... thanks and sorry my english*" That is all I need, to believe I'm right. The only difference is OP here uses constants. And this is not fundamental enough to be different IMO. – luk32 May 13 '14 at 14:43
  • @luk32 No, that's not the problem he actually wants to solve. The problem ultimately reduces to checking if an element is in a set, but that doesn't mean the question is a duplicate of, for e.g., "How do I check if an element is in a set?". Again, two questions having similar answers does not render them duplicates. – arshajii May 13 '14 at 14:46
  • @arshajii I just started a [discussion](http://meta.stackoverflow.com/q/254697/1903116) on meta. Please share your views there. – thefourtheye May 13 '14 at 15:29

2 Answers2

1

The simplest method is this:

if val in ("a", "as", "asd"):
    print("aaa")

This one doesn't work:

val = 123
if val == "a" or "as" or "asd":
    print("aaa")

because it is the same as writing:

if (val == "a") or "as" or "asd":
    print("aaa")

In that case, the first expression in the "or" is (val == "aa"), which is False, but the second expression is "as", which isn't false. That means that the whole expression evaluates to something true, and so "aaa" is printed.

And this one doesn't work:

if val == ("a" or "as" or "asd"):
    print("aaa")

because of the nature of "or". The expression "a" or "as" or "asd" evaluates to "a", because it is the first non-false value in the expression, so it is the same as saying:

if val == "a":
    print "aaa"
Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
1

It's a pythonic way;

if val in ['a','as','asd']:
   print("aaa")
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42