Page 90 - Dig_CodeAI_V2.1_Class_7
P. 90

Logical Operators

                  Logical operators are used to evaluate and decide. Python supports the following logical operators:


                   Operator Name        Description                                  Example (x=2)            Output

                   and         AND      It returns true, if both conditions are true. (x < 5) and (x < 10)    True

                   or          OR       It  returns true, if  one  of  the conditions  is  (x < 5) or (x < 2)  True
                                        true.
                   not         NOT      It reverses the result, returns false, if the  not [(x < 5) and (x < 10)] False
                                        result is true or vice versa.

                  Program: To perform all the logical operations.

















                  On running the above program, you will get the following output:
















                  Relational Operator

                  Relational operators are used to compare the value of the two operands and returns True or False
                  accordingly. The relational operators are described in the following table:

                                                                                             Example
                   Operator    Name         Description                                                      Output
                                                                                             (x=8 and y=6)

                       ==      Equal to     It  checks  if  the  values  of  two  operands  are  x == y      False
                                            equal or not. If the values are equal, then the
                                            condition becomes true.
                       !=      Not equal    It  checks  if  the  values  of  two  operands  are  x != y      True
                               to           equal or not. If the values are not equal, then
                                            the condition becomes true.




                       88    DigiCode AI (Ver. 2.1)-VII
   85   86   87   88   89   90   91   92   93   94   95