## Introduction to Python Assignment Operators

Assignment Operators are used for assigning values to the variables. We can also say that assignment operators are used to assign values to the left-hand side operand. For example, in the below table, we are assigning a value to variable ‘a’, which is the left-side operand.

Operator | Description | Example | Equivalent |
---|---|---|---|

= | Assignment | a = 2 | a = 2 |

+= | Addition and Assignment | a += 2 | a = a + 2 |

-= | Subtraction and Assignment | a -= 2 | a = a – 2 |

*= | Multiplication and Assignment | a *= 2 | a = a * 2 |

/= | Division and Assignment | a /= 2 | a = a / 2 |

%= | Modulus and Assignment | a %= 2 | a = a % 2 |

//= | Floor Division and Assignment | a //= 2 | a = a // 2 |

**= | Exponential and Assignment | a **= 2 | a = a ** 2 |

&= | Bitwise AND and Assignment | a &= 2 | a = a & 2 |

|= | Bitwise OR and Assignment | a |= 2 | a = a | 2 |

^= | Bitwise XOR and Assignment | a ^= 2 | a = a ^ 2 |

>>= | Bitwise Right Shift and Assignment | a >>= 2 | a = a >> 2 |

<<= | Bitwise Left Shift and Assignment | a <<= 3 | a = a << 2 |

**Assignment Operators**

## Assignment Operator

Equal to sign ‘=’ is used as an assignment operator. It assigns values of the right-hand side expression to the variable or operand present on the left-hand side.

Assigns value 3 to variable ‘a’.

```
a = 3
print(a)
```

## Addition and Assignment Operator

The addition and assignment operator adds left-side and right-side operands and then the sum is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a + 2.

```
a = 3
a += 2
print(a)
```

## Subtraction and Assignment Operator

The subtraction and assignment operator subtracts the right-side operand from the left-side operand, and then the result is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a – 2.

```
a = 3
a -= 2
print(a)
```

## Multiplication and Assignment Operator

The multiplication and assignment operator multiplies the right-side operand with the left-side operand, and then the result is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a * 2.

```
a = 3
a *= 2
print(a)
```

## Division and Assignment Operator

The division and assignment operator divides the left-side operand with the right-side operand, and then the result is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a / 2.

```
a = 3
a /= 2
print(a)
```

## Modulus and Assignment Operator

The modulus and assignment operator divides the left-side operand with the right-side operand, and then the remainder is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a % 3.

```
a = 8
a %= 3
print(a)
```

## Floor Division and Assignment Operator

The floor division and assignment operator divides the left side operand with the right side operand. The result is rounded down to the closest integer value(i.e. floor value) and is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a // 3.

```
a = 8
a //= 3
print(a)
```

## Exponential and Assignment Operator

The exponential and assignment operator raises the left-side operand to the power of the right-side operand, and the result is assigned to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a ** 3.

```
a = 2
a **= 3
print(a)
```

## Bitwise AND and Assignment Operator

Bitwise AND and assignment operator performs bitwise AND operation on both the operands and assign the result to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a & 3.

```
a = 2
a &= 3
print(a)
```

Illustration:

Numeric Value | Binary Value | |
---|---|---|

2 | 010 | |

3 | 011 | |

Bitwise AND | 2 | 010 |

## Bitwise OR and Assignment Operator

Bitwise OR and assignment operator performs bitwise OR operation on both the operands and assign the result to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a | 3.

```
a = 2
a |= 3
print(a)
```

Illustration:

Numeric Value | Binary Value | |
---|---|---|

2 | 010 | |

3 | 011 | |

Bitwise OR | 3 | 011 |

## Bitwise XOR and Assignment Operator

Bitwise XOR and assignment operator performs bitwise XOR operation on both the operands and assign the result to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a ^ 3.

```
a = 2
a ^= 3
print(a)
```

Illustration:

Numeric Value | Binary Value | |
---|---|---|

2 | 010 | |

3 | 011 | |

Bitwise XOR | 1 | 001 |

## Bitwise Right Shift and Assignment Operator

Bitwise right shift and assignment operator right shifts the left operand by the right operand positions and assigns the result to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a >> 1.

```
a = 2
a >>= 1
print(a)
```

```
a = 4
a >>= 1
print(a)
```

Illustration:

Numeric Input | Binary Value | Right shift by 1 | Numeric Output |
---|---|---|---|

2 | 0010 | 0001 | 1 |

4 | 0100 | 0010 | 2 |

## Bitwise Left Shift and Assignment Operator

Bitwise left shift and assignment operator left shifts the left operand by the right operand positions and assigns the result to the left-hand side operand.

Below code is equivalent to:

â€ƒa = a << 1.

```
a = 2
a <<= 1
print(a)
```

```
a = 4
a <<= 1
print(a)
```

Illustration:

Numeric Input | Bitwise Value | Left shift by 1 | Numeric Output |
---|---|---|---|

2 | 0010 | 0100 | 4 |

4 | 0100 | 1000 | 8 |