# 其他的数字类型——Sets——在Python 2.6和更早版本中的 set基础

``````>>> x = set('abcde')
>>> y = set('bdxyz')``````

``````>>> x
set(['a', 'c', 'b', 'e', 'd']) # Pythons <= 2.6 显示格式``````

``````>>> x − y # 差集
set(['a', 'c', 'e'])
>>> x | y # 并集
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])
>>> x & y # 交集
set(['b', 'd'])
>>> x  y # 对称差集
(XOR)
set(['a', 'c', 'e', 'y', 'x', 'z'])
>>> x > y, x < y # 超集, 子集
(False, False)``````

``````>>> 'e' in x # 成员 (sets)
True
>>> 'e' in 'Camelot', 22 in [11, 22, 33] # 但也适用于其他类型
(True, True)``````

``````>>> dir(S)
['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']``````

``````>>> z = x.intersection(y) # 等同于 x & y
>>> z
set(['b', 'd'])
>>> z
set(['b', 'd', 'SPAM'])
>>> z.update(set(['X', 'Y'])) # 合并：就地修改的并集
>>> z
set(['Y', 'X', 'b', 'd', 'SPAM'])
>>> z.remove('b') # 删除项
>>> z
set(['Y', 'X', 'd', 'SPAM'])``````

``````>>> for item in set('abc'): print(item * 3)
aaa
ccc
bbb``````

``````>>> S = set([1, 2, 3])
>>> S | set([3, 4]) # 表达式需要两边都是set
set([1, 2, 3, 4])
>>> S | [3, 4]
TypeError: unsupported operand type(s) for |: 'set' and 'list'
>>> S.union([3, 4]) # 但它们的方法允许任何可迭代对象
set([1, 2, 3, 4])
>>> S.intersection((1, 3, 5))
set([1, 3])
>>> S.issubset(range(-5, 5))
True``````