Page 279 - Ai_C10_Flipbook
P. 279

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. Logical operators used in Python are given below:

                   Name       Symbol               Purpose                          Example                   Output
                    AND         and      Returns True if both the  Num1=14
                                         operands are true.             Num2=10
                                                                        Num1<Num2 and Num1>20                  False

                     OR          or      Returns  True  if  either  the  Num1=14
                                         operands is true.              Num2=10

                                                                        Num1>Num2 or Num1<Num2                 True
                    NOT         not      Returns True if the operand is  not(5>10 or 2<5)                      False
                                         False and vice versa. It reverses  not(5<10)                          True
                                         the result.                    not(False)                             True

                 Assignment Operator


                 Assignment operator is used to assign a value to a variable or a constant. The '=' sign is used as an assignment
                 operator in Python.

                 [1]:   RollNo = 7
                        Name = "Aparna"
                        Percentage = 97.6


                 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 a new value to the operand on the left side. For example, if Sum=0, then
                 Sum+=10 is the same as Sum=Sum+10. These operators with example are shown below:

                       Symbol                    Purpose                       Example                   Output
                         +=           Adds first and then assigns a new  N1=10
                                      value.                          N2=15

                                                                      N1+=N2

                                                                      N2+=5
                                                                      print(N1,N2)                        25 20
                          -=          Subtracts first and then assigns a  Num1=5
                                      new value.
                                                                      Num2=10
                                                                      Num2-=Num1

                                                                      print(Num2)                           5
                          *=          Multiplies first and then assigns  Prod=5
                                      a new value.                    Prod*=5

                                                                      print(Prod)                           25


                                                                                    Advance Python (Practical)  277
   274   275   276   277   278   279   280   281   282   283   284