Python tutorials > Core Python Fundamentals > Operators and Expressions > What are assignment operators?
What are assignment operators?
Basic Assignment (=)
x = 5
print(x)
Addition Assignment (+=)
x = 5
x += 3
print(x)
Subtraction Assignment (-=)
x = 5
x -= 2
print(x)
Multiplication Assignment (*=)
x = 5
x *= 4
print(x)
Division Assignment (/=)
x = 20
x /= 5
print(x)
Floor Division Assignment (//=)
x = 20
x //= 6
print(x)
Modulo Assignment (%=)
x = 20
x %= 6
print(x)
Exponentiation Assignment (**=
)
**=
) raises the left-hand operand to the power of the right-hand operand and assigns the result to the left-hand operand. In this example, x (which is 2) is raised to the power of 3, and the result (8) is assigned back to x.
x = 2
x **= 3
print(x)
Bitwise AND Assignment (&=
)
&=
) performs a bitwise AND operation between the left-hand operand and the right-hand operand, and assigns the result to the left-hand operand. In this example, 5 (0101 in binary) AND 3 (0011 in binary) is 0001 (which is 1 in decimal).
x = 5 # Binary: 0101
y = 3 # Binary: 0011
x &= y
print(x)
Bitwise OR Assignment (|=
)
|=
) performs a bitwise OR operation between the left-hand operand and the right-hand operand, and assigns the result to the left-hand operand. In this example, 5 (0101 in binary) OR 3 (0011 in binary) is 0111 (which is 7 in decimal).
x = 5 # Binary: 0101
y = 3 # Binary: 0011
x |= y
print(x)
Bitwise XOR Assignment (^=
)
^=
) performs a bitwise XOR (exclusive OR) operation between the left-hand operand and the right-hand operand, and assigns the result to the left-hand operand. In this example, 5 (0101 in binary) XOR 3 (0011 in binary) is 0110 (which is 6 in decimal).
x = 5 # Binary: 0101
y = 3 # Binary: 0011
x ^= y
print(x)
Bitwise Right Shift Assignment (>>=
)
>>=
) shifts the bits of the left-hand operand to the right by the number of positions specified by the right-hand operand, and assigns the result to the left-hand operand. In this example, 8 (1000 in binary) right-shifted by 2 positions is 0010 (which is 2 in decimal).
x = 8 # Binary: 1000
x >>= 2
print(x)
Bitwise Left Shift Assignment (<<=
)
<<=
) shifts the bits of the left-hand operand to the left by the number of positions specified by the right-hand operand, and assigns the result to the left-hand operand. In this example, 2 (0010 in binary) left-shifted by 2 positions is 1000 (which is 8 in decimal).
x = 2 # Binary: 0010
x <<= 2
print(x)
Concepts Behind the Snippet
Real-Life Use Case Section
x += 1
to increment a counter with each iteration. In game development, you might use position *= speed
to update an object's position based on its speed. They are also used in algorithms involving bit manipulation, such as image processing or data compression.
Best Practices
Interview Tip
When to Use Them
Memory Footprint
Alternatives
x += 3
is equivalent to x = x + 3
. The choice between the two often comes down to readability and personal preference, although assignment operators are generally preferred for conciseness.
Pros
Cons
FAQ
-
What is the difference between '=' and '+='?
The '=' operator is a simple assignment operator; it assigns the value on the right to the variable on the left. The '+=' operator is an addition assignment operator; it adds the value on the right to the current value of the variable on the left and assigns the result back to the variable. For example,x = 5
assigns 5 to x, whilex += 3
adds 3 to the current value of x. -
Are assignment operators just syntactic sugar?
Yes, assignment operators are essentially syntactic sugar.x += 5
is equivalent tox = x + 5
. The compiler or interpreter optimizes them similarly, so there's no major performance difference, but they improve code readability and conciseness. -
Can assignment operators be chained like 'a = b = c = 5'?
Yes, assignment operators can be chained in Python. For example,a = b = c = 5
assigns the value 5 to all three variables (a, b, and c). This is a valid and often used way to initialize multiple variables with the same value. -
Are assignment operators available in other languages?
Yes, most programming languages such as C, C++, Java, JavaScript, and C# also support assignment operators like +=, -=, *=, and /=. The syntax and behavior are generally consistent across these languages.