-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch5-5.py
41 lines (27 loc) · 997 Bytes
/
ch5-5.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
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 17:15:57 2021
@author: HMFR
"""
"""
sets: unordered list, duplicates are removed
make by {'element1',...,'','',''}
to make an empty one use set()
"""
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
'''
#set comprehensions
a = {x for x in 'abracadabra' if x not in 'abc'}