4.7 Building sets – literals, adding, comprehensions, and operators
If we’ve decided to create a collection based on only an item being present—a set—we have several ways of building this structure. Because of the narrow focus of sets, there’s no ordering to the items—no relative positions—and items cannot be duplicated. We’ll look at a number of ways we can assemble a set collection from a source of individual items.
The set operators parallel the operators defined by the mathematics of set theory. These can be helpful for doing bulk comparisons between sets. We’ll look at these in addition to the methods of the set class.
Sets have an important constraint: they only contain immutable objects. Informally, immutable objects have no internal state that can be changed. Numbers are immutable, as are strings, and tuples of immutable objects. Formally, immutable objects have an internal hash value, and...