Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Operator |

Sets every bit position present in either integer.

Version: Python 3.14; repository image, production runtime not attested

Syntax

left | right

Sets every bit position present in either integer.

Combine independent flag masks to enable any selected option. A result bit is zero only when that position is zero in both integer operands.

Sets interpret this operation as union. Dictionaries produce a merged dictionary, keeping the right value on duplicate keys. The meaning depends on the actual operand types.

Combining a mask with itself leaves its bits unchanged.

Syntax

left | right

__or__, __ror__ · TypeError

Worked example

0b1010 | 0b0110 → 0b1110
0b1010 | 0 → 0b1010
{1} | {2} → {1, 2}
{"x": 1} | {"x": 3} → {"x": 3}

set |

For sets, | creates a union containing elements from either operand, including each common element once. Both operands retain their contents. A left-hand frozenset yields a frozenset. The operator rejects a list, while union accepts one.

west={1,2}
east=frozenset([2,3])
assert west | east == {1,2,3} and west=={1,2}
assert type(east | west) is frozenset and west.union([4])=={1,2,4}
try: west | [4]
except TypeError: pass
else: raise AssertionError

Examples

print(0b1100 | 0b1010)
print(0b1010 | 0b1010)

Result

14
10

Combining a mask with itself leaves its bits unchanged.

Sources