Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python's None: Null in Python (Summary)

None is a powerful tool in the Python toolbox. Like True and False, None is an immutable keyword. As the null in Python, you use it to mark missing values and results, and even default parameters where it’s a much better choice than mutable types.

Now you can:

  • Test for None with is and is Not
  • Choose when None is a valid value in your code
  • Use None and its alternatives as default parameters
  • Decipher None and NoneType in your tracebacks
  • Use None and Optional in type hints

Sample Code (.zip)

1015 bytes

Course Slides (.pdf)

2.5 MB

00:00 Welcome back. This is our fifth and final video. It’s our conclusion video and so I’d like to go over the key points which we discussed in the previous sections.

00:09 So, one of the first things I showed you is that None is the return value of functions that don’t have a return value, or that don’t have an explicit return value. It’s an object, it’s a constant, and it’s a singleton, meaning that there is only one None.

00:24 This last point is important because when you’re checking for None, although in many cases the equality operator, so the double equal signs, will get you the result that you want.

00:34 If you really want to check if something is None, you have to use the is keyword, and this is because everything which is None is pointing to the same instance of None. So None is an object, but there is a single instance of this object and all things which are None are pointing to it. I also showed you how sometimes when you’re creating a function, or rather, when you are defining a function, if you’re using a mutable data type as a default parameter, this can get you into trouble.

01:02 The code in the function definition is only run once, when it’s first called, so this can lead to some unwanted behavior. Every time that you use this function without passing a parameter, it’s going to be using the first parameter, the default parameter, which was run when the function was first defined. And a way to get around this is to set default parameters to None.

01:23 Also, something that’s important to keep in mind here is that if you do need to accept None as a valid input, the easiest way to do this is to create a class which you can use as a signal to not accept an input. That way you’re free to accept None as an input if it comes up.

01:39 The last thing we saw in the video just before this one was how to deal with None in tracebacks. So, often you’ll get a traceback when something which wasn’t expected to be None turned out to be None. There are three steps which you can use to deal with this sort of error.

01:54 First, find the attribute that raised the error, find the object that this attribute was called on, and then follow the steps through your code to see what led that to be None.

02:03 So, chances are, you didn’t want this to be None, or else you need to modify your code so that it’s able to deal with None in this situation. That’s it for this lesson.

02:13 This was the last lesson in our course. I hope you enjoyed it, I hope you learned something new, and I hope we’ll see you again on Real Python. Remember to practice what you learned today, and keep learning!

Avatar image for Agil C

Agil C on July 30, 2020

Thanks for the video..!

Avatar image for Sam W

Sam W on Aug. 2, 2020

This was all very helpful. So thank you.

But it has confused me about something I had thought I understood.

I use if var: a lot to test if data exists. And I’m usually basing that on a returned function.


def main(input):
    test = test_func(input)
    if test:

def test_func(input):
    if input == something:
        return something

Is it wrong of me, then, to use this Truthy/Falsey if test: approach? My test_func is returning None if the condition is not met, and so I want my main func to do_something_else().

To ensure this, would I be better writing if test is not None: instead?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 3, 2020

Using explicit comparison is more Pythonic because it clearly states your intent. That’s one of the core philosophies you’ll find in the Zen of Python.

More importantly, however, relying on implicit truthy/falsy values is risky because it may not work as expected, depending on what your functions return. For example, if zero (0) should be considered a success, which often is in Unix programs, then your condition won’t work, because zero evaluates to false in a Boolean context.

As a rule of thumb, use explicit comparison whenever possible.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 3, 2020

By the way, it’s also worth noting to avoid double negation in expressions like this:

if test is not None:

You can swap the order to make it a little easier on the eyes:

if test is None:
Avatar image for Ghani

Ghani on Oct. 18, 2020

Nice course!

By the way, the last lesson should be titled “Python’s None: Null in Python (Summary)” and not Python’s None: Null in Python (Overview).


Avatar image for Chris Bailey

Chris Bailey RP Team on Oct. 19, 2020

@Ghani, Thanks, it is fixed!

Become a Member to join the conversation.