When I'm trying to check whether a list is empty or not using python's not
operator it is behaving in a weird manner.
I tried using the not
operator with a list to check whether it is empty or not.
>>> a = []
>>> not (a)
True
>>> not (a) == True
True
>>> not (a) == False
True
>>> True == False
False
The expected output for not (a) == False
should be False.
==
has higher precedence than not
. not (a) == False
is parsed as not (a == False)
.
This is working as expected. Parenthesis added below to clarify how this is being executed:
not (a == True)
# True
not (a == False)
# True
An empty list, a = []
, evaluates to False
in boolean expressions, but it does not equal False
or True
. Your expressions in the middle are testing whether it is equal to False
or True
, so they both evaluate to False
, and not False
is True
.
You can add parenthesis like below to get what you expect:
(not a) == True
# True
(not a) == False
# False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With