Skip to main content

Python None

In Python, the special constant None stands for a missing value.

Its type is NoneType, and no other object of that type exists.


NoneType

Assign None to a variable to mean "no value" or "not set".

The type() function will show you the type of a None value.

Example

Give a variable the value None and print it:

Prompt
Write a Python program that assigns None to a variable and prints it.
Output: complete Python program

Example

Give a variable the value None and print its data type:

Prompt
Write a Python program that assigns None to a variable and prints its type.
Output: complete Python program

Comparing to None

When checking a value against None, use the identity operators is and is not.

Example

Check for None with the identity operator is:

Prompt
Write a Python program that compares a variable to None using "is". Set result to None and check if it is None.
Output: complete Python program

Example

The same check, written with is not:

Prompt
Write a Python program that compares a variable to None using "is not". Set result to None and check if it is not None.
Output: complete Python program

True or False

Wherever Python expects a Boolean, None counts as False.

Example

Test its truth value:

Prompt
Write a Python program that checks the truthiness of None. Use bool() to check if None evaluates to True or False.
Output: complete Python program

Functions returning None

A function that never explicitly returns a value gives back None by default.

Example

This function has no return statement, so the result is None:

Prompt
Write a Python program showing that functions without a return statement return None. Create a function that sets x = 5 but does not return it, then print the result.
Output: complete Python program