Understand Python None for Beginners – Python Tutorial

By | July 30, 2019

Python None is a special constant, which represents a null or absent value. In this tutorial, we will introduce some basic knowledge about on using it.

None is unequal False, 0 or empty list, dictionary, tuple or string.

None means a value is absent, nobody knows its specific value. However, if a variable is 0, False, empty list, dictionary, tuple or string, the value of them are specific.

Here is an example to illustrate the difference among them.

>>> None == 0
False
>>> None == []
False
>>> None == False
False
>>> x = None
>>> y = None
>>> x == y
True

Functions that do not return anything will return None

Create a function that does not return any variables

def getValue(x)
    x = 3

From this function, getValue() return nothing.

y = getValue(5)

y is None.

if None is False and if not None always True

This truth is very helpful, especially you don’t know how to determine what value a variable will be.

>>> content = None
>>> if not content:
...     print("content is empty.")

Then the output is:

content is empty.

Leave a Reply

Your email address will not be published. Required fields are marked *