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 # Errorπ§° Why 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: Mayaβ οΈ Common 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.