Page 95 - 2617_JSSPS_C-8
P. 95

Logical Operators
                 Logical operators are used to evaluate conditions and make decisions on the basis of results. They are
                 used on conditional statements and return either True or False. Python supports the following logical
                 operators:

                  Operator Name                       Description                        Example (x=2)         Output

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


                  Program 5: To compute the difference between two numbers entered by the user


                     Program5.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)
                                                                              True
                   print (a or b)
                   print (not a)                                              False
                   print (not b)                                              True




                           Interdisciplinary Learning                                                   Lab Activity


                    Write a program in Python to calculate the area of a square.






                 PRECEDENCE OF OPERATORS

                 Precedence  of  operators determines  the  order  in  which  the  operators are executed.  The  operator
                 precedence in Python is listed in the given table. The highest precedence is at the top. The following
                 table shows the precedence of operators in Python:

                                       Operator                                 Name
                             ()                            Parenthesis
                             **                            Exponent
                             *, /, %, //                   Multiplication, Division, Modulo, Floor Division

                             +, –                          Addition, Subtraction
                             ==, !=, >, <, >=, <=          Comparison
                             =, +=, -=, *=, /=, %=, **=, //=  Assignment






                                                                                           Introduction to Python   93
   90   91   92   93   94   95   96   97   98   99   100