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'>

Floor-Division equals To (//=)

As we know, the floor division equals to is also used to get quotient value in integer. In this, it will divide left operand with right one and later the resulting integer quotient assigns to the left operand.

For example :-

p = 16
q = 3
print('before //= operation,the value of p=%5d'%p)
print('before //= operation,the value of q=%5d'%q)

p //= q      # it is same as p=p//q 

print('after //= operation,the value of p=%5d'%p)
print('after //= operation,the value of q=%5d'%q)

 Assignment operator floor division equals to

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

before// = operation,the value of p=16
before //= operation,the value of q=3
after //= operation,the value of p=5
after //= operation,the value of q=3

Modulus equals To (%=)

The modulus equals to is also used to divide the left operand from the right operand , but it will return remainder as a result. Later this remainder assigns to the left operand.

For example :-

x = 15
y = 2
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 modulus equals to 

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

before %= operation,the value of x=15
before %= operation,the value of y=2
after %= operation,the value of x=1
after %= operation,the value of y=2

Exponent equals To (**=)

The exponent equals to is used to solve exponential value( power) and the result will be assigned to the left operand.

Let’s understand this through an example :-

p = 3
q = 4
print('before **= operation,the value of p=%5d'%p)
print('before **= operation,the value of q=%5d'%q)

p **= q      # it is same as p=p**q

print('after **= operation,the value of p=%5d'%p)
print(after **= operation,the value of q=%5d'%q)

Assignment operator power equals to

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

before **= operation,the value of p=3
before **= operation,the value of q=4
after **= operation,the value of p=81
after **= operation,the value of q=4

Bitwise-AND equals To (&=)

This operator performs AND (&) operation between left and right operand before assignment and later the result will assign to the left operand.

For example :-

The bitwise and equals to operator 
x=13
y=9
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)

and equals to

before %= operation,the value of x=13
before% = operation,the value of y=9
after %= operation,the value of x=9
after %= operation,the value of y=9

Bitwise-OR equals To(|=)

This operator performs OR( | ) operation between left and right operand before assignment and later the result will assign to the left operand.

For example :-

x = 12
y = 9

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 or equals to

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

before |= operation,the value of x=12
before | = operation,the value of y=9
after |= operation,the value of x=13
after |= operation,the value of y=9

Bitwise-Exclusive-OR equals To (^=)

This operator performs Exclusive-OR( ^ ) operation between left and right operand before assignment and later the result will assign to the left operand.

For example :-

x = 6
y = 7

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 xor equals to

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

before ^= operation,the value of x=6
before ^= operation,the value of y=7
after ^= operation,the value of x=1
after ^= operation,the value of y=7

Left-Shift equals To (<<=)

The left shift equals to operator perform left shifting on left operand with respect to their position(right operand) and later the result assigns to left operand.

Let’s understand this through an example :-

a = 3
b = 7

print('before <<= operation,the value of a=%5d'%a)
print('before <<= operation,the value of b=%5d'%b)

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)

 assignment operator left shift equals to

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

before <<= operation,the value of a=3
before << = operation,the value of b=7
after <<= operation,the value of a=384
after <<= operation,the value of b=7

Right_Shift equals To (>>=)

The right shift equals to operator perform right shifting on left operand with respect to their position(right operand) and later the result assigns to left operand.

Let’s understand this through an example :-

x = 12
y = 2

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 operators right shift equals to

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

before >>= operation,the value of x=12
before >> = operation,the value of y=2
after >>= operation,the value of x=2
after >>= operation,the value of y=2

The assignment operator = and equality operator == both are different and work differently.