Assignment Operators

The assignment operators are used to assign values to the variable. It evaluates the expression on the right side of the operator and substitutes it to the variable on the left side. It means, this is necessary that the left side will be a variable and in the right side it may be a literal or predefined variable or an expression.

For example-

m = 12 + 23

In the above statement, the value 12+23 is evaluated and substituted to the variable m.

Let us consider the statement :-

a = 12     # This is simple assignment in python
a = a + 1

This will have the effect of incrementing the value of a by 1, mean the equation will be :-

a = (previous assigned value to a) + 1
a = 12 + 1
a = 13  # Then the 13 will substitute to the variable a

There are several python assignment operators, which are listed below :-

= Equals to a = 2 a = 2
+= Plus equals to a += 2 a = a + 2
-= Minus equals to a -= 2 a = a - 2
*= Asterisk equals to a *= 2 a = a* 2
/= Division equals to a /= 2 a = a / 2
//= Floor division equals to a //= 2 a = a // 2
%= Modulus equals to a % 2 a = a % 2
**=   a **= 3 a = a ** 3
&= Bitwise and equals to a &= 4 a = a & 4
|= Bitwise or equals to a |= 3 a = a | 3
^= Bitwise exclusive or equals to a ^= 2 a = a ^ 2
<<= Left shift equals to a <<= 3 a = a << 3
>>= Right shift equals to a >>= 2 a = a >> 2

In the assignment operation, the variable on the left side will automatically convert to the type of value to the right side of the assignment operator. So, let’s understand these operators’ working through some python programs.

Plus equals To (+=)

In this operation first addition takes place and then the assignment operation will perform. It adds the right operand value to the left operand and later assigns the result value to the left.

Let’s understand this through an example :-

x = 20
y = 13
print('before += operation,the value of x = %5d' %x)
print('before += operation,the value of y = %5d' %y)

x += y  # it is same as x = x+y 

print('after += operation,the value of x = %5d' %x)
print('after += operation,the value of y = %5d' %y)

 Operator Plus Equals to

So, the output will be :-

before += operation,the value of x=20
before += operation,the value of y=13
after += operation,the value of x=33
after += operation,the value of y=13

Minus equals To (-=)

The minus equals to subtracts right operand from the  left one and later assigns the result value to the left operand.

Let’s understand this through an example :-

m = 12
n = 5
print('before -= operation,the value of m=%5d'%m)
print('before -= operation,the value of n=%5d'%n)

m -= n    # it is same as m=m-n

print('after -= operation,the value of m=%5d'%m)
print('after -= operation,the value of n=%5d'%n)

Assignment Operator minus equals to

So, the output of the following program  will be :-

before -= operation,the value of m=12
before -= operation,the value of n=5
after -= operation,the value of m=7
after -= operation,the value of n=5

Multiplication equals To (*=)

The multiplication equals to multiply both the right and left operand and later the product assigns to left operand.

For example :-

x = 2
y = 8
print('before *= operation,the value of x=%5d'%x)
print('before *= operation,the value of y=%5d'%y)

x *=y       # it is same as x= x*y 

print('after *= operation,the value of x=%5d'%x)
print('after *= operation,the value of y=%5d'%y)

assignment operator multiplication equals to

So, the output of the following program will be :-

before = operation,the value of x=2
before = operation,the value of y=8
after = operation,the value of x=16
after = operation,the value of y=8

Division equals To (/=)

The division equals to operator divide left operand from the right operand and return quotient as a result and later this quotient value assigns to the left operand. The result(quotient value) of this operation will be the real number.

Let’s understand this through an example :-

a = 13
b = 2
print('before *= operation,the value of a=%5d'%a)
print('before *= operation,the value of b=%5d'%b)
print(type(a))
a /= b       # it is same as a=a/b 

print('after /= operation,the value of a=%5d'%a)
print('after /= operation,the value of b=%5d'%b)
print(type(a))     # float value.

 assignment operator division equals to

So, the output of the following program will be :-

before/ = operation,the value of a=13
before  /= operation,the value of b=2
<class 'int'>
after /= operation,the value of a=6.5
after /= operation,the value of b=2
<class 'float'>