June 2015
Intermediate to advanced
298 pages
7h 15m
English
All of the collections we've looked at previously have been sequences: str, bytes, tuple, and list have items which can be accessed by their position within the collection. A set collection is an unordered collection where items are present or absent.
Items in a set collection must be immutable; they must provide a proper hash value as well as an equality test. This means that we can create sets of numbers, strings, and tuples. We can't easily create a set of lists or a set of sets.
The syntax of a set display is a sequence of expressions wrapped in {}.
Here's an example set built using numbers:
>>> fib_set = {1, 1, 3, 5, 8}
>>> fib_set
{8, 1, 3, 5}We've created a set object by enclosing the values in {}. This syntax looks ...
Read now
Unlock full access