Sets¶
Set is unordered collection of unique items
In [61]:
myset = {'a','b','c','d','a','b','e','f','g'}
print (myset) # notice no repetition values
{'e', 'd', 'f', 'b', 'c', 'a', 'g'}
Membership Test¶
In [62]:
print ('a' in myset) # is member ?
print ('f' not in myset) # is not member ?
True False
Subset Test¶
Subset Test : <= Proper Subset Test : <
In [63]:
mysubset = {'d','g'}
mysubset <= myset
Out[63]:
True
Proper Subset test that the master set contain at least one element which is not in the subset
In [64]:
mysubset = {'b','a','d','c','e','f','g'}
print ('Is Subset : ', mysubset <= myset)
print ('Is Proper Subet : ', mysubset < myset)
Is Subset : True Is Proper Subet : False
Union using '|'¶
In [65]:
{'a','b','c'} | {'e','f'}
Out[65]:
{'a', 'b', 'c', 'e', 'f'}
Intersection using '&'¶
Any elments that exist in both left and right set
In [66]:
{'a','b','c','d'} & {'c','d','e','f'}
Out[66]:
{'c', 'd'}
Difference using '-'¶
Anything in left that is not in right
In [67]:
{'a','b','c','d'} - {'c','d','e','f'}
Out[67]:
{'a', 'b'}