Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

Sets

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 characters

Unlike 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book