What's the difference between If not (variable) and if (variable) == false?

Question:

I’m learning Python and I just started learning conditionals with booleans

I am very confused though as to the specific topic of "If Not". Could someone please explain to me the difference between :

x = False

if not x:
   print("hello")

if x == False:
   print("hello")

When testing this code on a Python compiler, I receive "hello" twice. I can assume this means that they both mean the same thing to the computer.

Could someone please explain to me why one would use one method over the other method?

Asked By: WillPak_Alt

||

Answers:

In one case you are checking for equality with the value "False", on the other you are performing a boolean test on a variable. In Python, several variables pass the "if not x" test but only x = False passes "if x == False".

See example code:

x = [] # an empty list

if not x: print("Here!")
# x passes this test

if x == False: print("There!")
# x doesn't pass this test
Answered By: Luiz F. Bianchi

Try it with x = None: not x would be True then and x == False would be False. Unlike with x = False when both of these are True. not statement also accounts for an empty value.

Answered By: Toni Sredanović

It dependsâ„¢. Python doesn’t know what any of its operators should do. It calls magic methods on objects and lets them decide. We can see this with a simple test

class Foo:
  
    """Demonstrates the difference between a boolean and equality test
    by overriding the operations that implement them."""
    def __bool__(self):
        print("bool")
        return True

    def __eq__(self, other):
        print("eq", repr(other))
        return True

x = Foo()

print("This is a boolean operation without an additional parameter")
if not x:
    print("one")

print("This is an equality operation with a parameter")
if x == False:
    print("two")

Produces

This is a boolean operation without an additional parameter
bool
This is an equality operation with a parameter
eq False
two

In the first case, python did a boolean test by calling __bool__, and in the second, an equality test by calling __eq__. What this means depends on the class. Its usually obvious but things like pandas may decide to get tricky.

Usually not x is faster than x == False because the __eq__ operator will typically do a second boolean comparison before it knows for sure. In your case, when x = False you are dealing with a builtin class written in C and its two operations will be similar. But still, the x == False comparison needs to do a type check against the other side, so it will be a bit slower.

Answered By: tdelaney

There are already several good answers here, but none discuss the general concept of "truthy" and "falsy" expressions in Python.

In Python, truthy expressions are expression that return True when converted to bool, and falsy expressions are expressions that return False when converted to bool. (Ref: Trey Hunner’s regular expression tutorial; I’m not affiliated with Hunner, I just love his tutorials.)

Falsy stuff:

What’s important here is that 0, 0.0, [], None and False are all falsy.

When used in an if statement, they will fail the test, and they will pass the test in an if not statement.

Truthy stuff:

Non-zero numbers, non-empty lists, many objects (but read @tdelaney’s answer for more details here), and True are all truthy, so they pass if and fail if not tests.

Equality tests

When you use equality tests, you’re not asking about the truthiness of an expression, you’re asking whether it is equal to the other thing you provide, which is much more restrictive than general truthiness or falsiness.

EDIT: Additional references

Here are more references on "Truthy" and "Falsy" values in Python:

Answered By: joanis