Let's talk about Python's None value.
None valuePython has a special object that's typically used for representing nothingness.
It's called None.
If we look at None from the Python REPL, we'll see nothing at all:
>>> name = None
>>>
Though if we print it, we'll see None:
>>> name = None
>>> name
>>> print(name)
None
When checking for None values, you'll usually see Python's is operator used (for identity) instead of the equality operator (==):
>>> name is None
True
>>> name == None
True
Why is that?
Well, None has its own special type, the NoneType, and it's the only object of that type:
>>> type(None)
<class 'NoneType'>
In fact, if we got a reference to that NoneType class, and then we called that class to make a new instance of it, we'll actually get back the same exact instance, always, every time we call it:
>>> NoneType = type(None)
>>> NoneType() is None
True
The NoneType class is a singleton class.
So comparing to None with is works because there's only one None value.
No object should compare as equal to None unless it is None.
None is falseyWe often rely on the falsiness of None in Python.
So instead of checking whether an object is None, we could check whether that object is falsey.
This works particularly well if all of the objects we're working with are either truthy or None.
For example, the search function in Python's re module always returns either None or a match object, and match objects are always truthy.
So instead of asking if match is not None, we can just say if not match:
>>> import re
>>> email = "[email protected]"
>>> match = re.search(r"^\S+@\S+[.]\S+$", email)
>>> if not match:
... print("That doesn't look like an email address")
...
This also works well if a falsey object should be interpreted the same way as None.
For example, if an empty string and None should both be handled the same way:
>>> name = ""
>>> if not name:
... print("No name was given")
...
No name was given
In cases like this, checking for falsiness rather than checking for None is actually preferable.
None represents nothingnessWhere does None come up?
Well, conventionally None is used for saying "there's nothing here".
The dictionary get method is a good example of where None appears.
The get method can look up a value for a key or return a default value when that key is missing:
>>> fruits = {"apple": 3, "banana": 5, "lime": 6}
>>> pears = fruits.get("pear", 0)
>>> pears
0
But if no default value is given, we'll get None:
>>> plums = fruits.get("plums")
>>> print(plums)
None
None is also sometimes used as the default value for function arguments.
For example, the string split method will treat None the same way as if no argument had been given at all:
>>> hughes = "Does it dry up\nlike a raisin in the sun?\n"
>>> hughes.split(None)
['Does', 'it', 'dry', 'up', 'like', 'a', 'raisin', 'in', 'the', 'sun?']
>>> hughes.split()
['Does', 'it', 'dry', 'up', 'like', 'a', 'raisin', 'in', 'the', 'sun?']
NoneEvery function in Python returns something, even functions without any return statement.
If a function has no return value, it returns None.
So if you call a function and it always seems to return None, it's probably because that function isn't meant to return anything:
>>> def greet(name):
... print("Hello", name)
...
>>> result = greet("world")
Hello world
>>> print(result)
None
See print versus return for more on Python's default function return value.
None is like NULL in other programming languagesPython's None value is similar to NULL or NIL in other programming languages.
None is the default return value of a function in Python, and it's often used to represent something that's missing or has no value yet.
It's common to use falsiness checks when looking for None values.
But you'll sometimes see the is operator used to explicitly look for None.
Python Jumpstart is designed to help new Python programmers get up to speed quickly. Get hands-on practice with 50 bite-sized modules that build your Python skills step by step.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!