When the and-or Trick Fails : And Or « Language Basics « Python






When the and-or Trick Fails

When the and-or Trick Fails


a = "" 
b = "second" 
print 1 and a or b


# Since a is an empty string, which is false in a boolean context, 
#1 and '' evaluates to '', and then '' or 'second' evaluates to 'second'. 

           
       








Related examples in the same category

1.And or with values in a list And or with values in a list
2.Introducing andIntroducing and
3.And Or in Python: int, empty list and empty dictionaryAnd Or in Python: int, empty list and empty dictionary
4.Introducing orIntroducing or
5.Using the and-or TrickUsing the and-or Trick
6.Using the and-or Trick Safely