What is Python Assignment Operators? Full List With Types and Examples
What are Assignment Operators in Python?
The assignment operators are utilized to assign values to variables.
Let's take a look at each Python Assignment Operator individually now.
-
Assign
The value of the right side of the expression is assigned to the left side operand using this operator.
Syntax
x = y + z
Example
a = 13
b = 15
c = a + b
print(c)
Output
-
Add and Assign
The right side operand is added to the left side operand, and the result is assigned to the left operand.
Syntax
x += y
Example
a = 3
b = 5
# a = a + b
a += b
print(a)
Output
-
Subtract and Assign
The result is assigned to the left operand after the right side operand is added to the left operand.
Syntax
x -= y
Example
a = 3
b = 5
# a = a - b
a -= b
print(a)
Output
-
Multiply and Assign
This operator multiplies the right operand by the left operand and assigns the result to the left operand.
Syntax
x *= y
Example
a = 3
b = 5
# a = a * b
a *= b
print(a)
Output
-
Divide and Assign
The right operand is multiplied by the left operand, and the result is assigned to the left operand.
Syntax
x /= y
Example
a = 3
b = 5
# a = a / b
a /= b
print(a)
Output
-
Modulus and Assign
Using the left and right operands, this operator computes the modulus and assigns the result to the left operand.
Syntax
x %= y
Example
a = 3
b = 5
# a = a % b
a %= b
print(a)
Output
-
Divide (floor) and Assign
The left operand is divided by the right operand, and the result (floor) is assigned to the left operand.
Syntax
x //= y
Example
a = 3
b = 5
# a = a // b
a //= b
print(a)
Output
-
Exponent and Assign
This operator calculates the exponent(raise power) value using operands and then assigns the result to the left operand.
Syntax
x **= y
Example
a = 3
b = 5
# a = a ** b
a **= b
print(a)
Output
-
Bitwise AND and Assign
This operator applies Bitwise AND to both operands before assigning the result to the left operand.
Syntax
x &= y
Example
a = 3
b = 5
# a = a & b
a &= b
print(a)
Output:
-
Bitwise OR and Assign
This operator performs a Bitwise OR on the operands before assigning the results to the left operand.
Syntax
x |= y
Example
a = 3
b = 5
# a = a | b
a |= b
print(a)
Output:
-
Bitwise XOR and Assign
This operator applies Bitwise XOR to the operands before assigning the results to the left operand.
Syntax
x ^= y
Example
a = 3
b = 5
# a = a ^ b
a ^= b
print(a)
Output:
-
Bitwise Right Shift and Assign
Before assigning the result to the left operand, this operator performs a Bitwise right shift on the operands.
Syntax
x >>= y
Example
a = 3
b = 5
# a = a >> b
a >>= b
print(a)
Output:
-
Bitwise Left Shift and Assign
This operator does a Bitwise left shift on the operands before assigning the results to the left operand.
Syntax
x <<= y
Example
a = 3
b = 5
# a = a << b
a <<= b
print(a)
Output: