basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket) # show that duplicates have been removed
'orange' in basket # fast membership testing
'crabgrass' in basket
Demonstrate set operations on unique letters from two words
a = set('abracadabra')
b = set('alacazam')
a # unique letters in a
a - b # letters in a but not in b
a | b # letters in a or b or both
a & b # letters in both a and b
a ^ b # letters in a or b but not both
s = {'Hi', 10,20,True}
s.remove('Hello')
print(s)
s.discard('Hello')
print(s)
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
tel['guido'] = 4127
print(tel)
tel['guido'] = 4199
print(tel)
Top comments (0)