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