Page 111 - TP_Pluse_V2.2_Class_6
P. 111

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 operand on the right side to the left   x = 5
                                              side operand.
                     +=      Addition         It adds right operand to the left operand and assigns the      x += 3
                             assignment       result to left operand. x+=3 is equivalent to x=x+3.
                     –=      Subtraction      It subtracts right operand from the left operand and assigns   x –= 3
                             assignment       the result to left operand. x–=3 is equivalent to x=x–3.
                     *=      Multiplication   It multiplies right operand with the left operand and assigns   x *= 3
                             assignment       the result to left operand. x*=3 is equivalent to x=x*3.
                      /=     Division         It divides left operand with the right operand and assigns     x /= 3
                             assignment       the result to left operand. x/=3 is equivalent to x=x/3.
                     %=      Remainder        It takes modulus of two operands and assigns the result to     x %= 3
                             assignment       left operand. x%=3 is equivalent to x=x%3.

                     //=     Floor division   It performs floor division on operators and assigns the        x //= 3
                             assignment       value to the left operand. x//=3 is equivalent to x=x//3.

                     **=     Exponentiation  It performs exponential (power) calculation on operators        x **= 3
                             assignment       and assigns the value to the left operand. x**=3 is
                                              equivalent to x=x**3.


                  Program 2: To perform all the assignment operators

                     Program2.py

                  File  Edit  Format   Run    Options   Window    Help
                  #Program to perform Assignment Operators
                  a, b, c = 21, 10, 0
                  c = a + b
                  print (c)
                  c +=a
                  print (c)
                  c -=a
                  print (c)
                  c *=a                                                          Output
                  print (c)
                                                                               31
                  c /=a
                  print (c)                                                    52
                  c=2                                                          31
                  c %=a
                                                                               651
                  print (c)
                  c **=a                                                       31.0
                  print (c)                                                    2
                  c //=a
                                                                               2097152




                                                                                        Introduction to Programming  109
   106   107   108   109   110   111   112   113   114   115   116