Assignment Operators in Python

Assignment Operators will work on values and variables. They are the special symbols that hold arithmetic, logical, and bitwise computations. The value which the operator operates is referred to as the Operand.

Read this article about Assignment Operators in Python

What are Assignment Operators?

The assignment operator will function to provide value to variables. The table below is about the different types of Assignment operator

OperatorDescriptionSyntax
+=Add and Assign operator will add right side operand with left side operand, assign to left operand a+=b
= It will assign the value of the right side of the expression to the left side operandx=y+z
-=Subtract AND operator can subtract the right operand from the left operand and then assign it to the left operand: True if both operands are equala -= b  
*=Subtract AND operator can subtract the right operand from the left operand and then assign it to the left operand: True if both operands are equala *= b     
/=Divide AND will divide the left operand with right operand and then assign to the left operanda /= b
%=Divide AND will divide the left operand with the right operand and then assign to the left operanda %= b  
<<=
It functions bitwise left on operands and will assign value to the left operand a <<= b 
>>=
This operator will perform right shift on operands and can assign value to the left operanda >>= b     

^=
This will function the bitwise xOR operands and can assign value to the left operand a ^= b    

|=
This will function Bitwise OR operands and will provide value to the left operand.a |= b    

&=
This operator will perform Bitwise AND on operand and can provide value to the left operanda&=b
**=
Exponent AND operator will evaluate the exponent value with the help of operands an assign value to the left operanda**=b

Here we have listed each of the Assignment operators

1. What is Assign Operator?

This assign operator will provide the value of the right side of the expression to the left operand.

Syntax

x = y + z

Example

# Assigning values using  
# Assignment Operator 
  
a = 3
b = 5
  
c = a + b 
  
# Output 
print(c)

Output

8

2. What is Add and Assign

This Add and Assign operator will function to add the right side operand with the left side operator, and provide the result to the left operand.

Syntax

x += y

Example

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

Output

8

3. What is Subtract and assign?

This subtract and assign operator works to subtract the right operand from the left operand and give the result to the left operand.

Syntax

x -= y

Example

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

Output

-2

4. What is Multiply and assign?

This Multiply and assign will function to multiply the right operand with the left operand and will provide the result in the left operand.

Syntax

x *= y

Example

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

Output

15

5. What is Divide and assign Operator?

This functions to divide the left operand and provides results at the left operand.

Syntax

x /= y

Example

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

Output

0.6

6. What is Modulus and Assign Operator?

This operator functions using the modulus with the left and the right operand and provides results at the left operand.

Syntax

x %= y

Example

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

Output

3

7. What is Divide ( floor)and Assign Operator?

This operator will divide the left operand with the right operand, and provide the result at the left operand.

Syntax

x //= y

Example

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

Output

0

8. What is Exponent and Assign Operator?

This operator will function to evaluate the exponent and value with the operands and, provide output in the left operand.

Syntax

x **= y

Example

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

Output

243'

9.What is Bitwise and Assign Operator?

This operator will function Bitwise AND on both the operand and provide the result on the left operand.

Syntax

x &= y

Example

a = 3
b = 5
  
# a = a & b 
a &= b 
  
# Output 
print(a)

Output

1

10. What is Bitwise OR and Assign Operator?

This operand will function Bitwise OR on the operand, and can provide result at the left operand.

Syntax

x |= y

Example

a = 3
b = 5
  
# a = a | b 
a |= b 
  
# Output 
print(a)

Output

7

11. What is Bitwise XOR and Assign Operator?

This operator will function for Bitwise XOR on the operands, and provide result at the left operand.

Syntax

x ^= y

Example

a = 3
b = 5
  
# a = a ^ b 
a ^= b 
  
# Output 
print(a)

Output

6

12. What is Bitwise Right Shift and Assign Operator?

This operator will function by providing the Bitwise shift on the operands and giving the result at the left operand.

Syntax

x >>= y

Example

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

Output

0

13. What is Bitwise Left shift and Assign Operator?

This operator will function Bitwise left shift by providing the Bitwise left shift on the operands and giving the result on the left operand.

Syntax

x <<= y

Example

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

Output

96

Conclusion

To conclude, different types of assignment operators are discussed in this. Beginners can improve their knowledge and understand how to apply the assignment operators through reading this.

Assignment Operators in Python- FAQs

Q1. What is an assignment statement in Python?

Ans. It will calculate the expression list and can provide a single resulting object to each target list from left to right

Q2. What is the compound operator in Python?

Ans. The compound operator will do the operation of a binary operator and will save the result of the operation at the left operand.

Q3. What are the two types of assignment statements

Ans. Simple Assignment Statements and Reference Assignment Statements are the two types of assignment statements.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment