Page 73 - TP_Prime_V2.2_Class7
P. 73

Logical Operators
                 Logical operators are used to evaluate multiple comparisons and return a Boolean result of

                 either True or False. There are three primary logical operators:

                  Operator Name Description                                     Example (x=2)               Output
                     and       AND  It returns true, if both operands           (x < 5) and (x < 10)         TRUE
 Prime (Ver. 2.2)-VII  not     NOT It reverses the result, returns false,  not [(x < 5) and (x < 10)] FALSE                 MORE ON PYTHON
                                       are true.
                                                                                (x < 5) or (x < 2)
                                                                                                             TRUE
                                       It returns true, if one of the
                                OR
                      or
                                       operands is true.
                                       if the result is true or vice versa.







 70              Let’s see an example of the above operators:                                                             71











                                                        Using Logical Operators
                 Operator Precedence

                 A single expression can contain multiple operators. When we have more than one operators
                 in  an  expression,  the  Python  interpreter  determines  the  order  of  evaluation  based  on

                 predefined rules of precedence and associativity.
                 Precedence: This refers to the priority assigned to each operator in Python.

                 Associativity:  When  two  operators  have  the  same  level  of  precedence,  associativity
                 determines  the  order  in  which  they  are  evaluated.  In  Python,  most  operators  are  left

                 associative, meaning they are evaluated from "left to right". However, some operators, like
                 exponentiation (**), are right associative, meaning they are evaluated from "right to left".

                      ()                      Parenthesis

                      **                      Exponential
                      (+,  –)                 Unary

                      *, /, //, %             Multiplication, Division, Floor Division, Modulus
                      +, -                    Addition,  Subtraction

                      >, <, <=,  >=,  ==,  !=  Relational Operators

                      NOT, AND, OR            Boolean/Logical Operators
   68   69   70   71   72   73   74   75   76   77   78