Check none

2 snippets in Python

Also written as None check

PYPython

None (Null Value)

PY · Variables & Types
Syntax
variable = None
Example
result = None
if result is None:
    print("No result yet")

# Common pattern: optional return
def find_user(user_id: int) -> str | None:
    return None
Output
No result yet

Note Always compare to None with 'is' or 'is not', never == or !=. None is a singleton object.

Truthiness Gotchas

PY · Common Mistakes
Syntax
# Falsy: None, False, 0, 0.0, '', [], {}, set()
Example
def process(data=None):
    # WRONG: if not data
    # This also catches empty list [], 0, and ""
    if not data:
        print("No data")  # Triggered by empty list too!

    # CORRECT: if data is None
    if data is None:
        print("Data is None")

process([])   # Might not intend to reject []
process(None)
process(0)
Output
No data
No data
Data is None
No data

Note Empty collections, zero, empty string, and None are all falsy. When you specifically mean 'no value provided', check 'is None' rather than relying on truthiness.

Frequently asked questions

How do you check none?
Python covers this with 2 copy-ready snippets on this page. The "None (Null Value)" snippet in Python uses `variable = None`.
Which code does the Python example use?
The "None (Null Value)" snippet uses `variable = None`, from the Variables & Types section of the Python cheat sheet.
What other Python snippets are shown for "check none"?
Besides "None (Null Value)", this page also shows "Truthiness Gotchas".
Is there anything to watch out for?
Yes. For "None (Null Value)": Always compare to None with 'is' or 'is not', never == or !=. None is a singleton object.