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






Using the and-or Trick

Using the and-or Trick



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

print 0 and a or b

# evaluated from left to right, so the and is evaluated first. 

# 0 and 'first' evaluates to False, and then 0 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.When the and-or Trick FailsWhen the and-or Trick Fails
6.Using the and-or Trick Safely