Page 384 - Ai_417_V3.0_C9_Flipbook
P. 384

Logical Operators

              Logical operators are used to combine one or more conditional statements and returns either True or False
              based on the evaluation of the conditions. Following logical operators are provided by Python:

                      Name          Symbol                 Purpose                      Example            Output
                                               Returns True if both the operands
                       AND            and                                            5 < 10 and 2 < 5        True
                                               are True otherwise False.
                                               Returns True if either the operands
                       OR              or                                             5 > 10 or 2 < 5        True
                                               is True otherwise False.
                                                                                   not(5 > 10 or 2 < 5)     False
                                               Returns True if the operand is False.
                       NOT            not                                              not(10 < 5)           True
                                               or vice versa. It reverses the result.
                                                                                        not(True)           False




                           Brainy Fact

                   In case of OR operator, the second condition is checked only if the first is False otherwise it ignores
                   the second operand.




              Assignment Operator
              Assignment operator is used to assign a value to a variable or a constant. = sign is used as an assignment
              operator in Python. For example,
                 rollno = 12
                myname = "Alisha"
                mymarks = 92.5
              Augmented Assignment Operators
              Augmented assignment operators are those operators which take the value of the operand on the right side,
              perform an operation and assign the result to the operand on the left side. These operators are also known as
              shorthand assignment operators. For example, if a=10 then a+=5 is the same as a=a+5.
              Python provides the following augmented assignment operators:

                     Symbol                          Purpose                         Example            Output
                                                                                       a = 5

                                   Adds  first  and  then  assigns  the  result  to  the   b = 10
                        +=
                                   left-hand side operand.                            a += b               15
                                                                                       a += 5              20
                                                                                       a = 5
                                   Subtracts first and then assigns the result to the
                        -=                                                             b = 10
                                   left-hand side operand.
                                                                                       a -= b              -5
                                   Multiplies first and then assigns the result to the   a = 5
                        *=
                                   left-hand side operand.                             a *= 5              25
                                   Divides first and then assigns the result to the    b = 10
                        /=
                                   left-hand side operand.                             b /= 5             2.0



                    382     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   379   380   381   382   383   384   385   386   387   388   389