Page 163 - Computer Science Class 11 Without Functions
P. 163

Table 7.4: Logical Operators (Assume the value of a, b, and c to be 10, 5, and -2
                                             respectively for computations in the table)
                Operator                 Explanation                                  Examples

             and             It yields  True if  both  the operands  >>> True and True
                             yield True, and False otherwise.        True
                                                                 >>> True and False
                                                                     False
                                                                 >>> a > b and b > c
                                                                     True
                                                                 >>> a < b and b > c
                                                                     False
                                                                 >>> a < b and b < c
                                                                     False
             or              It yields True if either  of the two  >>> True or True
                             operands  yield  True,  and  False       True
                             otherwise.                          >>> False or True
                                                                     True
                                                                 >>> False or False
                                                                     False
                                                                 >>> a > b or b > c
                                                                     True
                                                                 >>> a < b or b > c
                                                                     True
                                                                 >>> a < b or b < c
                                                                     False
             not             Not True yields False and Not  >>> not True
                             False yields True.                      False
                                                                 >>> not False
                                                                     True
                                                                 >>> not a > c
                                                                     False
                                                                 >>> test = 0
                                                                 >>> not test
                                                                     True
            7.5.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 7.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



                                                                                        Data Types and Operators  161
   158   159   160   161   162   163   164   165   166   167   168