-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsets.py
30 lines (21 loc) · 891 Bytes
/
sets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# $Id: sets.py, c86448423bdc makhtar $
# set: unordered collection with no duplicate elements
# support mathematical operations like union, intersection, difference, and symmetric difference
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket) # show that duplicates have been removed
print('orange' in basket) # fast membership testing
a = set('abracadabra')
b = set('alacazam')
print('\n', a) # unique letters in a
#{'a', 'r', 'b', 'c', 'd'}
print('\n', b)
print('\n', "a - b", a - b) # letters in a but not in b
# {'r', 'd', 'b'}
print('\n', "a | b", a | b) # letters in either a or b
# {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
print('\n', "a & b", a & b) # letters in both a and b
# {'a', 'c'}
print('\n', "a ^ b", a ^ b) # letters in a or b but not both
# Sets comprehension
a = {x for x in 'abracadabra' if x not in 'abc'}
print('\n', a)