serhii.net

In the middle of the desert you can say anything you want

27 Jan 2020

Day 392

Python truthy and falsy

TIL that values considered true/false without being explicitly True/False have a name, and from python - What is Truthy and Falsy? How is it different from True and False? - Stack Overflow here’s a handy list of them:

All values are considered “truthy” except for the following, which are “falsy”:

  • None
  • False
  • 0
  • 0.0
  • 0j
  • Decimal(0)
  • Fraction(0, 1)
  • [] - an empty list
  • {} - an empty dict
  • () - an empty tuple
  • '' - an empty str
  • b'' - an empty bytes
  • set() - an empty set
  • an empty range, like range(0)
  • objects for which
    • obj.__bool__() returns False
    • obj.__len__() returns 0

A “truthy” value will satisfy the check performed by if or while statements. We use “truthy” and “falsy” to differentiate from the bool values True and False.

Truth Value Testing

Nel mezzo del deserto posso dire tutto quello che voglio.