-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsets.py
More file actions
228 lines (124 loc) · 3.73 KB
/
sets.py
File metadata and controls
228 lines (124 loc) · 3.73 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
##################################
# #
# Python SETS #
# #
##################################
# sets {}
# A set is a collection which is unordered, unchangeable*, and unindexed.
set = { 'apple' , 'banana' , 'cherry' }
print(set)
# it will print o/p as {'banana', 'apple', 'cherry'}
# Note: the set list is unordered, meaning: the items will appear in a random order.
# sets are not allowed duplicate values..
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
# here 2 apple is there 1 will be removed
# and the o/p will be as randomed order...
# like wise list & tuples
# set contains data types
set1 = {"abc", 34, True, 40, "male"}
print(set1)
# the o/p will be as randomed .......
print(type(set1))
# the type is set .
# for loop in set
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
# it will be print the answer as randomed order ..
# NOTE : Once a set is created, you cannot change its items, but you can add new items. #
# add()
# for adding items
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
# it will add items and print answer as randomed ....
# update()
# updating the items in sets
# we can add sets in sets
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
# the o/p will be as randomed ..
# we can add set in list or tuples
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)
# like this
# as well as . it will print as randomed answer......
# removing or discarding items
# remove() or # discard ()
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
# it will be removed items ,,,
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
# it also remove but an error occur
# pop ()
# deleting the particular item
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
# clear()
# for clearing the set
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
# the o/p will be as set()
# set union and intersection like maths
# union ()
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
# it will united the sets and randomed answer will come ...
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
# union and update are like same only ...
# Note: Both union() and update() will exclude any duplicate items.
# intersection_update()
#The intersection_update() method will keep only the items that are present in both sets.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
# it will be printed as {'apple'}
# intersection ()
#The intersection() method will return a new set, that only contains the items that are present in both sets.
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
# we can create a new set and o/p as {'apple'} .
# symmetric_difference_update()
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
# it will keep all and removed the duplicates and print the answer
# symmetric_difference()
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
# it will return a new set and removed the duplicate items..
# sets
######################################
# clear()
# pop ()
# remove() or # discard ()
# update()
# add()
# union () and update()
#intersection ()
#intersection_update()
#symmetric_difference()
#symmetric_difference_update()
########################################