python
boolean
negation
programming
tutorial

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.

Browse interview questions

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

python
1a = True
2b = not a
3print(b)  # False
4
5c = False
6d = not c
7print(d)  # True

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

python
1is_logged_in = False
2
3if not is_logged_in:
4    print("Please log in")  # This runs
5
6items = []
7
8if not items:
9    print("List is empty")  # This runs (empty list is falsy)
10
11name = "Alice"
12
13if not name:
14    print("No name")  # Does NOT run (non-empty string is truthy)

Truthiness Rules

not works on any value, not just Booleans. Python first evaluates the truthiness of the value, then negates it:

python
1# Falsy values (not x returns True):
2not None       # True
3not False      # True
4not 0          # True
5not 0.0        # True
6not ""         # True
7not []         # True
8not {}         # True
9not set()      # True
10
11# Truthy values (not x returns False):
12not True       # False
13not 1          # False
14not -1         # False
15not "hello"    # False
16not [1, 2, 3]  # False
17not {"a": 1}   # False

Negating Comparison Results

python
1x = 5
2
3# Direct comparison
4if x != 10:
5    print("x is not 10")
6
7# Using not with comparison
8if not (x == 10):
9    print("x is not 10")
10
11# not with in operator
12fruits = ["apple", "banana"]
13if "cherry" not in fruits:
14    print("No cherry")
15
16# not with is
17value = None
18if value is not None:
19    print("Has a value")

Prefer !=, not in, and is not over wrapping comparisons with not() — they are more readable.

Toggling a Boolean

python
1flag = True
2
3# Toggle the flag
4flag = not flag
5print(flag)  # False
6
7flag = not flag
8print(flag)  # True
9
10# Common pattern in loops
11active = False
12for _ in range(4):
13    active = not active
14    print(active)
15# True, False, True, False

The ~ Operator (Bitwise NOT)

~ is the bitwise NOT operator. It does NOT negate Booleans the way you might expect:

python
1# Bitwise NOT on integers
2x = 5       # Binary: 0101
3y = ~x      # Binary: ...1010 (two's complement)
4print(y)    # -6
5
6# On Booleans (True=1, False=0)
7print(~True)   # -2 (not False!)
8print(~False)  # -1 (not True!)
9
10# Use not for Boolean negation, not ~
11print(not True)   # False
12print(not False)  # True

~ is useful for bitwise operations and NumPy array masking, not for Boolean logic.

NumPy Boolean Negation

python
1import numpy as np
2
3arr = np.array([True, False, True, False])
4
5# Use ~ for element-wise negation on NumPy arrays
6negated = ~arr
7print(negated)  # [False  True False  True]
8
9# not does NOT work on NumPy arrays
10# not arr  # ValueError: The truth value of an array is ambiguous
11
12# np.logical_not also works
13negated = np.logical_not(arr)
14print(negated)  # [False  True False  True]

Pandas Boolean Negation

python
1import pandas as pd
2
3df = pd.DataFrame({"active": [True, False, True, False]})
4
5# Use ~ for Series negation
6df["inactive"] = ~df["active"]
7print(df)
8#    active  inactive
9# 0    True     False
10# 1   False      True
11# 2    True     False
12# 3   False      True
13
14# Filter with negation
15inactive_rows = df[~df["active"]]

operator.not_ Function

python
1import operator
2
3# Functional form of not
4result = operator.not_(True)
5print(result)  # False
6
7# Useful with map/filter
8values = [True, False, True, False]
9negated = list(map(operator.not_, values))
10print(negated)  # [False, True, False, True]

Boolean Algebra

python
1# De Morgan's Laws in Python
2a, b = True, False
3
4# not (a and b) == (not a) or (not b)
5print(not (a and b))          # True
6print((not a) or (not b))     # True
7
8# not (a or b) == (not a) and (not b)
9print(not (a or b))           # False
10print((not a) and (not b))    # False
11
12# Double negation
13print(not not True)    # True
14print(not not False)   # False
15print(not not [1, 2])  # True (converts to bool)

Common Pitfalls

  • Using ~ instead of not for Booleans: ~True returns -2, not False. The ~ operator is bitwise NOT, which applies two's complement arithmetic. Always use not for Boolean negation.
  • Using not on NumPy arrays: not np.array([True, False]) raises ValueError because NumPy cannot determine the truth value of a multi-element array. Use ~array or np.logical_not(array) instead.
  • Operator precedence with not: not x == y is parsed as not (x == y), not (not x) == y. Use parentheses to make intent explicit: (not x) == y if that is what you mean.
  • Confusing not with !=: not x == y and x != y produce the same result, but x != y is clearer and more Pythonic. Use != for inequality checks, not not ==.
  • Double negation for type conversion: not not x converts any value to its Boolean equivalent, but bool(x) is the explicit and preferred way to convert to Boolean in Python.

Summary

  • Use not to negate a Boolean: not True returns False
  • not works on any value via Python's truthiness rules
  • Use ~ for bitwise negation and NumPy/Pandas array negation
  • Use not in and is not for readable negated membership and identity checks
  • Toggle a Boolean with flag = not flag
  • Prefer bool(x) over not not x for explicit Boolean conversion

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.