ASSIGNMENT OPERATOR IN PYTHON

Python Free Tutorial

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.

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

2 Comments on “ASSIGNMENT OPERATOR IN PYTHON”

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *