There are already several good answers here, but none discuss the general concept of "truthy" and "falsy" expressions in Python.
In Python, truthy expressions are expression that return True
when converted to bool
, and falsy expressions are expressions that return False
when converted to bool
. (Ref: Trey Hunner's regular expression tutorial; I'm not affiliated with Hunner, I just love his tutorials.)
Falsy stuff:
What's important here is that 0
, 0.0
, []
, None
and False
are all falsy.
When used in an if
statement, they will fail the test, and they will pass the test in an if not
statement.
Truthy stuff:
Non-zero numbers, non-empty lists, many objects (but read @tdelaney's answer for more details here), and True
are all truthy, so they pass if
and fail if not
tests.
Equality tests
When you use equality tests, you're not asking about the truthiness of an expression, you're asking whether it is equal to the other thing you provide, which is much more restrictive than general truthiness or falsiness.
EDIT: Additional references
Here are more references on "Truthy" and "Falsy" values in Python: