Augmented Assignment Expressions in Python

In Python, we know how the arithmetic operators can be used to add, subtract, divide and multiply two variables.

In this article, we will learn how we can extend the functionality of an operator in a precise form at the time of evaluating an expression.

Let us have a look at few examples that will make the use of augmented assignment expressions clear to us.

We will discuss the following expression -

  1. +=
  2. -=
  3. *=
  4. /=
  5. %=
  6. **=
  7. //=
  8. >>=
  9. <<=
  10. &=

We will have a look at the programs that involve augmented assignment expressions and understand how they can be used.

1. Using +=

In the program given below, we have done the following operations-

  1. First, we have added two variables x and y.
  2. Used augmented assignment addition for adding x and 5.

Example -

Output

Value of z is:  25
Value of x is:  20

2. Using -=

In the program given below, we have done the following operations-

  1. First, we have subtracted two variables x and y.
  2. Used augmented assignment subtraction for subtracting x and 5.

Example -

Output

Value of z is:  5
Value of x is:  10

3. Using *=

In the program given below, we have done the following operations-

  1. First, we have multiplied two variables x and y.
  2. Used augmented assignment multiplication for multiplying x and 5.

Example -

Output

Value of z is:  150
Value of x is:  75

4. Using /=

In the program given below, we have done the following operations-

  1. First, we have divided two variables x and y.
  2. Used augmented assignment division for dividing x and 5.

Example -

Output

Value of z is:  1.5
Value of x is:  3.0

5. Using %=

In the program given below, we have done the following operations-

  1. First, we have taken the modulus of two variables x and y.
  2. Used augmented assignment modulus operation for obtaining results of x and 5.

Example -

Output

Value of z is:  5
Value of x is:  0

6. Using **=

In the below program, we have performed the following operations-

  1. First, we have calculated x raised to the power y.
  2. Used augmented assignment expression to evaluate x raised to power 3.

Example -

Output

Value of z is:  225
Value of x is:  3375

7. Using //=

In the below program, we have performed the following operations-

  1. First, we have calculated the value of the integer division of x and y.
  2. Used augmented assignment expression to evaluate integer division of x and 3.

Example -

Output

Value of z is:  7
Value of x is:  5

8. Using >>=

In the below program, we have evaluated the expression for the bitwise right shift between the variables x and y.

Example -

Output

Value of x is:  3

9. Using <<=

In the below program, we have evaluated the expression for the bitwise left shift between the variables x and y.

Example -

Output

Value of x is:  60

10. Using &=

In the below program, we have evaluated the expression for bitwise and shift between the variables x and y.

Example -

Output

Value of x is:  2

So, in this article, we learned how the augmented assignment expressions can be used in Python.






Latest Courses