The intersection of two sets is the set of their shared elements. For instance, if we take the intersection of the set of high vowels with the set of back vowels, we get the high back vowels.
\[\{\text{i, u, ɪ, ʊ}\} \cap \{\text{u, ʊ, o, ɔ, ɑ}\} = \{\text{u, ʊ}\}\]
The union of two sets is the set of elements in both. For instance, if we take the union of the set of high vowels with the set of back vowels, we get the set of high and/or back vowels.
\[\{\text{i, u, ɪ, ʊ}\} \cup \{\text{u, ʊ, o, ɔ, ɑ}\} = \{\text{i, ɪ, u, ʊ, o, ɔ, ɑ}\}\]
The + operator does not work for sets like it does for lists! You need to use | or union() explicitly.
try: high_vowels + high_back_vowelsexceptTypeError:print("+ for sets does not implement union!")
+ for sets does not implement union!
Set builder notation
It is commonly the case that we want to filter a larger set–e.g. the vowels–down to a set containing only elements of that set with particular properties. For instance, suppose we want the high front vowels and we know how to check whether a vowel is high and whether it is front. We could describe the high front vowels using set-builder notation.
\[\{x \in \text{vowels} \mid x \text{ is high and } x \text{ is front}\}\]
Set-builder notation can be implemented using set comprehensions.
vowels: set[str] = {"e", "i", "o", "u", "æ", "ɑ", "ɔ", "ə", "ɛ", "ɪ", "ʊ"}def is_high(x: str) ->bool:return x in {"i", "u", "ɪ", "ʊ"}def is_front(x: str) ->bool:return x in {"i", "ɪ", "e", "æ", "ɛ"}{v for v in vowels if is_high(v) and is_front(v)}
{'i', 'ɪ'}
Note that:
\[\{x \in \text{vowels} \mid x \text{ is high and } x \text{ is front}\} = \{x \in \text{vowels} \mid x \text{ is high}\} \cap \{x \in \text{vowels} \mid x \text{ is front}\}\]
{v for v in vowels if is_high(v)} & {v for v in vowels if is_front(v)}
{'i', 'ɪ'}
This fact will be important for your first homework.
Complement
The (absolute) complement of a set \(A\) relative to a universe \(U\) (a possibly improper superset of \(A\)) is all elements in \(U\) that are not in \(A\).
\[A^\complement = \overline{A} = A' = \{x\;|\;x \in U \land x \not\in A\}\]
For instance, if \(U \equiv \{\text{e, i, o, u, æ, ɑ, ɔ, ə, ɛ, ɪ, ʊ}\}\), then the complement of the high vowels is the non-high vowels.
\[\{\text{i, u, ɪ, ʊ}\}^\complement = \{\text{e, o, æ, ɑ, ɔ, ə, ɛ}\}\]
Note that \(U = A \cup \overline{A}\).
Set difference
The set difference (or relative complement) of a set \(A\) relative to another set \(B\) is all elements in \(B\) that are not in \(A\)
\[B - A = \{x\;|\;x \in B \land x \not\in A\}\]
For instance, the difference of the set of high vowels relative to the set of high back vowels, is the high non-back vowels.
\[\{\text{i, u, ɪ, ʊ}\} - \{\text{u, ʊ}\} = \{\text{i, ɪ}\}\]