Using the and-or Trick Safely
a = "" b = "second" print (1 and [a] or [b])[0] # Since [a] is a nonempty list, it is never false. Even if a is 0 or '', # or some other false value, the list [a] is true because it has one element.
1. | And or with values in a list | ||
2. | Introducing and | ||
3. | And Or in Python: int, empty list and empty dictionary | ||
4. | Introducing or | ||
5. | Using the and-or Trick | ||
6. | When the and-or Trick Fails |