Assignment operator in python to assign values of the right expression to the left operand. Usually , ‘=’ operator is used to assign value to the variable.
>>> a = 5
>>> print (a)
output : 5
Suppose we want to update the value with the new value. Various assignment operators (+=, – = , *=, /= , etc.) can be used in python to update the value of variables. We can also use a compound assignment operator, where we can add, subtract, multiply right operand to left and assign addition (or any other arithmetic function) to the left operand.
operator example same as
------------------------------------
= x = 5 x = 5
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Lets go with some examples of assignment operator in python
>>> a = 5
>>> a+=3
>>> print ('a+=3 i.e a = a+3 is',a)
>>> a = 5
>>> a*=3
>>> print ('a*=3 i.e a = a*3 is',a)
>>> a = 5
>>> a/=3
>>> print ('a/=3 i.e a = a/3 is',a)
>>> a = 5
>>> a&=3
>>> print ('a&=3 i.e a = a&3 is',a)
>>> a = 5
>>> a%=3
>>> print ('a%=3 i.e a = a%3 is',a)
>>> a = 5
>>> a|=3
>>> print ('a|=3 i.e a = a|3 is',a)
>>> a = 5
>>> a^=3
>>> print ('a^=3 i.e a = a^3 is', a)
>>> a = 5
>>> a>>=3
>>> print ('a>>=3 i.e a = a>>3 is', a)
And here is the output :
a+=3 i.e a = a+3 is 8
a*=3 i.e a = a*3 is 15
a/=3 i.e a = a/3 is 1.6666666666666667
a&=3 i.e a = a&3 is 1
a%=3 i.e a = a%3 is 2
a|=3 i.e a = a|3 is 7
a^=3 i.e a = a^3 is 6
a>>=3 i.e a = a>>3 is 0
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.
Generally I do not learn post on blogs, but I wish to say
that this write-up very compelled me to check
out and do it! Your writing taste has been amazed me. Thanks, quite great article.