Lesson 16 +10 XP

None

None

None is a special value that means "nothing here". It is its own type, NoneType.

None means no value

x = None
print(x)  # None
print(type(x))  # <class 'NoneType'>

A default placeholder

Functions often return None when there is nothing to return:

def greet(name):
    if name:
        return "Hello " + name
    # no return means None

print(greet("Ada"))   # Hello Ada
print(greet(""))      # None

Check for None

Use is or is not to test for None:

x = None
if x is None:
    print("Nothing here")

The identity test is None is the recommended style.

None is falsy

In a boolean context, None counts as false:

if None:
    print("won't run")
else:
    print("None is falsy")

Common uses

  • Optional function arguments: def build(name=None):
  • Placeholder before a real value is assigned.
  • Signals from functions that have no result.

TL;DR

  • None means "no value" and has type NoneType.
  • Functions without a return give None.
  • Test with is None and is not None.
  • In conditions, None is falsy.