After this lesson you can represent a missing value with None, and you will know which values Python treats as False when it makes a decision.
None means no value
Sometimes a program needs a name that exists but has nothing meaningful in it yet: a member who has not chosen a username, a score that has not been recorded, a search that found no match. Python's answer is None. It is a single special value, and it has its own type.
username = None
print(username)
print(type(username))None
<class 'NoneType'>None is not zero, not an empty string, and not False. It is a separate idea: "nothing here". That difference matters. A student with a mark of 0 has a real score. A student whose mark is None has not been marked at all.
Testing for None
Because there is only ever one None object, the correct test uses is, which checks for the same object:
result = None
print(result is None)
print(result is not None)
print(result == None)True
False
TrueThe third line works, but it is not the accepted style, because a class can define == to behave oddly. Use is None and is not None and your code will always be correct.
Many operations return None to say "nothing to give you". The most surprising case is a function with no return statement. You will learn about functions soon, but you can already see the principle in print itself, which does its job and then hands back nothing:
shown = print("hello")
print(shown)hello
NoneTruthiness
An if statement, which you will meet in the next section, accepts any value as its condition, not just True or False. Python converts the value using fixed rules. A value that behaves like False is called falsy. Everything else is truthy. The falsy values are:
FalseandNone- zero of any number type:
0,0.0 - empty text:
"" - empty collections:
[],(),{}and an empty set
Every other value is truthy, including the text "0", the text "False" and a list that holds one zero. You can ask Python directly with bool():
values = [0, 1, -3, 0.0, "", " ", "0", None, [], [0], {}]
for value in values:
print(repr(value), "->", bool(value))0 -> False
1 -> True
-3 -> True
0.0 -> False
'' -> False
' ' -> True
'0' -> True
None -> False
[] -> False
[0] -> True
{} -> FalseThe repr() function shows the value as you would type it in code, which makes the empty string and the single space easy to tell apart. The loop belongs to the next section, so treat it as a tool that repeats for each item.
Using truthiness in practice
Truthiness lets you write short checks. Instead of testing whether a list has any items by comparing its length with zero, you can test the list itself:
basket = []
if basket:
print("Basket has items")
else:
print("Basket is empty")
basket.append("pen")
if basket:
print("Basket has items")
else:
print("Basket is empty")Basket is empty
Basket has itemsThe list method append is covered in the collections section. The point here is that an empty list is falsy and a list holding something is truthy.
A trap: zero is not None
Truthiness is convenient, but it can hide a bug when zero is a perfectly valid value. Compare these two checks on a score of zero:
score = 0
if not score:
print("treated as missing")
if score is None:
print("really missing")
else:
print("score is", score)treated as missing
score is 0The first check wrongly calls a genuine score of zero missing. When zero, an empty string or an empty list are valid answers in your program, test with is None and leave truthiness for places where empty really does mean nothing.
Choosing how to signal a missing value
None is only one way to signal that something is missing. Some programs use an empty string, a zero or a special value such as minus one. The trouble is that these look like real data, so they can slip into a calculation unnoticed. None is better because it cannot be mistaken for a genuine answer: adding None to a number fails loudly instead of quietly giving a wrong total. When you design a function that might find nothing, returning None is a clear and honest choice, as long as the code that calls it checks the result before using it.
Common mistakes
- Using
== None. It usually works butis Noneis the correct form. - Treating zero like None.
if not scorealso catches a real zero. Testis Nonewhen zero is allowed. - Thinking
"False"or"0"is falsy. Any non-empty string is truthy. - Printing a function's result when it has no return value and being surprised by
None. - Writing
Nonein lowercase. OnlyNonewith a capital N works.
A video version of this topic is in the video course at /learn/python/.
Exercise
Create a variable nickname = "" and a variable middle_name = None. Print bool(nickname), then print whether middle_name is missing using is None. Finally print the nickname if it is not empty, otherwise print the word friend, using a single expression with or.
Show a possible answer
nickname = ""
middle_name = None
print(bool(nickname))
print(middle_name is None)
print(nickname or "friend")False
True
friendKey points
- None is Python's way of saying there is no value here. It has its own type, NoneType.
- Falsy values are False, None, zero of any numeric type, and empty strings and empty collections. Everything else is truthy.
- bool(value) shows how Python sees any value in a condition.
- Test for None with is None or is not None, not with ==.
- Zero and None are different. A score of 0 is a real value, but a missing score is None.