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.
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.
In cases like:
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.
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:
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 Noneandis not Nonefor missing-value checks. - Use plain truthiness checks only when any truthy or falsy value is acceptable.
- Use explicit
is Trueandis Falseonly when tri-state logic or strict boolean semantics matter. - Do not confuse falsy values such as
0or""withNone. - Validate input early when code depends on exact boolean meaning.

