February 2006
Intermediate to advanced
648 pages
14h 53m
English
A set is used to contain an unordered collection of objects. To create a set, use the set() function and supply a sequence of items such as follows:
s = set([3,5,9,10]) # Create a set of numbers
t = set("Hello") # Create a set of charactersUnlike lists and tuples, sets are unordered and cannot be indexed in the same way. More over, the elements of a set are never duplicated. For example, if you print the value of t from the preceding code, you get the following:
>>> print t set(['H', 'e', 'l', 'o'])
Notice that only one 'l' appears.
Sets support a standard collection of set operations, including union, intersection, difference, and symmetric difference. For example:
a = t | s # Union of t and s b = t & s # Intersection of t and s c ...