Assignment operators in Python are used to assign values to variables and update them using different operations. These operators assign the result of an expression to the variable on the left-hand side, making variable manipulation simpler and more efficient.
# Assignment Operator
a = 3
b = 5
c = a + b
print(c)
Output
8
Addition Assignment Operator
The Addition Assignment Operator is used to add the right-hand side operand with the left-hand side operand and then assigning the result to the left operand.
a = 3
b = 5
a += b
print(a)
Output
8
Subtraction Assignment Operator
The Subtraction Assignment Operator is used to subtract the right-hand side operand from the left-hand side operand and then assigning the result to the left-hand side operand.
a = 3
b = 5
a -= b
print(a)
Output
-2
Multiplication Assignment Operator
The Multiplication Assignment Operator is used to multiply the right-hand side operand with the left-hand side operand and then assigning the result to the left-hand side operand.
a = 3
b = 5
a *= b
print(a)
Output
15
Division Assignment Operator
It's is used to divide the left-hand side operand with the right-hand side operand and then assigning the result to the left operand.
a = 3
b = 5
a /= b
print(a)
Output
0.6
Syntax: a /= b
Modulus Assignment Operator
The Modulus Assignment Operator is used to take the modulus, that is, it first divides the operands and then takes the remainder and assigns it to the left operand.
a = 3
b = 5
a %= b
print(a)
Output
3
Syntax: a %= b
Floor Division Assignment Operator
The Floor Division Assignment Operator is used to divide the left operand with the right operand and then assigns the result(floor value) to the left operand.
a = 3
b = 5
a //= b
print(a)
Output
0
Syntax: a //= b
Exponentiation Assignment Operator
The Exponentiation Assignment Operator is used to calculate the exponent(raise power) value using operands and then assigning the result to the left operand.
a = 3
b = 5
a **= b
print(a)
Output
243
Syntax: a **= b
Bitwise AND Assignment Operator
The Bitwise AND Assignment Operator is used to perform Bitwise AND operation on both operands and then assigning the result to the left operand.
a = 3
b = 5
a &= b
print(a)
Output
1
Syntax: a &= b
Bitwise OR Assignment Operator
The Bitwise OR Assignment Operator is used to perform Bitwise OR operation on the operands and then assigning result to the left operand.
a = 3
b = 5
a |= b
print(a)
Output
7
Syntax: a |= b
Bitwise XOR Assignment Operator
The Bitwise XOR Assignment Operator is used to perform Bitwise XOR operation on the operands and then assigning result to the left operand.
a = 3
b = 5
a ^= b
print(a)
Output
6
Syntax: a ^= b
Bitwise Right Shift Assignment Operator
The Bitwise Right Shift Assignment Operator is used to perform Bitwise Right Shift Operation on the operands and then assign result to the left operand.
a = 3
b = 5
a >>= b
print(a)
Output
0
Syntax: a >>= b
Bitwise Left Shift Assignment Operator
The Bitwise Left Shift Assignment Operator is used to perform Bitwise Left Shift Operator on the operands and then assign result to the left operand.
a = 3
b = 5
# a = a << b
a <<= b
# Output
print(a)
Output
96
Syntax: a <<= b
Walrus Operator
The Walrus Operator is a new assignment expression operator introduced in Python 3.8 and higher. This operator is used to assign a value to a variable within an expression.
Example: In this code, we have a Python list of integers. We have used Python Walrus assignment operator within the Python while loop. The operator evaluates the expression on the right-hand side and assigns the result to the variable on the left-hand side operand 'x' and then execute the remaining code.
# a list
a = [1, 2, 3, 4, 5]
# walrus operator
while(x := len(a)) > 2:
a.pop()
print(x)
Output
5 4 3
Syntax: a := expression