Scripting

How to Use Assignment Operators in Python

How_to_Use_Assignment_Operators_in_Python

This section will go through the Python programming language’s assignment operators. A little refresher on Python operators is in order before we get into the core of the topic. 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.

Assignment Operators in Python with examples

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

In this section we are going to discuss all the related assignment operators that can be used in Python programming language.

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 that can be seen below

Syntax: 
a += b

Example:

a = 3

b = 5 

#a = a + b  

a += b

print(a)

Output:

8

Subtract and Assignment Operator (-=)

Here we are also going to subtract other than just assigning the values as shown below. The result will be stored in variable an on the left side.

Syntax: 
a -= b

Example:

a = 5 
b = 3
#a = a - b   
a -= b  

print(a)

Output:

2

Multiply and Assignment Operator (*=)

In this section, 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.

Syntax: 
a *= b

Example:

a = 5 

b = 3 

#a = a * b   

a *= b

print(a)

Output:

15

Divide and Assignment Operator (/=)

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

Syntax: 
a /= b

Example:

a = 3 
b = 5
#a = a / b   
a /= b

print(a)

Output:

0.6

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 an as shown below.

Syntax: 
 a %= b

Example:

a = 3 
b = 5
#a = a % b   
a %= b

print(a)

Output:

3

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.

Syntax: 
a **= b

Example:

a = 3 
b = 5
#a = a ** b   
a **= b

print(a)

Output:

243

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.

Syntax: 
a &= b

Example:

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


a &= b = 0001 = 1

print(a)

Output:

1

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.

Syntax: 
a |= b

Example:

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

print(a)

Output:

7

Bitwise XOR and Assignment Operator (^=)

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

Syntax:
a ^= b

Example:

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

print(a) = (0110)

Output:

6

Bitwise Right Shift and Assign Operator (>>=)

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

Syntax: 
a >>= b

Example:

a = 3 
b = 5
#a = a >> b   
a >>= b

print(a)

Output:

0

Bitwise Left Shift and Assign operator (<<=)

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

Syntax: 
 a <<= b

Example:

a = 3 
b = 5
#a = a << b   
a <<= b

print(a)

Output:

96

Conclusion

In this article, we have discussed some of the most useful assignment operators in Python. Their explanation and syntax have been discussed in detail in this article for better understanding.

Similar Posts