Have you stumbled upon the strange-looking Python operator A |= B
in a code snippet and you don’t know what it means? This article will clarify it once and for all! Let’s start with the short answer:
Python’s A |= B
applies the |
operator in place. Thus, it is semantically identical to the longer-form version A = A | B
of first performing the operation A | B
and then assigning the result to the variable A
.
The following minimal example creates two Boolean variables A and B and performs the in-place B |= A
operation to perform a logical OR operation B | A
and assigning the result to the first operand B
that becomes True
:
>>> A = True >>> B = False >>> B |= A >>> B True
In this example, you’ve seen this in-place operation on Boolean operands. But the |
operator is overloaded in Python. The three most frequent use cases for the |
and |=
operators are the following:
- Python Sets: set union operator
- Python Dictionaries: dictionary update operator
- Python Booleans: logical OR operator
Let’s dive into each of them one by one.
|= on Python Sets
Python’s set.union(set_1, set_2, ...)
creates and returns a new set consisting of the elements that are members of any of the involved sets. A shorthand notation for the set union operator is the |
operator such as in set_1 | set_2 | set_3
. The set_1 |= set_2
operator performs the set union operator in-place—it replaces the set given as a first operand.
For example, the following three expressions are semantically equivalent—they all perform the union of sets set_1
and set_2
and assign the result to the set_1
variable.
>>> set_1 = set_1 | set_2 >>> set_1 |= set_2 >>> set_1.__ior__(set_2)
The first operation is an assigned OR operation. The second operation is an in-place OR. The third is an in-place operation using a special “dunder” method.
The following minimal example shows how set_1
is updated with the union of the two sets, in-place:
>>> set_1 = {'Alice'} >>> set_2 = {'Bob', 'Alice', 1, 2, 3} >>> set_1 |= set_2 >>> set_1 {1, 2, 3, 'Bob', 'Alice'}
|= on Dictionaries
Python 3.9 has introduced the merge and update operators on dictionaries.
dict_1 | dict_2
creates a new dictionary with all elements indict_1
anddict_2
. The second operand takes precedence over the first, so if a key exists in both dictionaries, Python uses the (key, value) pair from the second dictionary.dict_1 |= dict_2
updates the first dictionarydict_1
with the same merged dictionary elements.
In the following example, we updated the first dictionary with the (key, value) pairs from the second dictionary:
d1 = {'Alice': 42, 'Bob': 18} d2 = {'Alice': 18, 'Carl': 22} d1 |= d2 print(d1)
The output is the updated dictionary
{'Alice': 18, 'Bob': 18, 'Carl': 22}
|= on Booleans
The Python |=
operator when applied to two Boolean values A
and B
performs the logical OR operation A | B
and assigns the result to the first operand A
. As a result, operand A
is False
if both A
and B
are False
and True
otherwise.
This is shown in the following example where variable B is updated with the result of the operation B | A
using the in-place Boolean OR operation B |= A
.
>>> A = True >>> B = False >>> B |= A >>> B True
Python In-Place Operators
In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3
is the same as x = x + 3
of first calculating the result of x +3
and then assigning it to the variable x.
Operator | Name | Short Example | Equivalent Long Example |
---|---|---|---|
= | In-place Assignment | x = 3 | |
+= | In-place Addition | x += 3 | x = x + 3 |
-= | In-place Subtraction | x -= 3 | x = x - 3 |
*= | In-place Multiplication | x *= 3 | x = x * 3 |
/= | In-place Division | x /= 3 | x = x / 3 |
%= | In-place Modulo | x %= 3 | x = x % 3 |
//= | In-place Integer Division | x //= 3 | x = x // 3 |
**= | In-place Power | x **= 3 | x = x ** 3 |
&= | In-place Bitwise And | x &= 3 | x = x & 3 |
|= | In-place Bitwise Or | x |= 3 | x = x | 3 |
^= | In-place Bitwise XOR | x ^= 3 | x = x ^ 3 |
>>= | In-place Bitwise Shift Right | x >>= 3 | x = x >> 3 |
<<= | In-place Bitwise Shift Left | x <<= 5 | x = x << 5 |
Summary
Python’s A |= B
applies the |
operator in place. Thus, it is semantically identical to the longer-form version A = A | B
of first performing the operation A | B
and then assigning the result to the variable A
.
The | operator is most often used as one of the following:
- Python Sets: set union operator
- Python Dictionaries: dictionary update operator
- Python Booleans: logical OR operator
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.