not
In Python, the not
keyword is a logical operator that inverts the truth value of the expression returning True
or False
. It’s also used in negated membership tests with the not in
operator and negated identity checks with the is not
the operator.
Python not
Keyword Examples
Here’s a quick example to demonstrate how to use the not
keyword:
>>> not True
False
>>> not False
True
>>> not (5 > 3)
False
>>> not (2 == 2)
False
In these examples, the not
keyword takes a single Boolean expression and flips its value. The not True
expression evaluates to False
, while not False
evaluates to True
. Similarly, not (5 > 3)
evaluates to False
because 5 > 3
is True
, and not (2 == 2)
evaluates to False
because 2 == 2
is True
.
Python not
Keyword Use Cases
- Negating Boolean values in logical expressions
- Simplifying conditionals in
if
,elif
, andwhile
statements - Improving code readability by clarifying the intent of a condition
Related Resources
Tutorial
Using the "not" Boolean Operator in Python
In this step-by-step tutorial, you'll learn how Python's "not" operator works and how to use it in your code. You'll get to know its features and see what kind of programming problems you can solve by using "not" in Python.
For additional information on related topics, take a look at the following resources:
- Python Booleans: Use Truth Values in Your Code (Tutorial)
- Using the Python not Operator (Course)
- Python Booleans: Leveraging the Values of Truth (Course)