Lesson 3 of 6 · Numbers, Strings and Operators · 2 min read · Updated
True, False and None
What Python treats as true, what it treats as false, and the value that means nothing.
True and False are the two bool values — note the capital letters. But every value in Python can stand in a condition, and Python decides whether it counts as true.
print(bool(0), bool(42))
print(bool(""), bool("hi"))
print(bool([]), bool([0]))
print(bool(None))
False True
False True
False True
False
The rule is short: zero, empty and None are false; everything else is true. An empty string, an empty list, an empty dictionary — all false. A list holding a single 0 is not empty, so it is true.
name = ""
if name:
print("Hello,", name)
else:
print("No name given")
No name given
Note
if name: is the Python way to write if name != "":, and if items: means "if the list is not empty". Experienced Python code uses this everywhere, so it is worth recognising.
None: no value yet
winner = None
print(winner)
print(type(winner))
if winner is None:
print("No winner yet")
None
<class 'NoneType'>
No winner yet
None means "nothing here", which is different from 0 or an empty string. A function that does not return anything gives back None — you will see that in the Functions topic.
Which value counts as true in an if?
Try this now
- Ask for a name; if the user just presses Enter, print "Guest" instead.
- Predict
bool(" ")(a single space), then check it.
Create a free account to save your progress, or sign in.