not — Expert Examples
Logical NOT operator; inverts a boolean value
not bytecode: UNARY_NOT
How 'not' compiles vs 'is not' and 'not in'.
python
import dis def unary_not(x): return not x def is_not(x): return x is not None def not_in(x, y): return x not in y print("not x:") dis.dis(unary_not) print("\nx is not None:") dis.dis(is_not) print("\nx not in y:") dis.dis(not_in) # not x -> UNARY_NOT (evaluates x, then negates) # is not -> IS_OP 1 (single opcode, no negation step) # not in -> CONTAINS_OP 1 (single opcode, no negation step)
'not' compiles to UNARY_NOT applied after evaluation. But 'is not' and 'not in' are single opcodes (IS_OP 1 and CONTAINS_OP 1) — they don't evaluate then negate, they do the negated comparison directly.
Want to try these examples interactively?
Open Expert Playground