IS NOT

What Is the IS NOT Operator?

  • The is not operator 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 not operator is:

    python
    result = variable1 is not variable2
    
    • variable1 and variable2 are the variables you want to compare.
    • result will be True if both variables do not point to the same memory location, and False if they do.

Examples and Exercises:

  1. Using the IS NOT Operator:

    python
    a = [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 a and b are the same, but a and b are different objects, so a is not b returns True.
  • When a is assigned to c, c points to the same object in memory as a, so a is not c returns False.

Summary:

  • The is not operator 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!