None and Truthiness

What counts as false in a condition, how None differs from empty, and why `is None` is not the same test as `not x`.

Overview

The falsy values

There are not many, and the list is worth memorising:

None  False  0  0.0  ""  []  {}  set()

Everything else is truthy. That includes "0", "False", [0] and -1, all of which catch people out because they look empty or negative in some sense and are not.

truthiness.py

truthiness.py Python 3
Output

                    

none_vs_empty.py

none_vs_empty.py Python 3
Output

                    

Worth knowing

Falsy: None, False, 0, 0.0, "", [], {}, set(). Everything else is truthy.
not x means "empty or zero or missing". x is None means only "missing".
Use is None, not == None: there is exactly one None object.
if not result on a function that can return 0 is a real bug, not a style question.

None and Truthiness: A Practical Guide

Any value can be used in a condition. Knowing which ones count as false, and when that is the wrong question to ask, prevents a class of bug that produces no error at all.

The idiomatic empty test

if not items:

is how Python asks "is this empty?", and it is preferred over len(items) == 0 because it works on anything and reads as prose. For a list, a string or a dict, this is right and unremarkable.

Where it goes wrong

The trouble starts when a value can legitimately be 0 or "":

result = find(items, "a")  # returns index 0
if not result:
    print("not found")  # WRONG

Index 0 is falsy, so "found at the first position" and "not found at all" take the same branch. Nothing raises. The program is simply wrong for one input, and that input is the first element, which many test cases skip.

The page runs exactly this, printing the wrong answer and then the fix.

`is None` asks a different question

if result is None:

This tests for one specific object, not for emptiness. It is true for None and nothing else — not for 0, not for "". When a function returns "the thing, or None if there isn't one", this is the only correct test.

The rule that follows: use truthiness when you mean "empty or zero or missing, and I treat them the same". Use is None when None means something distinct from a legitimate empty value.

Why `is` rather than `==`

There is exactly one None object in a running program, so identity is the precise test and it is faster than equality. It also cannot be fooled: a class can define __eq__ so that x == None is true for something that is not None. is compares the object itself.

The same applies to the two default-argument patterns from earlier in the track: if basket is None is correct, and if not basket would treat an intentionally empty list as missing.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Which of these is truthy?

  2. A function returns an index or None. Why is `if not result` a bug?

  3. Why `is None` rather than `== None`?

Cheat sheet

None and Truthiness

Any value can be used in a condition. Knowing which ones count as false, and when that is the wrong question to ask, prevents a class of bug that produces no error at all.

PYTHON · vizlearn.in/python/none_and_truthiness.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.