Page 25 - tp_Modula_v2.0
P. 25

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       (x < 5) and (x < 10)             TRUE
                                       are true.

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


                 Program 3: To perform all the logical operations.

                     Program3.py
                  File  Edit  Format  Run   Options   Window    Help

                  #examples of logical operators
                  a=True
                  b=False
                  print(a and b)
                  print(a or b)
                  print(not a)
                  print(not b)




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

                     Output

                  False
                  True
                  False
                  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:
                                                                                           Example
                  Operator       Name                     Description                                      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.


                                                                            Data Types and Operators in Python    23
   20   21   22   23   24   25   26   27   28   29   30