not
The not keyword is a logical negation operator in Python. It returns the boolean opposite of its operand’s truthiness — True becomes False and vice versa.
Syntax
result = not expression
How It Works
Python’s not always returns a boolean value (True or False), unlike some languages where ! might return the original value. The operator evaluates the truthiness of its operand using the same rules Python uses throughout the language.
# Basic negation
print(not True)
# False
print(not False)
# True
# With non-boolean values - checks truthiness
print(not 0)
# True
print(not 1)
# False
print(not "")
# True
print(not "hello")
# False
print(not None)
# True
Truthiness Rules
Python considers these values falsy:
NoneFalse- Zero:
0,0.0,0j,Decimal(0),Fraction(0, 1) - Empty sequences:
"",[],(),{} - Objects with
__bool__()returningFalseor__len__()returning0
Everything else is truthy.
# Numbers
print(not 0) # True - zero is falsy
print(not 42) # False - non-zero is truthy
# Strings
print(not "") # True - empty string is falsy
print(not "x") # False - non-empty is truthy
# Collections
print(not []) # True - empty list is falsy
print(not [1]) # False - non-empty list is truthy
# None
print(not None) # True
Common Use Cases
Negating conditions
is_logged_in = False
if not is_logged_in:
print("Please log in")
# Please log in
user = None
if not user:
print("No user found")
# No user found
Double negation
Sometimes you need to convert a truthy/falsy value to an explicit boolean:
value = "hello"
# Using not not to get boolean
is_truthy = not not value
print(is_truthy)
# True
value = ""
is_truthy = not not value
print(is_truthy)
# False
# Modern alternative - bool()
is_truthy = bool(value)
print(is_truthy)
# False
Combining with other operators
x = 5
# not with comparison
print(not x > 10)
# True
# not with in
languages = ["python", "rust", "go"]
if "javascript" not in languages:
print("Not found")
# Not found
# not with isinstance
value = "hello"
if not isinstance(value, int):
print("Not an integer")
# Not an integer
Guard patterns
def greet(user):
if not user:
return "Hello, stranger!"
return f"Hello, {user}!"
print(greet("Alice"))
# Hello, Alice!
print(greet(""))
# Hello, stranger!
print(greet(None))
# Hello, stranger!
Precedence
The not operator has lower precedence than comparison operators but higher than and and or:
x = 5
# not binds tighter than and/or but looser than comparisons
result = not x > 0
print(result)
# False
# Parentheses can clarify intent
result = not (x > 0)
print(result)
# False
Operator precedence from highest to lowest:
not(unary)andor
# Demonstration
print(not True or True)
# True - (not True) is False, False or True is True
print(not (True or True))
# False - not True is False
Boolean Context
When not is used in a boolean context, the result is already a boolean, so no conversion happens:
result = not "hello"
if result:
print("This won't execute")
# (nothing printed - result is False)
if not not "hello":
print("This will execute")
# This will execute
See Also
- bool built-in — convert values to boolean explicitly
- and keyword — logical conjunction operator
- all built-in — return True if all elements are truthy