-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD11_SetDatatype.py
More file actions
125 lines (79 loc) · 1.72 KB
/
Copy pathD11_SetDatatype.py
File metadata and controls
125 lines (79 loc) · 1.72 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
#1. Set
s={1,2,3}
print(s)
print(type(s))
s1 = {10.2,"Hiii",(1,2,3)}
print(s1)
s3={1,1,1,2,3,4}
print(s3) #duplicates not allowed
print(len(s3))
s4={}
print(type(s4)) #type is dict
s5=set()
print(type(s5)) #type is set
#2.set is mutable
s6={1,2,3}
s6.update([4,5],{1,6,8})
print(s6)
s1 = {1,2,3}
a = s1.copy()
print(a)
s2 = {1,2,3}
s2.add(4)
print(s2)
s2.discard(2)
print(s2)
s2.remove(8)
print(s2)
#3. pop,clear
s3={1,1,1,2,3,4} #pop()
print("\n",s3)
s3.pop()
print(s3)
s3.clear()
print(s3)
#4. for loop
s={"apple","banana"}
for fruit in s:
print(fruit)
for l in set("apple"):
print(l)
#5. adding grp of vals in set
x = {i for i in range(10,20)}
print(type(x))
print(x)
x = {i*i for i in range(10,20)}
print(type(x))
print(x)
x = {i for i in range(10,20,2)}
print(type(x))
print(x)
l1 = [10,20,30,40,50]
s= set()
for x in l1:
if(x>30):
s.add(x)
print(s)
#6. unique value list by using set
l1 = ['ratan','anu','durga']
l2 = ['Jack','Sam','Susan']
l3= ['Jane','Jack','Susan']
engg = set(l1+l2+l3) #here lists are concat hence allowed
print(engg)
#7. superset and subset - returns bool
A = {'d',6,'y'}
B = {5,'a',6,'r','d','y'}
print(A.issubset(B))
print(B.issuperset(A))
"""
#8. Operators- set diff,union,intersection,symmetric
b1 = {'orange','apple','pear','banana'}
b2 = {'orange','kiwi','pear','banana'}
print(b1-b2) #set diff - obj in b1 but not in b2
print(b1 | b2) #set union - ORing
print(b1 & b2) #set intersection - ANDing
print(b1 ^ b2) #symmetric diff - uncommon objs
result= b1.difference_update(b2)
print(result)
print(b1)