There are a large number of set operations,
including union (|), intersection (&),
difference (-), symmetric difference (^).
These are unusual operations, so we'll look at them in some detail. In
addition to this operator notation, there are method functions which do
the same things. We'll look at the method function versions
below.

We'll use the following two sets to show
these operators.

The resulting set has elements from
both source sets. An element is in the
result if it is one setor the other.

>>>fib | primeset([1, 2, 3, 5, 7, 8, 11, 13])

Figure 16.1. Set Union, S1|S2

Intersection, &

The resulting set has elements that
are common to both source sets. An element
is in the result if it is in one setand the other.

>>>fib & primeset([2, 3, 5, 13])

Figure 16.2. Set Intersection, S1&S2

Difference, -

The resulting set has elements of the
left-hand set with all elements from the
right-hand set removed. An element will be
in the result if it is in the left-hand set
and not in the right-hand set.

The resulting set has elements which
are unique to each set. An element will be
in the result set if either it is in the
left-hand set and not in the right-hand
set or it is in the right-hand
set and not in the left-hand
set. Whew!

>>>fib ^ primeset([8, 1, 11, 7])

Figure 16.4. Set Symmetric Difference, S2^S2

Published under the terms of the Open Publication License