Python None Data Type
Table of Contents
In the last lesson, we learned Boolean values. Now letβs learn how Python represents the absence of a value.
Sometimes a variable exists, but we do not have a useful value for it yet.
For example, a user may not have added a phone number. A search may not have found a result. A function may have nothing to return.
Python uses a special value called None for these situations.
What Is None in Python?
None means no value or nothing here yet.
phone_number = Noneprint(phone_number)Output
NoneThe variable phone_number exists. But it does not currently contain a phone number.
None is not the same as an empty string, zero, or False. It is its own special value.
The Type of None
We can use type() to check the data type of None.
result = Noneprint(type(result))Output
<class 'NoneType'>The data type of None is called NoneType.
There is only one None value in Python. We must write it with a capital N.
value = None # Correctvalue = none # ErrorWhy Do We Use None?
We use None when a value is missing, unknown, or not ready yet.
Here is a variable whose value will be added later:
delivery_date = Noneprint(delivery_date)
delivery_date = "Monday"print(delivery_date)Output
NoneMondayAt first, we do not know the delivery date. Later, we replace None with the real value.
Checking a Value with is None
The clearest way to check for None is with the is operator.
middle_name = None
if middle_name is None: print("No middle name was provided")Output
No middle name was providedTo check that a value is not None, we use is not None.
score = 85
if score is not None: print(f"Score: {score}")Output
Score: 85For None, prefer is None and is not None instead of == None and != None.
None Is Different from Other Empty Values
These values may look similar, but they do not mean the same thing.
print(None == 0) # Falseprint(None == False) # Falseprint(None == "") # Falseprint(None == []) # FalseHere is the difference:
Nonemeans no value0is a numberFalseis a Boolean value""is an empty string[]is an empty list
We should choose the value that matches what our program means.
None Is Falsy
None is a falsy value. That means bool(None) gives False.
print(bool(None))Output
FalseBut a general truthy or falsy check cannot tell us exactly which value we have.
value = 0
if not value: print("The value is falsy")This code runs for 0, False, "", and None. If we only want to find None, we should write value is None.
Real-Life Example: Search Result
Imagine that we search for a student by ID. Before a match is found, the result is None.
student = None
if student is None: print("Student not found")else: print(f"Student: {student}")Output
Student not foundIf we later find a student, we can store the name instead.
student = "Maya"
if student is None: print("Student not found")else: print(f"Student: {student}")Output
Student: MayaCommon Mistakes with None
Mistake 1: Writing none in Lowercase
value = none # ErrorPython is case sensitive. We must write None with a capital N.
Mistake 2: Putting None in Quotes
"None" is text. It is not the special None value.
print(type("None")) # <class 'str'>print(type(None)) # <class 'NoneType'>Mistake 3: Treating Every Falsy Value as None
quantity = 0
if not quantity: print("No value")This prints the message even though quantity contains the valid number 0. If we only want to check for a missing value, we use quantity is None.
Next, we will learn about Type Conversion so we can change values from one data type to another.