Page 136 - Trackpad_V4.0_c6_Flpbook
P. 136

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

                  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
                   not      NOT    It reverses the result, returns false, if the   not [(x < 5) and (x < 10)]  False
                                   result is true or vice versa.

                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)
                print (a or b)                                              True
                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





                134     Premium Edition-VI
   131   132   133   134   135   136   137   138   139   140   141