Link copied to clipboard. Share away!

Dismiss

Published on 09 Mar 2020

Assignment operators are generally used to **assign new values to existing or new variables**. They always assign the right-hand side value or an operand to the left side operand. Here is a list of all assignment operators in Python.

Operator | Name | Description |
---|---|---|

= | Assignment | This operator assigns its right-side value to its left-side operand |

+= | Addition assignment | This operator adds left and the right side operands and assigns the result to the left side operand |

-= | Subtraction assignment | This operator subtracts the right operand from the left operand and assigns the result to the left side operand |

*= | Multiplication assignment | This operator multiplies the right-side operand with the left-side operand and assigns the result to the left side operand |

/= | Division assignment | This operator divides the left side operand by the right-side operand and assigns the quotient to the left side operand |

%= | Modulus assignment | This operator divides the left side operand by the right side operand and assigns the remainder to the left side operand |

**= | Exponentiation assignment | This operator raises the left side operand to the power of the right-side operand and assigns the result value to the left side operand |

//= | Floor division assignment | This operator divides the left operand by the right operand and assigns the quotient value (in the form of an integer value) to the left operand |

&= | Bitwise AND assignment | This operator performs a bitwise AND operation on both the left and the right side operands and assigns the result to the left side operand |

|= | Bitwise OR assignment | This operator performs a bitwise OR operation on both the left and the right side operands and assigns the result to the left side operand |

^= | Bitwise XOR assignment | This operator performs bitwise XOR operation on both the left and the right side operands and assigns the result to the left side operand |

>>= | Bitwise right shift assignment | This operator right shifts the given value by the specified position and assigns the result to the left side operand |

<<= | Bitwise left shift assignment | This operator left shifts the given value by the specified position and assigns the result to the left side operand |

Here is a code snippet using various assignment operators in Python. This will help you understand better of how values get evaluated ad assigned.

```
#using = assignment operator
a = 10 #value 10 gets stored in a
b = 5 #value 5 gets stored in b
print (a)
print (b)
#using += addition assignment operator
a += 10 #value 10 gets added to current value of a i.e 10
print (a)
#using -= subtraction assignment operator
a -= 10 #value 10 gets added to current value of a i.e 20
print (a)
#using *= multiplication assignment operator
a *= 10 #value 10 gets multiplied to current value of a i.e 10
print (a)
#using /= division assignment operator
a /= 10 #current value of a i.e 100 gets divided by 10 & quotient is stored in a
print (a)
#using %= modulus assignment operator
a %= 8 #current value of a i.e 10.0 gets divided by 8 & the remainder is stored in a
print (a)
#using **= exponential assignment operator
a **= 10
print (a)
#using //= floor division assignment operator
a //= 10
print (a)
Output:
10
5
20
10
100
10.0
2.0
1024.0
102.0
```

All the assignment operators have the same precedence and hence we would consider **associativity to prioritize operations **when multiple assignment operators are used in one expression.**The associativity of these operators is right to left**. This means the operator that appears first when the expression is read from right to left, gets evaluated first.

Read more about the precedence & associativity of operators in Python

Know about all the operators in Python

If you have any feedback about this
article and want to improve this, please write to **enquiry@faceprep.in**

×