DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Check If a Variable Is Not None in Python

In Python, use `value is not None` to check specifically for a non-None value. A truthiness check also rejects values such as 0, False, and empty strings.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use value is not None to check that a Python variable is not None:

if value is not None:
    # value exists, even if it is 0, False, or ""
    ...

Python’s missing-value singleton is spelled None, not “null.” The right condition depends on whether you mean “not None” or “not empty/false.”

As an Amazon Associate I earn from qualifying purchases.

Check specifically whether a value is not None

Use the identity test is not None when only None should count as absent:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if value is not None:
    print("A value was provided")

This condition also passes for 0, False, an empty string (""), and an empty list ([]). Those values are not None, even though some are false in a Boolean context.

To check the opposite—whether a value is exactly None—write value is None.

Use a truthiness check only when falsey values also count as missing

if value: checks whether a value is truthy, not whether it is different from None. It rejects None, False, numeric zero, and empty strings and containers. Use it when that broader behavior is what you want; Python’s documentation lists values treated as false in Boolean contexts in the expressions reference.

For example, this function treats an empty string as a supplied name:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
def greet(name=None):
    if name is not None:
        print(f"Hello, {name}")
    else:
        print("Hello, guest")

If an empty string should instead trigger the guest greeting, use a truthiness check deliberately:

def greet(name=None):
    if name:
        print(f"Hello, {name}")
    else:
        print("Hello, guest")

Why use is instead of ==

Prefer value is None and value is not None over value == None and value != None. Python’s is and is not operators test object identity; None is a singleton. The Python language reference describes identity comparisons, and PEP 8 recommends using is or is not for comparisons to singletons such as None.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the condition that matches your intent

What you want to test Condition What it accepts
Value is not the None singleton value is not None 0, False, empty strings and containers, and other non-None values
Value is the None singleton value is None Only None
Value is truthy if value: Values that are not falsey; rejects None, False, zero, and empty strings and containers

PEP 8 specifically warns that a truthiness test can be wrong when checking whether an optional argument with a default of None was given a different value: the caller may have supplied a valid but falsey value.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.