not

Flip the truth — and the idiomatic emptiness check: `if not items:`.

Logical operatorPython 1.0+Live demo
Common call
if not items:
Returns
always a real True/False
Replaces
falsy: None, False, 0, "", [], {}, set()
Watch out
`not a == b` parses as `not (a == b)` — == binds tighter
not aaThe value to truth-test and negate.type: Any · required
bool

Demo

Live evaluation
Try:
Inputs
aAnyempty = falsy
Output
not 'x'
False

Any truthy value → False, any falsy value → True. This is also the one logical operator that guarantees a genuine bool result.

Operands

NameTypeRequiredDescription
aAnyyesThe value to truth-test and negate.

Return value

boolTrue when a is falsy, False when truthy — unlike and/or, ALWAYS an actual bool.

Common patterns

Emptiness checks
The idiomatic way to test for empty containers.
if not items:
    return
Boolean coercion
Double negation is a terse bool() — bool() reads better.
has_data = bool(rows)   # clearer than: not not rows

Examples

1. Negate truthy
not "x"
Returns
False
2. Negate falsy
not ""
Returns
True
3. Empty list is falsy
not []
Returns
True

Pitfalls

1. Precedence with comparisons
not binds LOOSER than ==, so `not a == b` is `not (a == b)`.
Reads wrong
not x == 5   # looks like (not x) == 5
actually not (x == 5)
Say what you mean
x != 5
clearer spelling
2. `not x in y` vs `x not in y`
They mean the same, but the dedicated operator reads better and is the convention.
Awkward
if not key in d:
works, non-idiomatic
Fix
if key not in d:
idiomatic

When to use

Use it
  • Emptiness / absence checks
  • Flipping a condition for guard clauses
Reach for something else
  • Double negation for coercion → bool()
  • `not a == b` → a != b
  • `not a is b` → a is not b

Notes

Complexity
O(1) plus the truth test (__bool__ or __len__)
Return
bool — always
CPython impl
UNARY_NOT opcode → PyObject_IsTrue
Memory
No allocation
Thread-safe
Yes

FAQ

None, False, numeric zeros (0, 0.0, 0j), empty sequences and collections ("", [], (), {}, set(), range(0)), and any object whose __bool__ returns False or __len__ returns 0.

History

1.0
Core operator from the beginning.