What are Operators in Python?

Before we learn about assignment Operators, we must first understand what are Operators?. Operators in Python or any language are special symbols that carry out various operations like comparisons, performing arithmatic and logical operations or assigning value to some variable. For example when we say:

A+B , here

Symbol + is an arithmatic Operator. This Operator is performing some action or acting upon variables A and B. Here A and B are also known as operands as operators acts upon operands. 

Depending upon what action the operator performs on the operands, we have classified them into several types, which we are going to discuss in the coming few sections.

What are the Assignment Operators in Python?

We have already learned about variables and assigning values to them. To carry out such operation we require assignment operator. We have used assignment operator = earlier, for example:

Var=100
FName="Gaurav"
Amount=45678.87

In the example above, we are assigning some value (which could be of any type) to the variable on the LEFT.

List of all the Assignment Operators in Python

Assingment Operators Example Output Explanation

=

Age = 20
Print(Age)

20

 

+= Num = 10
Num += 5
Print(Num)
15 Num+=5 means
Num =Num+5 
-= Var = 100
Var -= 50
Print(Var)
50 Var-=5 means
Var=Var-5
*= X=10
Y=5
X*=Y
Print(X,Y)
50    5 X*=Y means
X=X*Y
/= Num=5
Num/=2
Print(Num)
2.5 Num/=2 means
Num=Num/2
//= Num=5
Num//=2
Print(Num)
2 Num//=2 means
Num =Num//2
%= Num=5
Num%=2
Print(Num)
1 Num%=2 means
Num=Num%2
**= X=3
Y=2
X**=Y
9 X**=Y means
X=X**Y

Assignment Operators are the most used operators. Different variations of the assignment operators for ex. +=, -=, *=, /=, //=, %=, **= have been introduced to make programs more compact and smaller in size. They are often also referred as macros. 

We know that variables are just memory bins (containers). We need to store some values and manipulate these values all the time through out while writing the program. This is usually in the following format:

LHS=RHS

While assigning values (using assignment operators) – LHS (Left Hand Side), always has to be a variable only. We cannot have anything else apart from a variable name on LHS, where as on the RHS we can use variables, literals, expressions, function calls etc. Following are some wrong statements:

5=10
100=Var+10

5+=10
12+=X
20/=5

All the above and similar statements would produce an error. 

Hence, it is very important to use assignment operators correctly, following the rules above.

Learn more about assignment in the field of Computer Science