Python None Data Type

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 = None
print(phone_number)

Output

None

The 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 = None
print(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 # Correct
value = 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 = None
print(delivery_date)
delivery_date = "Monday"
print(delivery_date)

Output

None
Monday

At 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 provided

To 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: 85

For 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) # False
print(None == False) # False
print(None == "") # False
print(None == []) # False

Here is the difference:

  • None means no value
  • 0 is a number
  • False is 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

False

But 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 found

If 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 # Error

Python 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.