DEV Community

Discussion on: Python 'is' vs '=='

 
dbanty profile image
Dylan Anthony

There's actually a bit more to it than that. if not checks "truthyness", not just equality to False. So not foo will evaluate to True if foo is any of these and more:

  1. False
  2. None
  3. 0
  4. []
  5. {}
  6. ""

The same thing applies in the other direction, if [1] will succeed because the list is not empty, empty list are "falsey" whereas lists containing elements are "truthy".

The lesson here is pretty much don't use if without an explicit comparison (== or is) unless you're sure you know how truthy the value will be in all cases.