Not in Python: Boolean Operator and Membership Tests

When checking whether a Python value meets a condition, the expression depends on the question being asked. The not operator reverses a truth test, and not in checks whether a value is absent from a container.

I’ll show how to choose the right negative test and explain what happens with empty values and None.

TL;DR: Choose the right negative test

Choose the operator that matches the question: negate a condition, test membership, compare values, or check identity.

  • Use not items for a truth test on a collection.
  • Use value not in container for absence and != for unequal values.
  • Use value is not None when checking specifically for the None singleton.

What Is the not Operator in Python?

not tests the truth value of one expression and returns the opposite Boolean result. Python evaluates an object’s truth value when a condition needs a Boolean result.

The result of not is always True or False, even when its operand is a list or string. That differs from and and or, which can return one of their operands. For more context, see AskPython’s Python AND operator guide.

How to Use not and not in Step by Step

Python uses separate operators for a false condition, missing membership, unequal values, and object identity. This tutorial runs each form and shows how syntax and operand types affect the result.

Step 1: Negate a condition with not

Comparison binds more tightly than not, so not 3 == 4 means not (3 == 4). Output: not (3 == 4) is True.

Step 2: Check absence with not in

The output is True because the empty list has no matching member.

items = []
print("'pear' not in items:", "pear" not in items)
'pear' not in items: True

Strings search for substrings, while lists and tuples search their elements. Sets check members, and dictionaries check keys. AskPython’s Python conditionals examples show where this test fits in an if statement.

Step 3: Compare values with !=

For these integers, 3 != 4 evaluates to True. A custom type may define its inequality comparison separately from equality.

Step 4: Test whether a value is the None singleton

The identity expression evaluates to False. Zero and empty containers are distinct from the None singleton.

Terminal output comparing Python not, not in, inequality, and identity
The example prints results for four distinct negative tests.
items = []
print("not items:", not items)
print("items == False:", items == False)
print("'pear' not in items:", "pear" not in items)
print("not (3 == 4):", not (3 == 4))
print("3 != 4:", 3 != 4)
print("None is not None:", None is not None)

Edge Cases: Empty Values, None, and Precedence

The table distinguishes an empty collection from a missing-value marker and an absent member.

QuestionUseReason
Is this collection empty?not itemsEmpty collections are falsy.
Was no value supplied?value is NoneTests identity with the None singleton.
Is the value missing from a container?value not in itemsPerforms a membership test.

A numeric zero also makes if not value true, so use the None identity check when zero is valid input and only None means missing.

Array libraries can define truth testing differently. NumPy rejects the truth test of a multi-element array as ambiguous because its elements may disagree. Use an explicit element-wise operation or a size/any/all check from that library instead of assuming a built-in list rule applies.

Parentheses make a compound test clear. The expression not a == b means not (a == b), while a == not b is invalid syntax.

The tilde operator performs bitwise inversion rather than a Boolean truth test. Python documents Boolean inversion as deprecated, and its integer result differs from logical negation. The standard library function operator.not_() applies truth testing as a function.

In a conditional statement, the test controls which indented suite runs. Place the expression after if, add a colon, and put the intended branch on the next indented line. The same Boolean result can guard an if or an else branch.

items = []
if "pear" not in items:
    print("Add the missing fruit")
else:
    print("Already present")
Add the missing fruit

The membership condition is true for this empty list, so Python runs the first indented suite.

Conclusion

Choose an expression by the relationship you need to test. The examples above show how Python evaluates each one.

For the authoritative details, read Python’s truth value testing documentation and its operator precedence reference.

FAQ

These answers distinguish common negative expressions that look similar.

How do you write not equal in Python?

Use != for value inequality, such as 3 != 4. Use is not only for object identity checks such as value is not None.

What does not in mean in Python?

The not in comparison returns True when the left-hand value is absent from the right-hand container.

Why does not on an empty list return True?

An empty list has a false truth value, so not reverses that result to True. The list is not equal to False.

Tanvi Bugdani
Tanvi Bugdani
Articles: 63