28 p10 = set([2, 3, 5, 7]) |
28 p10 = set([2, 3, 5, 7]) |
29 |
29 |
30 f10 is the set of fibonacci numbers from 1 to 10. |
30 f10 is the set of fibonacci numbers from 1 to 10. |
31 p10 is the set of prime numbers from 1 to 10. |
31 p10 is the set of prime numbers from 1 to 10. |
32 |
32 |
33 Sets can be iterated upon just like lists and tuples. |
|
34 :: |
|
35 |
|
36 for i in f10: |
|
37 print i, |
|
38 |
|
39 prints the elements of f10. |
|
40 |
|
41 The length and containership check on sets is similar as in lists and tuples. |
|
42 :: |
|
43 |
|
44 len(f10) |
|
45 |
|
46 shows 5. And |
|
47 :: |
|
48 |
|
49 2 in f10 |
|
50 |
|
51 prints False |
|
52 |
|
53 The order in which elements are organised in a set is not to be relied upon |
|
54 since sets do not support indexing. Hence, slicing and striding are not valid |
|
55 on sets. |
|
56 |
|
57 Various operations that we do on sets are possible here also. |
33 Various operations that we do on sets are possible here also. |
58 The | character stands for union |
34 The | character stands for union |
59 :: |
35 :: |
60 |
36 |
61 f10 | p10 |
37 f10 | p10 |
81 f10 ^ p10 |
57 f10 ^ p10 |
82 |
58 |
83 is all the elements in f10 union p10 but not in f10 intersection p10. In |
59 is all the elements in f10 union p10 but not in f10 intersection p10. In |
84 mathematical terms, it gives the symmectric difference. |
60 mathematical terms, it gives the symmectric difference. |
85 |
61 |
|
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 |
86 {{{ Pause here and try out the following exercises }}} |
107 {{{ Pause here and try out the following exercises }}} |
87 |
108 |
88 %% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] |
109 %% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] |
89 list all the duplicates |
110 list all the duplicates |
90 |
111 |
108 This brings us to the end of the tutorial. |
129 This brings us to the end of the tutorial. |
109 we have learnt |
130 we have learnt |
110 |
131 |
111 * How to make sets from lists |
132 * How to make sets from lists |
112 * How to input sets |
133 * How to input sets |
|
134 * How to perform union, intersection and symmectric difference operations |
|
135 * How to check if a set is a subset of other |
113 * The various similarities with lists like length and containership |
136 * The various similarities with lists like length and containership |
114 * How to perform union, intersection and symmectric difference operations |
|
115 |
137 |
116 {{{ Show the "sponsored by FOSSEE" slide }}} |
138 {{{ Show the "sponsored by FOSSEE" slide }}} |
117 |
139 |
118 #[Nishanth]: Will add this line after all of us fix on one. |
140 #[Nishanth]: Will add this line after all of us fix on one. |
119 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
141 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |