-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSets.py
More file actions
49 lines (33 loc) · 1.13 KB
/
Sets.py
File metadata and controls
49 lines (33 loc) · 1.13 KB
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
42
43
44
45
46
47
48
49
'''
A set is a collection which is unordered, unchangeable*, and unindexed.
Duplicates Not Allowed
'''
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset) #{'apple', 'cherry', 'banana'}
print(len(thisset))
print(type(thisset)) #<class 'set'>
'''
Sets are unordered collections, so they don’t support indexing or slicing like lists or tuples do.
'''
#Convert the set to a list or tuple (which allows indexing)
thisset = {"apple", "banana", "cherry", "apple"}
x = list(thisset)
print(x[0]) #apple
x[0] = "Jack Fruit"
thisset = set(x)
print(type(thisset)) #<class 'set'>
#---Loop----
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
#---Set Join---
'''
The union() and update() methods joins all items from both sets.
The intersection() method keeps ONLY the duplicates.
The difference() method keeps the items from the first set that are not in the other set(s).
The symmetric_difference() method keeps all items EXCEPT the duplicates.
'''
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.intersection(set2)
print(set3)