Python Assignment Operators
flowchart TD
A["x += y"] --> B{"does type(x) define __iadd__?"}
B -->|"yes -- list"| C["mutate in place, same object"]
C --> D["every other reference sees the change"]
B -->|"no -- tuple, int, str"| E["build a new object, rebind the name"]
E --> F["other references still see the OLD value"]
G["t = ([1],) ; t[0] += [2]"] --> H["the inner list IS extended"]
H --> I["then the assignment back into the tuple fails"]
I --> J["TypeError -- and the mutation already happened"]
Assignment Operators in Python: Bridging Values and Variables
Section titled “Assignment Operators in Python: Bridging Values and Variables”Assignment operators in Python are the linchpin between variables and values, facilitating the assignment of data to variables and enabling dynamic and flexible programming. These operators not only assign values but also offer concise ways to update variables based on their current values. In this comprehensive guide, we’ll delve into the world of assignment operators, their syntax, and how they empower efficient Python programming.
The following table lists the assignment operators in Python:
| Operator | Description | Example |
|---|---|---|
= | Assigns values from the right side operands to the left side operand | x = y + z |
+= | It adds the right operand to the left operand and assigns the result to the left operand | x += y is equivalent to x = x + y |
-= | It subtracts the right operand from the left operand and assigns the result to the left operand | x -= y is equivalent to x = x - y |
*= | It multiplies the right operand with the left operand and assigns the result to the left operand | x *= y is equivalent to x = x * y |
/= | It divides the left operand with the right operand and assigns the result to the left operand | x /= y is equivalent to x = x / y |
%= | It takes modulus using two operands and assigns the result to the left operand | x %= y is equivalent to x = x % y |
**= | Performs exponential (power) calculation on operators and assigns the result to the left operand | x **= y is equivalent to x = x ** y |
//= | It performs floor division on operators and assigns the result to the left operand | x //= y is equivalent to x = x // y |
&= | Performs bitwise AND on operators and assigns the result to the left operand | x &= y is equivalent to x = x & y |
|= | Performs bitwise OR on operators and assigns the result to the left operand | x |= y is equivalent to x = x | y |
^= | Performs bitwise XOR on operators and assigns the result to the left operand | x ^= y is equivalent to x = x ^ y |
>>= | Performs bitwise right shift on operators and assigns the result to the left operand | x >>= y is equivalent to x = x >> y |
<<= | Performs bitwise left shift on operators and assigns the result to the left operand | x <<= y is equivalent to x = x << y |
Assignment Operators
Section titled “Assignment Operators”= (Assignment) Operator
Section titled “= (Assignment) Operator”The assignment operator (=) assigns the value of the right side operand to the left side operand. The following example demonstrates how to use the assignment operator in Python:
# Assignment operator
x = 10
y = 5
z = x + y
print(z)Output:
C:\Users\Your Name> python operators.py
15In the above example, we have used the assignment operator to assign the value of the right side operand to the left side operand. The value of x is 10 and the value of y is 5. The assignment operator adds the values of x and y and assigns the result to the variable z. The value of z is then printed to the console.
Addition Assignment Operators
Section titled “Addition Assignment Operators”+= (Addition Assignment) Operator
Section titled “+= (Addition Assignment) Operator”The addition assignment operator (+=) adds the value of the right side operand to the left side operand and assigns the result to the left side operand. The following example demonstrates how to use the addition assignment operator in Python:
# Addition assignment operator
x = 10
y = 5
x += y
print(x)Output:
C:\Users\Your Name> python operators.py
15In the above example, we have used the addition assignment operator to add the value of the right side operand to the left side operand and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The addition assignment operator adds the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Subtraction Assignment Operators
Section titled “Subtraction Assignment Operators”-= (Subtraction Assignment) Operator
Section titled “-= (Subtraction Assignment) Operator”The subtraction assignment operator (-=) subtracts the value of the right side operand from the left side operand and assigns the result to the left side operand. The following example demonstrates how to use the subtraction assignment operator in Python:
# Subtraction assignment operator
x = 10
y = 5
x -= y
print(x)Output:
C:\Users\Your Name> python operators.py
5In the above example, we have used the subtraction assignment operator to subtract the value of the right side operand from the left side operand and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The subtraction assignment operator subtracts the value of y from the value of x and assigns the result to the variable x. The value of x is then printed to the console.
Multiplication Assignment Operators
Section titled “Multiplication Assignment Operators”*= (Multiplication Assignment) Operator
Section titled “*= (Multiplication Assignment) Operator”The multiplication assignment operator (*=) multiplies the value of the right side operand with the left side operand and assigns the result to the left side operand. The following example demonstrates how to use the multiplication assignment operator in Python:
# Multiplication assignment operator
x = 10
y = 5
x *= y
print(x)Output:
C:\Users\Your Name> python operators.py
50In the above example, we have used the multiplication assignment operator to multiply the value of the right side operand with the left side operand and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The multiplication assignment operator multiplies the value of x with the value of y and assigns the result to the variable x. The value of x is then printed to the console.
Division Assignment Operators
Section titled “Division Assignment Operators”/= (Division Assignment) Operator
Section titled “/= (Division Assignment) Operator”The division assignment operator (/=) divides the left side operand with the right side operand and assigns the result to the left side operand. The following example demonstrates how to use the division assignment operator in Python:
# Division assignment operator
x = 10
y = 5
x /= y
print(x)Output:
C:\Users\Your Name> python operators.py
2.0In the above example, we have used the division assignment operator to divide the left side operand with the right side operand and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The division assignment operator divides the value of x with the value of y and assigns the result to the variable x. The value of x is then printed to the console.
Modulus Assignment Operators
Section titled “Modulus Assignment Operators”%= (Modulus Assignment) Operator
Section titled “%= (Modulus Assignment) Operator”The modulus assignment operator (%=) takes modulus using two operands and assigns the result to the left side operand. The following example demonstrates how to use the modulus assignment operator in Python:
# Modulus assignment operator
x = 10
y = 5
x %= y
print(x)Output:
C:\Users\Your Name> python operators.py
0In the above example, we have used the modulus assignment operator to take modulus using two operands and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The modulus assignment operator takes modulus using the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Exponent Assignment Operators
Section titled “Exponent Assignment Operators”**= (Exponent Assignment) Operator
Section titled “**= (Exponent Assignment) Operator”The exponent assignment operator (**=) performs exponential (power) calculation on operators and assigns the result to the left side operand. The following example demonstrates how to use the exponent assignment operator in Python:
# Exponent assignment operator
x = 10
y = 5
x **= y
print(x)Output:
C:\Users\Your Name> python operators.py
100000In the above example, we have used the exponent assignment operator to perform exponential (power) calculation on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The exponent assignment operator performs exponential (power) calculation on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Floor Division Assignment Operators
Section titled “Floor Division Assignment Operators”//= (Floor Division Assignment) Operator
Section titled “//= (Floor Division Assignment) Operator”The floor division assignment operator (//=) performs floor division on operators and assigns the result to the left side operand. The following example demonstrates how to use the floor division assignment operator in Python:
# Floor division assignment operator
x = 10
y = 5
x //= y
print(x)Output:
C:\Users\Your Name> python operators.py
2In the above example, we have used the floor division assignment operator to perform floor division on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The floor division assignment operator performs floor division on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Bitwise AND Assignment Operators
Section titled “Bitwise AND Assignment Operators”&= (Bitwise AND Assignment) Operator
Section titled “&= (Bitwise AND Assignment) Operator”The bitwise AND assignment operator (&=) performs bitwise AND on operators and assigns the result to the left side operand. The following example demonstrates how to use the bitwise AND assignment operator in Python:
# Bitwise AND assignment operator
x = 10
y = 5
x &= y
print(x)Output:
C:\Users\Your Name> python operators.py
0In the above example, we have used the bitwise AND assignment operator to perform bitwise AND on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The bitwise AND assignment operator performs bitwise AND on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Bitwise OR Assignment Operators
Section titled “Bitwise OR Assignment Operators”|= (Bitwise OR Assignment) Operator
Section titled “|= (Bitwise OR Assignment) Operator”The bitwise OR assignment operator (|=) performs bitwise OR on operators and assigns the result to the left side operand. The following example demonstrates how to use the bitwise OR assignment operator in Python:
# Bitwise OR assignment operator
x = 10
y = 5
x |= y
print(x)Output:
C:\Users\Your Name> python operators.py
15In the above example, we have used the bitwise OR assignment operator to perform bitwise OR on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The bitwise OR assignment operator performs bitwise OR on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Bitwise XOR Assignment Operators
Section titled “Bitwise XOR Assignment Operators”^= (Bitwise XOR Assignment) Operator
Section titled “^= (Bitwise XOR Assignment) Operator”The bitwise XOR assignment operator (^=) performs bitwise XOR on operators and assigns the result to the left side operand. The following example demonstrates how to use the bitwise XOR assignment operator in Python:
# Bitwise XOR assignment operator
x = 10
y = 5
x ^= y
print(x)Output:
C:\Users\Your Name> python operators.py
15In the above example, we have used the bitwise XOR assignment operator to perform bitwise XOR on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The bitwise XOR assignment operator performs bitwise XOR on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Bitwise Right Shift Assignment Operators
Section titled “Bitwise Right Shift Assignment Operators”>>= (Bitwise Right Shift Assignment) Operator
Section titled “>>= (Bitwise Right Shift Assignment) Operator”The bitwise right shift assignment operator (>>=) performs bitwise right shift on operators and assigns the result to the left side operand. The following example demonstrates how to use the bitwise right shift assignment operator in Python:
# Bitwise right shift assignment operator
x = 10
y = 5
x >>= y
print(x)Output:
C:\Users\Your Name> python operators.py
0In the above example, we have used the bitwise right shift assignment operator to perform bitwise right shift on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The bitwise right shift assignment operator performs bitwise right shift on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Bitwise Left Shift Assignment Operators
Section titled “Bitwise Left Shift Assignment Operators”<<= (Bitwise Left Shift Assignment) Operator
Section titled “<<= (Bitwise Left Shift Assignment) Operator”The bitwise left shift assignment operator (<<=) performs bitwise left shift on operators and assigns the result to the left side operand. The following example demonstrates how to use the bitwise left shift assignment operator in Python:
# Bitwise left shift assignment operator
x = 10
y = 5
x <<= y
print(x)Output:
C:\Users\Your Name> python operators.py
320In the above example, we have used the bitwise left shift assignment operator to perform bitwise left shift on operators and assign the result to the left side operand. The value of x is 10 and the value of y is 5. The bitwise left shift assignment operator performs bitwise left shift on the values of x and y and assigns the result to the variable x. The value of x is then printed to the console.
Conclusion
Section titled “Conclusion”Assignment operators are the cornerstone of variable manipulation in Python, providing the means to assign values, update variables, and handle multiple assignments. Whether you’re incrementing counters, performing complex calculations, or swapping values, assignment operators streamline these operations and contribute to the readability of your code.
As you delve deeper into Python programming, experiment with different assignment operators, explore their applications in real-world scenarios, and use them to enhance the efficiency and clarity of your code. For more hands-on examples and in-depth tutorials, explore our resources on Python Central Hub!
Check yourself
Section titled “Check yourself”-
`t = ([1],)` then `t[0] += [2]`. What happens?
Augmented assignment is two steps. `t[0].__iadd__([2])` extends the list — that succeeds, because the list is mutable. Then Python assigns the result back into the tuple, which fails. You get the exception AND the mutation.
pch.quizShowAnswer
B — TypeError, but `t` is now `([1, 2],)` — Augmented assignment is two steps. `t[0].__iadd__([2])` extends the list — that succeeds, because the list is mutable. Then Python assigns the result back into the tuple, which fails. You get the exception AND the mutation.
-
`L = [1,2]; L += [3]` versus `T = (1,2); T += (3,)`. Which keeps the same object?
A list defines `__iadd__`, so `+=` mutates in place and every other reference sees the change. A tuple has no in-place add, so the name is rebound to a new object and other references keep the old value.
pch.quizShowAnswer
C — Only the list — A list defines `__iadd__`, so `+=` mutates in place and every other reference sees the change. A tuple has no in-place add, so the name is rebound to a new object and other references keep the old value.
-
Why does the list-versus-tuple difference matter beyond performance?
`x += y` on a shared list is `x.extend(y)` — a function that did this to an argument would change its caller's data. On a tuple it is `x = x + y`, which affects nothing else.
pch.quizShowAnswer
B — Because mutating in place is visible through every other reference to that list — `x += y` on a shared list is `x.extend(y)` — a function that did this to an argument would change its caller's data. On a tuple it is `x = x + y`, which affects nothing else.
-
What is the safest reading of `x += y` when you do not know `x`'s type?
Python tries `__iadd__` first and falls back to `__add__` plus a rebinding. Same syntax, opposite consequences for shared state.
pch.quizShowAnswer
C — It mutates if the type supports it, otherwise it rebinds — Python tries `__iadd__` first and falls back to `__add__` plus a rebinding. Same syntax, opposite consequences for shared state.
Try it: Assignment Operators Exercises
Section titled “Try it: Assignment Operators Exercises”Exercise 1 – Augmented Assignment
Section titled “Exercise 1 – Augmented Assignment”Exercise 2 – Floor Division Assignment
Section titled “Exercise 2 – Floor Division Assignment”Exercise 3 – Modulo Assignment
Section titled “Exercise 3 – Modulo Assignment”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading