Lesson 14 of 18 · 2 min
None — The Special "Nothing" Value
What is None?
None is not zero. None is not an empty string. None means there is literally no value at all — the variable exists but is completely empty.
result = None # this box exists, but it holds absolutely nothing print(result) # display: None print(type(result)) # display: <class 'NoneType'>
Output: None <class 'NoneType'>
When Functions Return None
Any function that does not have a return statement gives back None:
def show_message(text): # define a function (we cover these in detail later)
print(text) # this function prints but does not RETURN anything
x = show_message("Hello") # call the function — it prints, then returns None
print(x) # display what the function returned
Output: Hello None
Checking for None — Use "is" Not "=="
value = None # a variable with no value yet
if value is None: # correct way to check for None
print("No value has been set") # this line will print
else:
print("Value exists:", value)
Output: No value has been set
