Page 88 - TP_Modular_V2.1_Class6
P. 88

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 operands are    (x < 5) and (x < 10)         TRUE
                                        true.
                       or        OR     It returns true, if one of the operands   (x < 5) or (x < 2)          TRUE
                                        is true.
                      not       NOT     It reverses the result, returns false, if   not [(x < 5) and (x < 10)]  FALSE
                                        the result is true or vice versa.

                  Program 3: To perform all the logical operators
                       Program3.py

                    File  Edit  Format   Run   Options   Window     Help
                    #Program  to compute  the  difference
                    between two numbers entered by the user
                    a = True
                    b = False
                                                                                     Output
                                                                                   False
                    print (a and b)
                    print (a or b)                                                 True
                    print (not a)                                                  False
                    print (not b)                                                  True




                  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:

                   Operator      Name                       Description                        Example       Output
                                                                                            (x=8 and y=6)
                      ==          Equal     It  checks  if  the  values  of  two  operands      x == y       FALSE
                                            are  equal  or  not.  If  yes,  then  the  condition
                                            becomes true.
                       !=       Not equal   It checks if the values of two operands are         x != y       TRUE
                                            equal or not. If the values are not equal, then
                                            the condition becomes true.
                       >         Greater    It checks if the value of left operand is greater    x > y       TRUE
                                  than      than the value of right operand. If yes, then
                                            the condition becomes true.
                       <        Less than   It checks if the value of left operand is less       x < y       FALSE
                                            than the value of right operand. If yes, then
                                            the condition becomes true.


                   86       Modular (Ver. 2.1)-VI
   83   84   85   86   87   88   89   90   91   92   93