None
In this tutorial, we'll represent an intentional missing value.
None represents "no value" or "nothing here yet".
middle_name = None
This does not mean the variable is missing. The variable exists, but its value is intentionally empty.
Why None Is Useful
Sometimes you need a placeholder before you know the real value:
selected_item = None
print(selected_item)
Later, the value can change:
selected_item = "Notebook"
print(selected_item)
Checking for None
Use is None to check for None:
middle_name = None
if middle_name is None:
print("No middle name saved")
else:
print(middle_name)
Use is not None for the opposite:
score = 0
if score is not None:
print("Score recorded")
Notice that 0 is not the same as None. An empty score and a score of zero mean different things.
None Is Not a String
This is a string:
value = "None"
This is the special None value:
value = None
They are not the same.
Common Mistake
Do not compare to None with == while learning:
if value == None:
print("Missing")
Prefer:
if value is None:
print("Missing")
Practice
Create a variable called chosen_colour and set it to None. If it is None, print "Please choose a colour". Then assign it a colour and print it.