Page 475 - ComputerScience_Class_11
P. 475

Output

                  Logical AND Result: True
                  Logical OR Result: True
                  Logical NOT Result: True





                 12.2.5 Identity Operator
                 Identity operators are used to determine whether two variables point to the same object in memory. They do not
                 compare the actual values of the objects; instead, they check if both variables refer to the exact same location. The
                 result of an identity operation is always either True or False.
                 Python has two identity operators:

                   Operator                                           Description

                       is       Returns True if both variables refer to the same object.
                     is not     Returns True if both variables refer to different objects.

                 Program 8: To demonstrate the use of Identity operators.

                     Program 8.py
                  File  Edit  Format   Run   Options   Window     Help

                  # Assigning a list to variable a
                  a = [10, 20, 30]


                  # Assigning the same reference of a to variable b
                  b = a


                  # Creating a new list with the same values and assigning to variable c
                  c = [10, 20, 30]

                  # Using ‘is’ operator to check if a and b refer to the same object
                  print("a is b:", a is b)


                  # Using ‘is’ operator to check if a and c refer to the same object
                  print("a is c:", a is c)

                  # Using ‘is not’ operator to check if a and c refer to different objects
                  print("a is not c:", a is not c)




                     Output

                  a is b: True
                  a is c: False
                  a is not c: True





                                                                                           Operators and Expressions  473
   470   471   472   473   474   475   476   477   478   479   480