Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 149 ยท Data Types

None

Learn what None means in Python, why you check it with "is None", where it appears as a default return value, and how to use it safely for optional values.

Sometimes a variable needs to say "there's nothing here yet". The user hasn't picked a color, the search found no match, the sensor hasn't reported. Python represents that absence with a special value called None.

None is Python's version of what other languages call null or nil. In this lesson you'll see where None comes from, how to test for it correctly, and how to use it for optional values without causing crashes.

What None is

None is a single, built-in object of type NoneType. It isn't zero, it isn't an empty string and it isn't False. It means "no value".

Python Meet None

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two facts from that output are worth remembering:

  • None is falsy, as you saw in Booleans and Truthiness. So if not middle_name: is true when the name is None.
  • None is not equal to 0, "", [] or False. Those are real values; None is the absence of one.

There's only ever one None object in a running program. Every variable that holds None points to that same object. That's why there's a special way to check for it.

Checking for None with is

The correct way to test for None is with the identity operator is:

Python is None and is not None

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

is asks "is this the very same object?", rather than "do these have equal values?" Because there's exactly one None, x is None is precise and fast. You'll learn more about is in Membership and Identity Operators.

Notice how describe(0) gives "scored 0". If the function had used if not score: instead, a score of zero would have been reported as "not graded yet". That's a classic bug: use is None whenever a falsy value like 0 or "" is a legitimate answer.

Note: x == None usually works too, but PEP 8 recommends x is None. A class can redefine == to behave strangely, while is can't be fooled. Write is not None rather than not x is None; both work, but the first reads naturally.

The type hint int | None in the function signature says "an int, or None". Hints like this don't change how the code runs, but they document intent and help editors catch mistakes. Type Hints covers them fully.

Functions return None by default

Every Python function returns something. If a function ends without a return statement, or uses a bare return, it returns None. This explains a puzzling output you may have seen already:

Python The hidden return value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last example is a frequent beginner bug. Methods that change an object in place, such as list.sort(), list.append() and dict.update(), return None to signal "I modified the original; there's no new value to give you". If you want a new sorted list, use sorted(numbers) instead. You'll see the difference in List Methods.

Tip: If you see None printed where you expected a value, look for a function that's missing a return, or a method that works in place.

None as a placeholder and default

None is the natural starting value for something you'll fill in later, and the standard default for optional function parameters:

Python Optional values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The best_score pattern is common: start with None to mean "nothing seen yet", then replace it with the first real value. Starting with 0 would give a wrong answer if all the scores were negative.

Using None as a default parameter also avoids a well-known trap with mutable defaults like []. You'll see why in Arguments: Defaults, *args and **kwargs.

Errors caused by None

Because None has almost no abilities, using it as if it were a real value raises an error. The most common message mentions 'NoneType':

Python NoneType errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When you see 'NoneType' object has no attribute ..., read it as "this variable was None when I expected a real object". Then trace back to where the value came from. The try/except blocks above just let the example keep running after each error; you'll learn them properly in Exceptions and try/except.

Supplying a fallback

Often you want "the value, or a default if it's missing". A conditional expression makes that explicit:

Python
nickname = None
display = nickname if nickname is not None else "anonymous"

You'll also see nickname or "anonymous". That's shorter, but it replaces every falsy value, including an empty string or 0. Use or when any falsy value should fall back, and the explicit is not None version when only None should.

None or an exception?

When a function can't produce a result, it has two ways to say so: return None, or raise an exception. Both are common, and choosing well makes your code easier to use.

Return None when "nothing found" is a normal, expected outcome that callers will routinely check, such as looking up an optional setting or searching for a user who may not exist. dict.get() and re.match() work this way.

Raise an exception when the situation is a genuine error that the caller shouldn't silently ignore, such as invalid input or a missing file. int("abc") raises ValueError instead of returning None, because carrying on with "no number" would only cause a more confusing failure later. You'll learn to raise your own in Raising Exceptions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a function return if it has no return statement?

    Every function returns a value. Without a return statement, that value is None.
  2. 2. Which is the recommended way to test whether result holds None?

    is None checks identity against the single None object. not result would also be true for 0, empty strings and empty lists.
  3. 3. After items = [3, 1, 2] and ordered = items.sort(), what is ordered?

    list.sort() sorts the list in place and returns None. Use sorted(items) to get a new list.
  4. 4. Which statement about None is true?

    None is falsy, so bool(None) is False, yet None == False is False. There is exactly one None object, and len(None) raises a TypeError.

Summary

  • None means "no value". It's the only value of type NoneType.
  • It's falsy, but it isn't equal to 0, "", [] or False.
  • Test for it with is None and is not None, especially when 0 or "" are valid values.
  • Functions without return, and in-place methods like list.sort(), return None.
  • None is the standard placeholder for "not set yet" and the default for optional parameters.
  • An error mentioning 'NoneType' means a variable was None when you expected a real object.

Next: Type Conversion, where you'll turn strings into numbers and back again.