The not keyword is Python's logical negation operator. It returns True when its operand is falsy and False when the operand is truthy, and it always returns a real boolean. Use it to invert conditions, to test for empty containers, and in combinations such as not in and is not.
Python decides what is "falsy" with truth value testing, so not works on any object, not just booleans. For example, not [] is True and not "hello" is False.
not evaluates a valuenot in conditionsnot with and and ornot in and is not operatorsnot x versus x is Nonenot versus the bitwise inverse ~not evaluates a valueThe syntax is not expression. Python converts the expression to a boolean using its truth value, then flips it.
The values that count as falsy are False, None, zero of any numeric type, empty sequences and collections ("", [], (), {}, set(), range(0)), and objects whose __bool__ returns False or whose __len__ returns 0. Everything else is truthy.
not in conditionsA very common use is checking for an empty collection or string. It is the idiomatic Python alternative to comparing the length to zero:
not with and and orThe not operator has higher precedence than and and or, but lower than comparison operators. So not a == b means not (a == b). Use parentheses when the intent is not obvious.
Here temperature > 30 is False and is_raining is True, so the or group is True. Applying not gives False, and the else branch runs.
De Morgan's laws help simplify these expressions:
not (a or b) is the same as (not a) and (not b).not (a and b) is the same as (not a) or (not b).not in and is not operatorsPython has two dedicated forms that read better than wrapping a test in not.
Write x not in y instead of not x in y, and x is not None instead of not x is None. They are equivalent, but the first forms are the style recommended by PEP 8.
not x versus x is NoneChecking if not data: treats None, 0, "" and [] the same. That is often wrong when 0 or an empty list is a valid value.
If zero is a valid count, test for None explicitly:
not versus the bitwise inverse ~The not operator works on truth values and returns a boolean. The ~ operator inverts the bits of an integer. They are unrelated.
Also note that not cannot be overloaded with a dunder method. To control the result for your own classes, define __bool__ or __len__.
not always return a boolean?Yes. Unlike and and or, which return one of their operands, not always returns True or False.
not x and x == False?not x uses truthiness, so not 0 is True. x == False compares values, and it is flagged by linters. Use if not x or if x is False depending on what you mean.
not the same as ! in other languages?It plays the same role as ! in C, Java and JavaScript. Python has no ! operator for logical negation, although != is used for inequality.
not not x?Yes, and not not x converts a value to a boolean, though bool(x) is clearer.
not negates the truth value of its operand and always returns a boolean. Rely on truthiness for empty collections, prefer not in and is not for readability, use is None when 0 or empty values are valid, and add parentheses when mixing not with and and or.