IS NOT
What Is the IS NOT Operator?
- The
is notoperator in Python checks if two variables do not point to the same memory location.
How to Use It:
The basic way to use the
is notoperator is:pythonresult = variable1 is not variable2variable1andvariable2are the variables you want to compare.resultwill beTrueif both variables do not point to the same memory location, andFalseif they do.
Examples and Exercises:
Using the IS NOT Operator:
pythona = [5, 8] b = [5, 8] c = a if a is not b: print('a is not b') # Output: a is not b else: print('a is b') if a is not c: print('a is not c') else: print('a is c') # Output: a is c
Explanation:
- In the above example, the contents of
aandbare the same, butaandbare different objects, soa is not breturnsTrue. - When
ais assigned toc,cpoints to the same object in memory asa, soa is not creturnsFalse.
Summary:
- The
is notoperator in Python helps you check if two variables do not point to the same memory location. It's useful for understanding object references and memory allocation. Try the example to see how it works!