Page 177 - Ai_V3.0_c11_flipbook
P. 177

Assignment Operators
                 Assignment operators are used to assign values to variables. They combine the assignment operation with another
                 operation such as addition, subtraction, multiplication, etc.


                       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


                         %=        Computes modulus of left operand with right operand and assigns the result to left operand
                         //=       Performs floor division on left operand by right operand and assigns the result to left operand


                         **=       Raises left operand to the power of right operand and assigns the result to left operand


                  Program 7: To demonstrate the use of assignment operators

                     # Uses of Assignment Operators

                     # Assignment
                     x = 15
                     print("Assignment:", x)

                     # Addition assignment

                     x += 3
                     print("Addition Assignment:", x)

                     # Subtraction assignment
                     x -= 2
                     print("Subtraction Assignment:", x)

                     # Multiplication assignment
                     x *= 4

                     print("Multiplication Assignment:", x)

                     # Division assignment
                     x /= 2
                     print("Division Assignment:", x)

                     # Modulus assignment
                     x %= 3
                     print("Modulus Assignment:", x)




                                                                                         Python Programming     175
   172   173   174   175   176   177   178   179   180   181   182