>>> s = {1,2,3,4}
>>> s&{1,3}
{1, 3}
>>> s|{11}
{1, 2, 3, 4, 11}
>>> s>{1,3}
True
>>> s.union({1,3})
{1, 2, 3, 4}
>>> s.union((11,22))
{1, 2, 3, 4, 11, 22}
>>> s.union([111,22])
{1, 2, 3, 4, 111, 22}
>>> s.intersection((3,4,5,6))
{3, 4}
>>> s.issubset(range(1,111))
True
>>> s.add((12,13)) #no list or dict,but tuple only
>>> s
{1, 2, 3, 4, (12, 13)}
>>> s.add({112,122})
Traceback (most recent call last):
File "<pyshell#95>", line 1, in <module>
s.add({112,122})
TypeError: unhashable type: 'set'
>>> help(s.add)
Help on built-in function add:
add(...) method of builtins.set instance
Add an element to a set.
This has no effect if the element is already present.
>>> s.add(0)
>>> s
{0, 1, 2, 3, 4, (12, 13)}
>>>{x for x in 'spam'}
{'m', 'p', 'a', 's'}
>>> {x*2 for x in 'spam'}
{'ss', 'pp', 'mm', 'aa'}
>>> engineers = {'Bob', 'Sue', 'Ann', 'Vic'}
>>> managers = {'Tom', 'Sue'}
>>> managers ^ engineers #Who is in one but not both?
{'Ann', 'Vic', 'Bob', 'Tom'}
>>> managers & engineers #Who is in both
{'Sue'}
>>> engineers - managers #engineers who are not managers
{'Ann', 'Vic', 'Bob'}
>>> managers - engineers #managers who are not engineers
{'Tom'}
>>> (managers | engineers) - (managers ^ engineers) #Intersecion! Same as &
{'Sue'}