0
status= None
if 'Up' or 101 in status:
    print "Inside If statement"
else:
    print "Inside Else Statement"

The code flow goes inside "If" loop and prints "Inside If Statement". The status is None actually and by reading the code it should print "Inside Else Statement". I can modify the validation part and make it execute inside else statement. But i would like to know how "True" is returned for this condition

if 'Up' or 101 in status:
shriakhilc
  • 2,922
  • 2
  • 12
  • 17

1 Answers1

0

Strings in Python are falsy, which is to say an empty string ('') is equivalent to False, and others are True.

Your condition is evaluated as (brackets only for explanatory purpose)

if ('Up') or (101 in status):

And since 'Up' is always True it will always go inside the if block.

You can instead write:

if 'Up' in status or 101 in status:

Or a more generic way with any would be:

if any(x in status for x in ('Up', 101)):

You can find more answers for this in this question

shriakhilc
  • 2,922
  • 2
  • 12
  • 17