Page 97 - Information_Practice_Fliipbook_Class11
P. 97

4.4.5 Identity Operators

            Identity operators are typically used for checking the data type of an object. The identity operator returns True if the
            operands on either side of the operator are the same object or same type, and False otherwise.
                                                    Table 4.5: Identity Operators
            (Assume that a, b, c have values  -2, None, "Hello", respectively)

             Operator          Explanation                          Example
             is                It yields True if the operands on either  >>> type(a) is int
                               side of the operator are the  same object      True
                               or same type, and False otherwise. >>> type(c) is list
                                                                        False
                                                                    >>> a is b
                                                                        False
                                                                    >>> v = a
                                                                    >>> a is v
                                                                        True
             is not            It yields True if  the operands on  >>> type(b) is not int
                               either side of the operator are different      False
                               objects or  different types, and False  >>> type(c) is not list
                               otherwise.                               True
                                                                    >>> a is not b
                                                                        True
                                                                    >>> a = b
                                                                    >>> a is not b
                                                                        False

            4.4.6 Membership Operators

            These operators are used to check whether a particular value is a member of a given sequence and return either True
            or False.
                                                  Table 4.6: Membership Operators

             Operator        Explanation                          Examples

             in              The expression a in b yields True  >>> myList = [0, -4, 5, 8, 10, 50, 4]
                             if  the  object  a  is  included  in  b and  >>> 5 in myList
                             False otherwise.                         True

                                                                  >>> 100 in myList
                                                                      False
             not in          The expression a not in b yields  >>> myList = [0, -4, 5, 8, 10, 50, 4]
                             True if the object a is not included in  >>> 5 not in myList
                             b and False otherwise.                   False

                                                                  >>> 100 not in myList
                                                                      True







                                                                                        Data Types and Operators  83
   92   93   94   95   96   97   98   99   100   101   102