142
|
1 |
Hello friends and welcome to the tutorial on Sets
|
|
2 |
|
|
3 |
{{{ Show the slide containing title }}}
|
|
4 |
|
|
5 |
{{{ Show the slide containing the outline slide }}}
|
|
6 |
|
|
7 |
In this tutorial, we shall learn
|
|
8 |
|
|
9 |
* sets
|
|
10 |
* operations on sets
|
|
11 |
|
|
12 |
Sets are data structures which contain unique elements. In other words,
|
|
13 |
duplicates are not allowed in sets.
|
|
14 |
|
|
15 |
Lets look at how to input sets.
|
|
16 |
type
|
|
17 |
::
|
|
18 |
|
|
19 |
a_list = [1, 2, 1, 4, 5, 6, 7]
|
|
20 |
a = set(a_list)
|
|
21 |
a
|
|
22 |
|
|
23 |
We can see that duplicates are removed and the set contains only unique
|
|
24 |
elements.
|
|
25 |
::
|
|
26 |
|
|
27 |
f10 = set([1, 2, 3, 5, 8])
|
|
28 |
p10 = set([2, 3, 5, 7])
|
|
29 |
|
|
30 |
f10 is the set of fibonacci numbers from 1 to 10.
|
|
31 |
p10 is the set of prime numbers from 1 to 10.
|
|
32 |
|
|
33 |
Various operations that we do on sets are possible here also.
|
|
34 |
The | character stands for union
|
|
35 |
::
|
|
36 |
|
|
37 |
f10 | p10
|
|
38 |
|
|
39 |
gives us the union of f10 and p10
|
|
40 |
|
|
41 |
The & character stands for intersection.
|
|
42 |
::
|
|
43 |
|
|
44 |
f10 & p10
|
|
45 |
|
|
46 |
gives the intersection
|
|
47 |
|
|
48 |
similarly,
|
|
49 |
::
|
|
50 |
|
|
51 |
f10 - p10
|
|
52 |
|
|
53 |
gives all the elements that are in f10 but not in p10
|
|
54 |
|
|
55 |
::
|
|
56 |
|
|
57 |
f10 ^ p10
|
|
58 |
|
|
59 |
is all the elements in f10 union p10 but not in f10 intersection p10. In
|
|
60 |
mathematical terms, it gives the symmectric difference.
|
|
61 |
|
143
|
62 |
Sets also support checking of subsets.
|
|
63 |
::
|
|
64 |
|
|
65 |
b = set([1, 2])
|
|
66 |
b < f10
|
|
67 |
|
|
68 |
gives a True since b is a proper subset of f10.
|
|
69 |
Similarly,
|
|
70 |
::
|
|
71 |
|
|
72 |
f10 < f10
|
|
73 |
|
|
74 |
gives a False since f10 is not a proper subset.
|
|
75 |
hence the right way to do would be
|
|
76 |
::
|
|
77 |
|
|
78 |
f10 <= f10
|
|
79 |
|
|
80 |
and we get a True since every set is a subset of itself.
|
|
81 |
|
|
82 |
Sets can be iterated upon just like lists and tuples.
|
|
83 |
::
|
|
84 |
|
|
85 |
for i in f10:
|
|
86 |
print i,
|
|
87 |
|
|
88 |
prints the elements of f10.
|
|
89 |
|
|
90 |
The length and containership check on sets is similar as in lists and tuples.
|
|
91 |
::
|
|
92 |
|
|
93 |
len(f10)
|
|
94 |
|
|
95 |
shows 5. And
|
|
96 |
::
|
|
97 |
|
|
98 |
1 in f10
|
|
99 |
2 in f10
|
|
100 |
|
|
101 |
prints True and False respectively
|
|
102 |
|
|
103 |
The order in which elements are organised in a set is not to be relied upon
|
|
104 |
since sets do not support indexing. Hence, slicing and striding are not valid
|
|
105 |
on sets.
|
|
106 |
|
142
|
107 |
{{{ Pause here and try out the following exercises }}}
|
|
108 |
|
|
109 |
%% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23]
|
|
110 |
list all the duplicates
|
|
111 |
|
|
112 |
{{{ continue from paused state }}}
|
|
113 |
|
|
114 |
Duplicates marks are the marks left out when we remove each element of the
|
|
115 |
list exactly one time.
|
|
116 |
|
|
117 |
::
|
|
118 |
|
|
119 |
marks = [20, 23, 22, 23, 20, 21, 23]
|
|
120 |
marks_set = set(marks)
|
|
121 |
for mark in marks_set:
|
|
122 |
marks.remove(mark)
|
|
123 |
|
|
124 |
# we are now left with only duplicates in the list marks
|
|
125 |
duplicates = set(marks)
|
|
126 |
|
|
127 |
{{{ Show summary slide }}}
|
|
128 |
|
|
129 |
This brings us to the end of the tutorial.
|
|
130 |
we have learnt
|
|
131 |
|
|
132 |
* How to make sets from lists
|
|
133 |
* How to input sets
|
143
|
134 |
* How to perform union, intersection and symmectric difference operations
|
|
135 |
* How to check if a set is a subset of other
|
142
|
136 |
* The various similarities with lists like length and containership
|
|
137 |
|
|
138 |
{{{ Show the "sponsored by FOSSEE" slide }}}
|
|
139 |
|
|
140 |
#[Nishanth]: Will add this line after all of us fix on one.
|
|
141 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
|
|
142 |
|
|
143 |
Hope you have enjoyed and found it useful.
|
|
144 |
Thankyou
|
|
145 |
|
|
146 |
.. Author : Nishanth
|
|
147 |
Internal Reviewer 1 :
|
|
148 |
Internal Reviewer 2 :
|
|
149 |
External Reviewer :
|