Page 473 - ComputerScience_Class_11
P. 473

12.2.3 Assignment Operators
                 Assignment operators are used to assign values to variables. Python also provides compound assignment operators,
                 which combine an arithmetic operation with assignment. These operators allow you to update the value of a variable
                 more efficiently, without having to write the variable name twice.
                 The following table shows the Assignment operators in Python along with their description:

                   Operator                                           Description
                       =        Assign the value of the right operand to the left operand.
                       +=       Adds right operand to left operand and assigns the result to left operand.
                       -=       Subtracts right operand from left operand and assigns the result to left operand.
                       *=       Multiplies left operand by right operand and assigns the result to left operand.
                       /=       Divides left operand by right operand and assigns the result to left operand.
                       %=       Takes the reminder after division and assigns it to the left operand.

                 Program 6: To demonstrate the use of Assignment operators.
                     Program 6.py

                  File  Edit  Format   Run   Options   Window     Help

                  # To assign value 15 to variable x
                  x = 15
                  print("Initial value of x:", x)

                  # To add 5 to x using += operator
                  x += 5
                  print("After x += 5:", x)

                  # To subtract 3 from x using -= operator
                  x -= 3
                  print("After x -= 3:", x)


                  # To multiply x by 2 using *= operator
                  x *= 2
                  print("After x *= 2:", x)

                  # To divide x by 4 using /= operator
                  x /= 4
                  print("After x /= 4:", x)

                  # To find remainder of x divided by 3 using %= operator
                  x %= 3
                  print("After x %= 3:", x)

                  # To perform floor division of x by 2 using //= operator
                  x //= 2
                  print("After x //= 2:", x)


                  # To raise x to the power of 3 using **= operator
                  x **= 3
                  print("After x **= 3:", x)





                                                                                           Operators and Expressions  471
   468   469   470   471   472   473   474   475   476   477   478