1
+ #Using python to manipulate sets
2
+ '''
3
+ A Python set is the collection of the "unordered items".
4
+ Each element in the set must be "unique", "immutable",
5
+ and the sets remove the duplicate elements.
6
+ Sets are mutable which means we can modify it after its creation.
7
+
8
+ Unlike other collections in Python, there is no index attached
9
+ to the elements of the set, i.e., we cannot directly access
10
+ any element of the set by the index. However, we can print
11
+ them all together, or we can get the list of elements by
12
+ looping through the set.
13
+
14
+ a set is defined by a curly bracket. {}
15
+
16
+ Sets are mutable - this means that items can be changed.
17
+
18
+ Set has a whole bunch of methods available.
19
+ add() Adds an element to the set
20
+ clear() Removes all the elements from the set
21
+ copy() Returns a copy of the set
22
+ difference() Returns a set containing the difference between two or more sets
23
+ difference_update() Removes the items in this set that are also included in another, specified set
24
+ discard() Remove the specified item
25
+ intersection() Returns a set, that is the intersection of two or more sets
26
+ intersection_update() Removes the items in this set that are not present in other, specified set(s)
27
+ isdisjoint() Returns whether two sets have a intersection or not
28
+ issubset() Returns whether another set contains this set or not
29
+ issuperset() Returns whether this set contains another set or not
30
+ pop() Removes an element from the set
31
+ remove() Removes the specified element
32
+ symmetric_difference() Returns a set with the symmetric differences of two sets
33
+ symmetric_difference_update() Inserts the symmetric differences from this set and another
34
+ union() Return a set containing the union of sets
35
+ update() Update the set with another set, or any other iterable
36
+ '''
37
+
38
+ #The basics
39
+ basket = {'apple' , 'orange' , 'apple' , 'pear' , 'orange' , 'banana' }
40
+ print (basket )# show that duplicates have been removed
41
+
42
+ 'orange' in basket # fast membership testing TRUE
43
+ 'crabgrass' in basket # FALSE
44
+
45
+ #Example 1
46
+ Days = {"Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" }
47
+ print (Days )
48
+ print (type (Days ))
49
+ print ("looping through the set elements ... " )
50
+ for i in Days :
51
+ print (i )
52
+ '''
53
+ Output
54
+ {'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday', 'Wednesday'}
55
+ <class 'set'>
56
+ looping through the set elements ...
57
+ Friday
58
+ Tuesday
59
+ Monday
60
+ Saturday
61
+ Thursday
62
+ Sunday
63
+ Wednesday
64
+ '''
65
+
66
+ '''
67
+ It can contain any type of element such as integer, float, tuple etc.
68
+ But mutable elements (list, dictionary, set) can't be a member of set.
69
+ Consider the following example.
70
+ '''
71
+ # Creating a set which have immutable elements
72
+ set1 = {1 ,2 ,3 , "JavaTpoint" , 20.5 , 14 }
73
+ print (type (set1 ))
74
+ #Creating a set which have mutable element
75
+ set2 = {1 ,2 ,3 ,["Javatpoint" ,4 ]}
76
+ print (type (set2 ))
77
+
78
+ '''
79
+ Output
80
+ <class 'set'>
81
+
82
+ Traceback (most recent call last)
83
+ <ipython-input-5-9605bb6fbc68> in <module>
84
+ 4
85
+ 5 #Creating a set which holds mutable elements
86
+ ----> 6 set2 = {1,2,3,["Javatpoint",4]}
87
+ 7 print(type(set2))
88
+
89
+ TypeError: unhashable type: 'list'
90
+
91
+ In the above code, we have created two sets, the set set1 have immutable
92
+ elements and set2 have one mutable element as a list. While checking
93
+ the type of set2, it raised an error, which means set can contain only
94
+ immutable elements.
95
+
96
+ Creating an empty set is a bit different because empty curly {} braces
97
+ are also used to create a dictionary as well. So Python provides the set()
98
+ method used without an argument to create an empty set.
99
+ '''
100
+ # Empty curly braces will create dictionary
101
+ set3 = {}
102
+ print (type (set3 ))
103
+
104
+ # Empty set using set() function
105
+ set4 = set ()
106
+ print (type (set4 ))
107
+ '''
108
+ Output
109
+ <class 'dict'>
110
+ <class 'set'>
111
+ '''
112
+
113
+ #Adding items to the set
114
+ '''
115
+ Python provides the add() method and update() method which can be used
116
+ to add some particular item to the set. The add() method is used to add a
117
+ single element whereas the update() method is used to add multiple elements
118
+ to the set. Consider the following example.
119
+ '''
120
+ #Example
121
+ set = {"January" ,"February" }
122
+ set .add ("" March ")
123
+ set .update ({"April" , "May" , "June" })
124
+ for i in set :
125
+ print (i )
126
+
127
+ '''
128
+ Output
129
+ March
130
+ May
131
+ February
132
+ April
133
+ June
134
+ January
135
+ '''
136
+ #Removing items from the set
137
+ '''
138
+ Python provides the discard() method and remove() method
139
+ which can be used to remove the items from the set. The difference
140
+ between these function, using discard() function if the item does not
141
+ exist in the set then the set remain unchanged whereas remove() method
142
+ will through an error
143
+ '''
144
+ months = {"January" ,"February" , "March" , "April" , "May" , "June" }
145
+ print (months )
146
+ print ("\n Removing some months from the set..." )
147
+ months .discard ("January" )
148
+ months .discard ("April" )
149
+ months .remove ("August" )
150
+ '''
151
+ {'March', 'May', 'February', 'April', 'June', 'January'}
152
+ {'March', 'May', 'February', 'June'}
153
+ Traceback (most recent call last):
154
+ File "<stdin>", line 1, in <module>
155
+ KeyError: 'August'
156
+ '''
157
+ #Python Set Operations
158
+ '''
159
+ Set can be performed mathematical operation such as "union",
160
+ "intersection", "difference", and "symmetric difference".
161
+ Python provides the facility to carry out these operations
162
+ with operators or methods. We describe these
163
+ operations as follows.
164
+ '''
165
+ #Union of two Sets
166
+ '''The union of two sets is calculated by using the pipe (|) operator.
167
+ or the union() function The union of the two sets contains all
168
+ the items that are present in
169
+ both the sets.
170
+ '''
171
+ #Example 1: using union | operator
172
+ a = {"a" , "b" , "c" , "d" , "e" }
173
+ b = {"b" , "d" , "f" , "a" , "c" }
174
+ a | b
175
+ #{'f', 'c', 'e', 'd', 'a', 'b'} Output
176
+
177
+ #Intersection of two sets
178
+ '''
179
+ The intersection of two sets can be performed by the and & operator or
180
+ the intersection() function. The intersection of the two sets is given as
181
+ the set of the elements that common in both sets.
182
+ '''
183
+ Days1 = {"Monday" ,"Tuesday" , "Wednesday" , "Thursday" }
184
+ Days2 = {"Monday" ,"Tuesday" ,"Sunday" , "Friday" }
185
+ print (Days1 & Days2 ) # {'Monday', 'Tuesday'}
186
+
187
+ #Difference between the two sets
188
+ '''
189
+ The difference of two sets can be calculated by using the subtraction (-)
190
+ operator or intersection() method. Suppose there are two sets A and B, and
191
+ the difference is A-B that denotes the resulting set will be obtained that
192
+ element of A, which is not present in the set B.
193
+ '''
194
+ Days1 = {"Monday" , "Tuesday" , "Wednesday" , "Thursday" }
195
+ Days2 = {"Monday" , "Tuesday" , "Sunday" }
196
+ print (Days1 .difference (Days2 ))
197
+
198
+ #Symmetric Difference of two sets
199
+ '''
200
+ The symmetric difference of two sets is calculated by ^ operator or
201
+ symmetric_difference() method. Symmetric difference of sets, it removes
202
+ that element which is present in both sets. Consider the following example:
203
+ '''
204
+ a = {1 ,2 ,3 ,4 }
205
+ b = {1 ,2 ,9 }
206
+ c = a ^ b
207
+ print (c ) #{3, 4, 9}
208
+
209
+ #Set comparisons
210
+ '''
211
+ Python allows us to use the comparison operators i.e., <, >, <=, >= , ==
212
+ with the sets by using which we can check whether a set is a subset,
213
+ superset, or equivalent to other set. The boolean true or false is
214
+ returned depending upon the items present inside the sets.
215
+ '''
216
+ Days1 = {"Monday" , "Tuesday" , "Wednesday" , "Thursday" }
217
+ Days2 = {"Monday" , "Tuesday" }
218
+ Days3 = {"Monday" , "Tuesday" , "Friday" }
219
+
220
+ #Days1 is the superset of Days2 hence it will print true.
221
+ print (Days1 > Days2 )
222
+
223
+ #prints false since Days1 is not the subset of Days2
224
+ print (Days1 < Days2 )
225
+
226
+ #prints false since Days2 and Days3 are not equivalent
227
+ print (Days2 == Days3 )
0 commit comments