python
variable-testing
conditional-statements
boolean-values
programming-tips

In Python how should I test if a variable is None, True or False

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, the right test depends on what you mean. None, True, and False are three distinct values, but Python also has general truthiness rules, so a value like 0 or "" can behave like False in a condition even though it is not the boolean False.

Use Identity Checks for None

None is a singleton, so the canonical check is is None or is not None.

python
1def describe(value):
2    if value is None:
3        return "missing"
4    return f"provided: {value}"
5
6
7print(describe(None))
8print(describe(0))
9print(describe(""))

This is important because 0, empty strings, and empty containers are falsy, but they are not the same as a missing value.

Avoid == None. Equality can be overloaded, while identity with None is clear and idiomatic.

Use Truthiness Only When Any Truthy Value Is Acceptable

Sometimes you do not care whether the value is literally True. You only care whether it should behave as enabled or disabled in a condition.

python
1items = [None, False, True, 0, 1, "", "text", [], [1]]
2
3for item in items:
4    print(repr(item), "->", bool(item))

In cases like:

python
if value:
    print("do the thing")

Python treats many non-boolean objects as truthy or falsy. That is convenient, but only when that broader behavior matches your intent.

Use Explicit Boolean Checks for Tri-State Logic

If an API allows None, True, and False as separate meanings, write the branches explicitly.

python
1def feature_mode(flag):
2    if flag is None:
3        return "use default"
4    if flag is True:
5        return "enabled"
6    if flag is False:
7        return "disabled"
8    raise ValueError("flag must be None, True, or False")
9
10
11for value in [None, True, False]:
12    print(value, "=>", feature_mode(value))

This is clearer than trying to compress the same idea into a truthiness check that accidentally treats 0 and "" like valid boolean inputs.

If you need to accept only real booleans, validate the type:

python
1def require_bool(flag):
2    if type(flag) is not bool:
3        raise TypeError("flag must be a bool")
4    return flag

That rejects values such as 1 and 0, which are integers even though they behave like booleans in conditions.

Common Pitfalls

The biggest mistake is writing if not value: when you really mean "if the value is missing". That condition will also treat 0, False, empty strings, and empty containers as missing.

Another common issue is using is True and is False when plain truthiness would be simpler and more Pythonic. Reserve explicit boolean identity checks for places where you genuinely need to distinguish exact booleans from other truthy or falsy values.

People also forget that booleans are a subclass of integers in Python. That means True == 1 and False == 0, which is another reason to use identity and type checks carefully when semantics matter.

Finally, if user input arrives as text such as "false" or "0", convert it before branching. Non-empty strings are truthy, so raw text values can be very misleading in conditions.

Small helper functions for optional flags can be worth it. They make intent explicit and keep business logic from repeating the same conditional edge cases everywhere.

Summary

  • Use is None and is not None for missing-value checks.
  • Use plain truthiness checks only when any truthy or falsy value is acceptable.
  • Use explicit is True and is False only when tri-state logic or strict boolean semantics matter.
  • Do not confuse falsy values such as 0 or "" with None.
  • Validate input early when code depends on exact boolean meaning.

Course illustration
Course illustration