×


Use Assignment Operators in Python - Complete guide ?

Operators are special symbols in a programming language that are used in between operands to perform logical and mathematical operations. The value on which the operator conducts the computation is known as the operand. Arithmetic, logical, relational, assignment, and bitwise are just a few of the operators available.

Here at Ibmi Media, we shall look into the Python programming language's assignment operators. 


More about Assignment Operators in Python

As the name suggest, you can assign any value to any variable using an assignment operator. This operator represents by the equality (=) sign whereas you need to write any variable on the left side which is also known as operand and on the right side you need to mention any number.

A basic syntax of representing the assignment operator is given below:

a = Any number

Here a is the variable or the operand and on the right side you can assign any constant number that will be stored in it for example,

a = 5


Different Assignment Operators in Python with examples

1. Add and Assignment Operator (+=)

Here we are not only assigning the value, but we are also going to add them as well and the result will be stored in the variable a on the left side.

It's Syntax is given below: 

a += b

For example,

a = 3
b = 5 
#a = a + b  
a += b
print(a)

Output:

8


2. Subtract and Assignment Operator (-=)

Here we are also going to subtract other than just assigning the values. 

The result will be stored in variable an on the left side as shown below.

It's Syntax is given below: 

a -= b

Example:

a = 5 
b = 3
#a = a - b   
a -= b  
print(a)

Output:

2


3. Multiply and Assignment Operator (*=)

Here, we are to use the multiplication operator as well as the assignment operator. The result will be stored in the variable an on the left side.

It's Syntax is given below: 

a *= b

Example:

a = 5 
b = 3 
#a = a * b   
a *= b
print(a)

Output:

15


4. Divide and Assignment Operator (/=)

Here we are also going to use the division operator along with the assignment operator. The result will be stored in the variable a on the left side.

It's Syntax is given below: 

a /= b

Example:

a = 3 
b = 5
#a = a / b   
a /= b
print(a)

Output:

0.6


5. Modulus and Assignment Operator (%=)

Modulus operator will show you the remainder when you divide two numbers, and their result will be stored in the variable.

It's Syntax is given below: 

 a %= b

Example:

a = 3 
b = 5
#a = a % b   
a %= b
print(a)

Output:

3


6. Exponent and Assignment Operator (**=)

This operator is used to calculate the exponential value assigned to any number and its result will be stored on the left side in the variable a.

It's Syntax is given below: 

a **= b

Example:

a = 3 
b = 5
#a = a ** b   
a **= b
print(a)

Output:

243


7. Bitwise And (&) and Assignment Operator (&=)

This operator will first convert the numbers in their binary form and then performed the And (&) operator to display the result back in decimal form.

It's Syntax is given below: 

a &= b

Example:

a = 3 (0011) 
b = 5 (0101)
#a = a(0011) & b(0101)   

a &= b = 0001 = 1
print(a)

Output:

1


8. Bitwise OR and Assignment Operator (|=)

This operator will first convert the numbers in their binary form and then performed the OR (|) operator to display the result back in decimal form.

It's Syntax is given below: 

a |= b

Example:

a = 3 (0011) 
b = 5 (0101)
#a = a (0011) | b (0101)  
a |= b = 0111 = 7
print(a)

Output:

7


9. Bitwise XOR and Assignment Operator (^=)

This operator is used to perform Bitwise XOR on the operands and then assigning result to the left operand.

It's Syntax is given below:

a ^= b

Example:

a = 3 (0011) 
b = 5 (0101)
#a = a ^ b (0011) ^ (0101)   
a ^= b
print(a) = (0110)

Output:

6


10. Bitwise Right Shift and Assign Operator (>>=)

This operator is used to perform Bitwise right shift on the assigned values given to the variables.

It's Syntax is given below: 

a >>= b

Example:

a = 3 
b = 5
#a = a >> b   
a >>= b
print(a)

Output:

0


11. Bitwise Left Shift and Assign operator (<<=)

This operator is used to perform Bitwise left shift on the assigned values given to the variables.

It's Syntax is given below: 

 a <<= b

Example:

a = 3 
b = 5
#a = a << b   
a <<= b
print(a)

Output:

96


[Need help in fixing Python Operators issues ? We can help you. ]


Conclusion

This article covers some of the most useful assignment operators in Python. In fact, Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, bitwise computations. The value the operator operates on is known as Operand.