I was quite surprised when
[] is not []
evaluated to True
.
What is happening in this code? What really not
and is
statements are doing?
is means is same instance. It evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
Python supports multiple independent conditions in the same if block. Say you want to test for one condition first, but if that one isn't true, there's another one that you want to test. Then, if neither is true, you want the program to do something else. There's no good way to do that using just if and else .
Here we'll study how can we check multiple conditions in a single if statement. This can be done by using 'and' or 'or' or BOTH in a single statement. and comparison = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn't check the second one.
This is because they both have a function defined called print that takes in a string argument and prints it. Python2 also allowed you to make a statement to print to standard out without calling a function.
a is not b
is a special operator which is equivalent to not a is b
.
The operator a is b
returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is
returns False (and therefore is not
returns True).
is
is the identity comparison.
==
is the equality comparison.
Your statement is making two different lists and checking if they are the same instance, which they are not. If you use ==
it will return true and because they are both empty lists.
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