How to Reverse a Condition in Python with not
Python reverses a condition with the word not: if not is_banned: runs when is_banned is false. Other languages write !, which Python doesn't have. not also works on lists, text and numbers, because every Python value counts as either true or false.
Syntax
not <expression>
| Part | What it does |
|---|---|
not | Gives True when <expression> is false, and False when it's true. |
<expression> | Anything with a truth value: a variable, a comparison such as age >= 18, or a call. |
The Python Language Reference, on Boolean operations: "The operator not yields True if its argument is false, False otherwise."
Which values are false is listed under Truth Value Testing: "constants defined to be false: None and False", "zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)" and "empty sequences and collections: '', (), [], {}, set(), range(0)". Everything else is true.
The Example
The example uses not five ways: on a flag that's False, on an empty shopping cart, on the text "Ada", on the number 0, and on the comparison age >= 18 for someone who is 15.
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Write the Code
Create a file named main.py:

is_banned = False
print(not is_banned)
cart = []
if not cart:
print("The cart is empty")
print(not "Ada")
print(not 0)
age = 15
if not age >= 18:
print("Too young")
Step 2: Run It
uv run python main.py
uv run python main.py runs the file with the Python that uv manages.
True
The cart is empty
False
True
Too young

The command is the same in zsh, bash and PowerShell.
What Happened
not is_bannedprintedTrue, becauseis_bannedisFalse.if not cart:printed its line. An empty list is false, sonot cartis true. This is the usual way to test for an empty list.not "Ada"printedFalse, andnot 0printedTrue. Text with characters in it is true, and zero is false.if not age >= 18:printedToo young.notapplies to the whole comparison, so it meansnot (age >= 18). The documentation explains why: "not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing ! as in other languages: if !is_banned: | SyntaxError: invalid syntax | if not is_banned: |
Testing for a missing value with if not value: when 0, "" or [] are allowed | Those values are false too, so the check catches them as well | if value is None: |
Writing x == not y | SyntaxError: invalid syntax | Add brackets, x == (not y), or compare with != |
Expecting not "False" to be True | Any text with characters in it is true, including "False", so it's False | Compare the text: value == "False" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
SyntaxError: invalid syntax pointing at ! | ! on its own isn't an operator in Python | Write not |
An if not value: branch ran for 0 or an empty string | Both count as false | Test value is None when only a missing value should match |
FAQ
Does not always give True or False?
Yes. not [] gave True, and its type is bool. and and or are different: the documentation says they "return the last evaluated argument", which can be any value.
Is not x == y the same as x != y?
Yes. not has a lower priority than ==, so not x == y means not (x == y). x != y says the same thing more directly.
Should I write not x is None or x is not None?
x is not None. Both give the same result, and PEP 8, Python's style guide, says: "Use is not operator rather than not ... is. While both expressions are functionally identical, the former is more readable and preferred".
How do I write "not in"?
item not in items is its own operator. It's True when the value isn't in a list or a string, or isn't a key of a dictionary.
Conclusion
not turns true into false and false into true, for comparisons, flags and any other value. Remember that None, 0, empty text and empty collections are all false, and test is None when only a missing value should count.
Additional Resources
- Boolean operations, in the Python Language Reference.
- Truth Value Testing, on which values count as false.
