Here I expected to get True in the second case too, so clearly I don't
really get how they work.

You're seeing short-circuit evaluation:

  >>> "3" or "4"  # true
  '3'
  >>> '4' or '3'  # true
  '4'
  >>> '4' in l    # false
  False
  >>> '3' or False  # true
  '3'
  >>> '4' or '42' in l  # true: same as "'4' or ('42' in l)"
  '4'
  >>> '4' or '42'
  '4'
  >>> ('4' or '42') in l # true:  same as "'4' in l"
  '4'

It just happens that sometimes you get unexpected results that happen to be right because of how Python handles strings/numbers as booleans and order-of-operations.

What I really need is to create a sequence of "if" statements to check
the presence of elements in a list, because some of them are mutually
exclusive, so if for example there are both "3" and "no3" it should
return an error.

The "in" operator only checks containment of one item, not multiples, so you have to manage the checking yourself. This is fairly easy with the any()/all() functions added in 2.5:

  any(i in l for i in ("3", "4"))
  all(i in l for i in ("3", "4"))

For more complex containment testing, using sets will be more efficient and offers a richer family of batch operators (contains, intersection, union, difference, issubset, issuperset)

-tkc




--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to