-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.py
23 lines (20 loc) · 875 Bytes
/
Set.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# A set is an unordered collection with no duplicate elements. Use membership testing and eliminating duplicate entries.
# Set objects also 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
# fast membership testing
print('orange' in basket)
print('crabgrass' in basket)
# Demonstration of set operations on unique letters from two words
print()
a = set('abracadabra')
b = set('alacazam')
print(a) # unique letters in a
print(a - b) # letters in a but not in b
print(a | b) # letters in a or b or both
print(a & b) # letters in both a and b
print(a ^ b) # letters in a or b but not both
# set comprehensions
print()
c = {x for x in 'abracadabra' if x not in 'abc'}
print(c)