Page 119 - 2622_Delhi Police Public School_C-6
P. 119

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)
                                                                              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






                                                                                                         Python    117
   114   115   116   117   118   119   120   121   122   123   124