-= Subtraction assignment
Last updated: 2026-09-22
-= Subtraction assignment
-= subtracts the right value from the left variable, equivalent to x = x - y.
| Category | Operators |
|---|---|
| Kind | Operator |
📝 Syntax
x -= y
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
3
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does -= do on a set?
AIn-place difference: `s -= {1}` is equivalent to `s.difference_update({1})`, removing elements from the set `s`. For immutable types it is ordinary subtraction followed by rebinding.
QDoes x -= 1 require x to be already defined?
AYes. `-=` first reads the current value of `x` and then computes, so an undefined `x` raises `NameError`. This differs from `=`, which can directly create a new variable.
QAfter s -= {1}, is s still the same object?
AYes. A set's `-=` uses `__isub__` to remove elements in place, so `id(s)` is unchanged; only immutable types (int, str, tuple) rebind to a new object.