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.

Table of Contents

## |= 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.

creates a new dictionary with all elements in**dict_1 | dict_2**`dict_1`

and`dict_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.

updates the first dictionary**dict_1 |= dict_2**`dict_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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

**Practice projects is how you sharpen your saw in coding!**

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.