Skip to Content
Course content

27: The None Type and Its Uses

Click on the "Edit" button in the top corner of the screen to edit your slide content.

What exactly is None, and is it just like zero or False?

I see this come up a lot. The short answer is: no, it's not. In Python, None is its own data type (NoneType). It isn't a number, it isn't an empty string, and it isn't a boolean False.

Think of None as a way of saying, "There is no value here," or "This is intentionally empty." If you have a variable set to 0, you have a number. If you have a variable set to None, you have a placeholder indicating the absence of a value. I usually use it when I want to initialize a variable but I'm not yet sure what its actual value should be.

# None is not the same as other "empty" things
print(None == 0)      # False
print(None == False)  # False
print(None == "")     # False

Why does my function return None even when I didn't write a return statement?

This is one of those things that trips everyone up at first. In Python, every single function returns something. If you don't explicitly use the return keyword to send a value back, Python automatically returns None behind the scenes.

A classic example is using print() inside a function. Remember that print() just puts text on the screen; it doesn't "give" a value back to the code. If your function's last action is a print() call, the function itself still returns None.

def greet(name):
    print(f"Hello, {name}!")

result = greet("Alice") 
# "Hello, Alice!" is printed to the console, but...
print(result) # This will print: None

If you want result to actually hold a value, you have to use return. I've spent way too many hours debugging my own code only to realize I forgot one return statement at the end of a helper function.

Should I use == None or is None to check for emptiness?

You'll see both in old tutorials, but in modern Python, you should almost always use is None.

Here is the technical reason: == checks for equality (do these two things have the same value?), while is checks for identity (are these two things actually the exact same object in memory?). Since None is a singleton—meaning there is only ever one instance of None in your entire running program—checking identity is faster and safer.

user_bio = None

if user_bio is None:
    print("The user hasn't written a bio yet.")

if user_bio is not None:
    print("Here is the bio!")

How do I use None to handle optional function arguments?

This is probably the most practical "pro tip" I can give you regarding None. You might be tempted to use an empty list as a default argument, like def add_item(item, my_list=[]). Don't do that. Python creates that default list once when the function is defined, not every time it's called, which leads to some very weird bugs where items persist between different function calls.

Instead, use None as the default, and then assign the actual empty list inside the function. It's a standard Python idiom that you'll see in almost every professional codebase.

def add_to_inventory(item, inventory=None):
    if inventory is None:
        inventory = [] # Create a fresh list only if one wasn't provided
    
    inventory.append(item)
    return inventory

# Now it behaves exactly as you'd expect
print(add_to_inventory("Sword")) # ['Sword']
print(add_to_inventory("Shield")) # ['Shield']



📋 Practical Task

Building a User Profile Formatter

You are building a system to display user profiles. Some users provide a middle name and a biography, while others leave them blank (represented as None). Your goal is to write a function that cleans up this data for display.

Requirements:

  • Create a function called format_profile(first, middle, last, bio).
  • If middle is None, the full name should be "First Last".
  • If middle is provided, the full name should be "First Middle Last".
  • If bio is None, the bio section should display as "No biography provided.".
  • The function should return a formatted string combining the full name and the bio.

Test your code with these two scenarios:

# Scenario 1: Complete profile
print(format_profile("Jane", "Anne", "Doe", "Software Engineer from NY"))

# Scenario 2: Missing middle name and bio
print(format_profile("John", None, "Smith", None))
Rating
0 0

There are no comments for now.

to be the first to leave a comment.