Python Notes

# Set operations

In Python, over a pair of sets, you can perform union, intersection, difference, and symmetric difference operations.

Initial sets:

```>>> a = set('1234')
>>> b = set('3456')
>>> a
{'2', '4', '3', '1'}
>>> b
{'5', '4', '3', '6'}```

The result of the union `|` (OR) is a set containing elements that are found in at least one of the original sets:

```>>> a | b
{'5', '6', '1', '3', '2', '4'}```

The result of the intersection & (AND) is a set containing elements that are found in both source sets:

```>>> a & b
{'3', '4'}```

The result of the difference `-` is a set containing elements that are in the "decreasing", but they are not in the "deductible". That is unique to the "reduced". The result will depend on the order of the operands:

```>>> a - b
{'1', '2'}
>>> b - a
{'5', '6'}```

The result of the symmetric difference `^` (exclusive OR) is a set consisting only of the unique elements of the original sets. Matching items are excluded:

```>>> a ^ b
{'5', '6', '1', '2'}```