What is Assignment Operator in Python?
Before deep-diving into the assignment operators in Python, first, understand what operators are. So operators are used in between the operands to perform various operations, like mathematical operations, bitwise operations, logical operations, and so on. Here, operands are the values on which operators perform the actions. So, assignment operators are used to assigning values to the operands on the left-hand side. Assignment operators assign the right-hand side values to the operand that is present on the left-hand side. The assignment operator in Python is used as the "=" symbol.
Let’s see a very basic example of the assignment operator.
Table Of Assignment Operators
Here we will see different assignment operators in Python with their names, descriptions, and syntax. Let's take them one by one.
Operator | Name | Description | Syntax |
---|---|---|---|
= | Assignment Operator | It assigns the right-hand side expression value to the operand present on the left-hand side. | a=b+c |
+= | Addition Assignment Operator | This operator adds left and right operands, and after that, it assigns the calculated value to the left-hand operand. | a+=b or a=a+b |
-= | Subtraction Assignment Operator | This operator subtracts the right operand from the left operand and assigns the result to the left operand. | a-=b or a=a-b |
*= | Multiplication Assignment Operator | This operator will multiply the left-hand side operand by the right-hand side operand and, after that, assign the result to the left-hand operand. | a*=b or a=a*b |
/= | Division Assignment Operator | This operator will divide the left-hand side operand by the right-hand side operand and, after that, assign the result to the left-hand operand. | a/=b or a=a/b |
%= | Modulus Assignment Operator | This operator will divide the left-hand side operand by the right-hand side operand and, after that, assign the reminder to the left-hand operand. | a%=b or a=a%b |
**= | Exponentiation Assignment Operator | This operator raises the left-side operand to the power of the right-side operand and assigns the result to the left-side value. | a**=b or a=a**b |
//= | Floor Division Assignment Operator | This operator will divide the left operand by the right operand and assign the quotient value (which would be in the form of an integer) to the left operand. | a//=b or a=a//b |
&= | Bitwise AND Assignment Operator | This operator will perform the bitwise operation on both the left and right operands and, after that, it will assign the resultant value to the left operand. | a&=b or a=a&b |
|= | Bitwise OR Assignment Operator | This operator will perform the bitwise or operation on both the left and right operands and, after that, it will assign the resultant value to the left operand. | a|=b or a=a|b |
^= | Bitwise XOR Assignment Operator | This operator will perform the bitwise XOR operation on the left and right operands and, after that, it will assign the resultant value to the left operand. | a^=b or a=a^b |
>>= | Bitwise Right Shift Assignment Operator | This operator will right-shift the left operand by the specified position, i.e., b, and after that, it will assign the resultant value to the left operand. | a>>=b or a=a>>b |
<<= | Bitwise Left Shift Assignment Operator | This operator will left shift the left operand by the specified position, i.e., b, and after that, it will assign the resultant value to the left operand. | a<<=b or a=a<<b |
Examples
Assignment Operator :
This is an assignment operator in Python which assigns the right-hand side expression value to the operand present on the left-hand side.
Sample Code :
Output :
Addition Assignment Operator :
This type of assignment operator in Python adds left and right operands, and after that, it assigns the calculated value to the left-hand operand.
Sample Code :
Output :
Subtraction Assignment Operator :
This operator subtracts the right operand from the left operand and assigns the result to the left operand.
Sample Code :
Output :
Multiplication Assignment Operator:
This operator will multiply the left-hand side operand by the right-hand side operand and, after that, assign the result to the left-hand operand.
Sample Code :
Output :
Division Assignment Operator :
This type of assignment operator in Python will divide the left-hand side operand from the right-hand side operand and, after that, assign the result to the left-hand operand.
Sample Code :
Output :
Modulus Assignment Operator :
This operator will divide the left-hand side operand to the right-hand side operand and, after that, assign the reminder to the left-hand operand.
Sample Code :
Output :
Click here, to learn more about modulus in python.
Exponentiation Assignment Operator :
This operator raises the left-side operand to the power of the right-side operand and assigns the result to the left-side value.
Sample Code :
Output :
Floor Division Assignment Operator :
This operator will divide the left operand by the right operand and assign the quotient value (which would be in the form of an integer) to the left operand.
Sample Code :
Output :
Bitwise AND Assignment Operator :
This operator will perform the bitwise operation on both the left and right operands and, after that, it will assign the resultant value to the left operand.
Sample Code :
Output :
How?
Bitwise OR Assignment Operator:
This operator will perform the bitwise or operation on both the left and right operands and, after that, it will assign the resultant value to the left operand.
Sample Code :
Output :
How ?
Bitwise XOR Assignment Operator:
This operator will perform the bitwise XOR operation on the left and right operands and, after that, it will assign the resultant value to the left operand.
Sample Code :
Output :
How ?
Bitwise Right Shift Assignment Operator :
This operator will right shift the left operand by the specified position, i.e., b, and after that, it will assign the resultant value to the left operand.
Sample Code :
Output :
How ?
Bitwise Left Shift Assignment Operator:
This operator will left shift the left operand by the specified position, i.e., b, and after that, it will assign the resultant value to the left operand.
Sample Code :
Output :
How ?
Conclusion
- Assignment operators in Python assign the right-hand side values to the operand that is present on the left-hand side.
- The assignment operators in Python is used as the "=" symbol.
- We have different types of assignment operators like +=, -=, \*=, %=, /=, //=, \*\*=, &=, |=, ^=, >>=, <<=.
- All the operators have the same precedence, and hence we prioritise operations based on their associativiy. The associativity is from right to left.