Skip to main content

How to Reverse a Condition in Python with not

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
not <expression>
PartWhat it does
notGives 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:

main.py, which prints not is_banned for a False flag, prints The cart is empty when not cart is true for an empty list, prints not of the string Ada and not 0, and prints Too young when not age >= 18 is true for an age of 15.

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​

Terminal
uv run python main.py

uv run python main.py runs the file with the Python that uv manages.

Output
True
The cart is empty
False
True
Too young

A zsh terminal where uv run python main.py prints True, The cart is empty, False, True and Too young.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • not is_banned printed True, because is_banned is False.
  • if not cart: printed its line. An empty list is false, so not cart is true. This is the usual way to test for an empty list.
  • not "Ada" printed False, and not 0 printed True. Text with characters in it is true, and zero is false.
  • if not age >= 18: printed Too young. not applies to the whole comparison, so it means not (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​

MistakeWhat happensDo this instead
Writing ! as in other languages: if !is_banned:SyntaxError: invalid syntaxif not is_banned:
Testing for a missing value with if not value: when 0, "" or [] are allowedThose values are false too, so the check catches them as wellif value is None:
Writing x == not ySyntaxError: invalid syntaxAdd brackets, x == (not y), or compare with !=
Expecting not "False" to be TrueAny text with characters in it is true, including "False", so it's FalseCompare the text: value == "False"

Troubleshooting​

SymptomLikely causeFix
SyntaxError: invalid syntax pointing at !! on its own isn't an operator in PythonWrite not
An if not value: branch ran for 0 or an empty stringBoth count as falseTest 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​