-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06thSetsinPython.py
More file actions
55 lines (28 loc) · 933 Bytes
/
06thSetsinPython.py
File metadata and controls
55 lines (28 loc) · 933 Bytes
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
50
51
52
53
54
55
# ! Sets
s = {1,2,3,4,5}
print(type(s)) # * Returns set
a = {}
print(type(a)) # * Returns dictionary
# ! That's why we don't make sets like the above example as it returns dictionary type
a = set() # * This makes an empty set
print(s)
s1 = {1,2,3}
s2 = {5,3,4}
print(s1.union(s2))
print(s1.intersection(s2))
print(s1.difference(s2))
print(s1.symmetric_difference(s2))
print(s1 | s2) # * Union
print(s1 & s2) # * Intersection
print(s1 - s2) # * Difference
print(s1 ^ s2) # * Sysmmetric Difference
for i in s1:
print(i)
s1 = {1, (1,2), 'hi', True, 3, 2}
print(s1) # * Elements are printed in a random order
# s1.remove(100) # ! Throws an error as the element is not present in the list
s1.discard(100) # ! doesn't return an error if element is not present
print(s1.pop()) # ! pops random values
s1.clear() # * clears the set
print(s1) # ! returns set()
print(sorted(s2,reverse=True)) # ! Returns a LIST