How do I get the opposite negation of a Boolean in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Use the not operator to negate a Boolean in Python. not True returns False, and not False returns True. For bitwise negation of integer flags, use the ~ operator. Python also supports negation on non-Boolean values through its truthiness rules — not first evaluates the truthiness of the operand, then returns the opposite Boolean.
The not Operator
not is a unary operator that returns the Boolean opposite of its operand. It always returns True or False, regardless of the input type.
Negation in Conditionals
Truthiness Rules
not works on any value, not just Booleans. Python first evaluates the truthiness of the value, then negates it:
Negating Comparison Results
Prefer !=, not in, and is not over wrapping comparisons with not() — they are more readable.
Toggling a Boolean
The ~ Operator (Bitwise NOT)
~ is the bitwise NOT operator. It does NOT negate Booleans the way you might expect:
~ is useful for bitwise operations and NumPy array masking, not for Boolean logic.
NumPy Boolean Negation
Pandas Boolean Negation
operator.not_ Function
Boolean Algebra
Common Pitfalls
- Using
~instead ofnotfor Booleans:~Truereturns-2, notFalse. The~operator is bitwise NOT, which applies two's complement arithmetic. Always usenotfor Boolean negation. - Using
noton NumPy arrays:not np.array([True, False])raisesValueErrorbecause NumPy cannot determine the truth value of a multi-element array. Use~arrayornp.logical_not(array)instead. - Operator precedence with
not:not x == yis parsed asnot (x == y), not(not x) == y. Use parentheses to make intent explicit:(not x) == yif that is what you mean. - Confusing
notwith!=:not x == yandx != yproduce the same result, butx != yis clearer and more Pythonic. Use!=for inequality checks, notnot ==. - Double negation for type conversion:
not not xconverts any value to its Boolean equivalent, butbool(x)is the explicit and preferred way to convert to Boolean in Python.
Summary
- Use
notto negate a Boolean:not TruereturnsFalse notworks on any value via Python's truthiness rules- Use
~for bitwise negation and NumPy/Pandas array negation - Use
not inandis notfor readable negated membership and identity checks - Toggle a Boolean with
flag = not flag - Prefer
bool(x)overnot not xfor explicit Boolean conversion
Related reading
- How do I get the parent directory in Python?
- How do I get the path of the current executed file in Python?
- How do I get the path of the Python script I am running in?
- How do I get the picture size with PIL?
- How do I get the row count of a Pandas DataFrame?
- How do I get time of a Python program's execution?
- How do I get user IP address in Django?
- How do I handle the window close event in Tkinter?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.