Python: True, False (boolean)
Literal Value for True and False
True
and
False
are literal values of boolean type.
print(type(True)) # <class 'bool'> print(type(False)) # <class 'bool'>
What Value Evaluates to True?
In a if statement,
- non-empty things, and number
1
, eval toTrue
. None
, zero, and empty things eval toFalse
.
Following evaluates to False
:
False
→ A builtin literal expression.None
→ A builtin literal expression.0
→ Zero.0.0
→ Zero, float.""
→ Empty string.[]
→ Empty list.()
→ Empty tuple.{}
→ Empty dictionary.set([])
→ Empty set.frozenset([])
→ Empty frozen set.
Convert to True False
You can use the function
bool
to test.
# all the following are True print(bool(3)) print(bool("abc")) # all the following are False print(bool(None)) print(bool([])) print(bool(0)) print(bool(""))