- publishing free software manuals
 An Introduction to Python by Guido van Rossum and Fred L. Drake, Jr.Paperback (6"x9"), 124 pagesISBN 0954161769RRP £12.95 (\$19.95)Sales of this book support the Python Software Foundation! Get a printed copy>>>

## 5.4 Sets

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Here is a brief demonstration:

```    >>> basket = ['apple', 'orange', 'apple', 'pear',
'orange', 'banana']
>>> fruit = set(basket)  # make set without duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])
>>> 'orange' in fruit    # fast membership testing
True
>>> 'crabgrass' in fruit
False

>>> # Demonstrate set operations on unique letters
# from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a      # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b  # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b  # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b  # letters in both a and b
set(['a', 'c'])
>>> a ^ b  # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
```
 ISBN 0954161769 An Introduction to Python See the print edition