Page 134 - 2610_Springdales_C-6
P. 134

Assignment Operators
              The assignment operators are used to assign the value of the right expression to the left operand. The
              assignment operators are described in the following table:

               Operator        Name                                  Description                            Example
                   =      Assignment       It assigns the value of the operand on the right side to the left   x = 5
                                           side operand.
                   +=     Addition         It adds the right operand to the left operand and assigns the      x += 3
                          assignment       result to the left operand. x+=3 is equivalent to x=x+3.
                   –=     Subtraction      It subtracts the right operand from the left operand and assigns   x –= 3
                          assignment       the result to the left operand. x–=3 is equivalent to x=x–3.
                   *=     Multiplication  It multiplies the right operand with the left operand and assigns   x *= 3
                          assignment       the result to the left operand. x*=3 is equivalent to x=x*3.
                   /=     Division         It divides the left operand with the right operand and assigns     x /= 3
                          assignment       the result to the left operand. x/=3 is equivalent to x=x/3.
                   %=     Remainder        It takes the modulus of two operands and assigns the result       x %= 3
                          assignment       to the left operand. x%=3 is equivalent to x=x%3.
                   //=    Floor division   It performs floor division on operators and assigns the value      x //= 3
                          assignment       to the left operand. x//=3 is equivalent to x=x//3.
                  **=     Exponentiation  It performs exponential (power) calculations on operators          x **= 3
                          assignment       and assigns the value to the left operand. x**=3 is equivalent
                                           to x=x**3.


                Program 3: To perform all the assignment operators

                   Program3.py
                File  Edit  Format   Run    Options   Window    Help

                #Program to perform Assignment Operators
                a = 21
                b = 10
                c = 0
                c = a + b
                print (c)
                c +=a
                print (c)
                c -=a
                print (c)
                c *=a
                print (c)                                                     Output
                c /=a                                                       31
                print (c)
                                                                            52
                c=2
                                                                            31
                c %=a
                print (c)                                                   651
                c **=a                                                      31.0
                print (c)                                                   2
                c //=a
                                                                            2097152







                132     Premium Edition-VI
   129   130   131   132   133   134   135   136   137   138   139