using the or operator in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaarthikeyapreyan
    New Member
    • Apr 2007
    • 106

    using the or operator in python

    How does the python interpreter work for the or operator
    cause i am a bit confused with its output

    Code:
    >>> s= "unbound method __delitem__() must be called with _Environ instance as first argument (got str instance instead)"
    >>> 
    >>> if "least" in s:
    ...   print "hi"
    ... 
    [B]>>> if "least" or "exactly" in s:
    ...   print "hi"
    ... 
    hi[/B]
    >>> if "exactly" in s:
    ...   print "hi"
    ... 
    >>>
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Python's [or] is not much different from or in other languages. It simply returns the first "True" operand that it is passed. In your case you're using it wrong, because to Python, an empty string is False. As soon as a string has any non-null value it is true. So by saying "least" or "exactly" it will return the first true operand (least).

    To use it the way you are attempting you should have the following:
    [code=python]
    if "least" in s or "exactly" in s:
    ... print hi
    [/code]

    Comment

    Working...