24 \begin{center} |
24 \begin{center} |
25 \LARGE{Python: Data Structures}\\ |
25 \LARGE{Python: Data Structures}\\ |
26 \large{FOSSEE} |
26 \large{FOSSEE} |
27 \end{center} |
27 \end{center} |
28 \section{Basic Looping} |
28 \section{Basic Looping} |
29 \typ{while} |
29 \subsection{\typ{while}} |
30 \begin{lstlisting} |
30 \begin{lstlisting} |
31 In []: a, b = 0, 1 |
31 In []: a, b = 0, 1 |
32 In []: while b < 10: |
32 In []: while b < 10: |
33 ...: print b, |
33 ...: print b, |
34 ...: a, b = b, a + b # Fibonacci Sequence |
34 ...: a, b = b, a + b # Fibonacci Sequence |
35 \end{lstlisting} |
35 ...: |
|
36 \end{lstlisting} |
|
37 Basic syntax of \typ{while} loop is: |
|
38 \begin{lstlisting} |
|
39 while condition: |
|
40 statement1 |
|
41 statement2 |
|
42 \end{lstlisting} |
|
43 All the statements are executed, till the condition statement evaluates to True. |
|
44 \subsection{\typ{for} and \typ{range}} |
|
45 \typ{range(start, stop, step)}\\ |
|
46 returns a list containing an arithmetic progression of integers.\\ |
|
47 Of the arguments mentioned above, both start and step are optional.\\ |
|
48 For example, if we skip third argument, i.e \typ{step}, then default value is 1. So: |
|
49 \begin{lstlisting} |
|
50 In []: range(1,10) |
|
51 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
52 \end{lstlisting} |
|
53 \textbf{Note:} stop value is not included in the list.\\ |
|
54 Similarly if we don't pass \typ{start} argument, default is taken to be 0. |
|
55 \begin{lstlisting} |
|
56 In []: range(10) |
|
57 Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
58 \end{lstlisting} |
|
59 In case \typ{step} is mentioned, the jump between consecutive members of the list would be equal to that. |
|
60 \begin{lstlisting} |
|
61 In []: range(1,10,2) |
|
62 Out[]: [1, 3, 5, 7, 9] |
|
63 \end{lstlisting} |
|
64 %Notice the jump between two consecutive elements is 2, i.e step.\\ |
36 \typ{for} and \typ{range}\\ |
65 \typ{for} and \typ{range}\\ |
37 \typ{range([start,] stop[, step])} |
66 As mentioned previously \typ{for} in Python is used to iterate through the list members. So \typ{for} and \typ{range} can be used together to iterate through required series. For example to get square of all numbers less then 5 and greater then equal to 0, code can be written as: |
38 \begin{lstlisting} |
67 \begin{lstlisting} |
39 In []: for i in range(3, 10, 2): |
68 In []: for i in range(5): |
40 ....: print i, i * i |
69 ....: print i, i * i |
|
70 ....: |
|
71 ....: |
|
72 0 0 |
|
73 1 1 |
|
74 2 4 |
41 3 9 |
75 3 9 |
42 5 25 |
76 4 16 |
43 7 49 |
77 \end{lstlisting} |
44 9 81 |
78 |
45 \end{lstlisting} |
79 \section{list} |
46 List methods (Contd.) |
80 \begin{lstlisting} |
|
81 In []: num = [1, 2, 3, 4] # Initializing a list |
|
82 In []: num |
|
83 Out[]: [1, 2, 3, 4] |
|
84 \end{lstlisting} |
|
85 \subsection{Accessing individual elements} |
|
86 \begin{lstlisting} |
|
87 In []: num[1] |
|
88 Out[]: 2 |
|
89 \end{lstlisting} |
|
90 \textbf{Note:} Index of list starts from 0. |
|
91 \begin{lstlisting} |
|
92 In []: num[5] # ERROR: throws a index error |
|
93 IndexError: list index out of range |
|
94 In []: num[-1] |
|
95 Out[]: 4 |
|
96 \end{lstlisting} |
|
97 \textbf{Note: }\typ{-1} points the last element in a list. Similarly to access third last element of a list one can use: |
|
98 \begin{lstlisting} |
|
99 In []: num[-3] |
|
100 Out[]: 2 |
|
101 \end{lstlisting} |
|
102 \subsection{Adding lists and elements} |
|
103 \begin{lstlisting} |
|
104 In []: num += [9, 10, 11] # Adding two lists |
|
105 In []: num |
|
106 Out[]: [1, 2, 3, 4, 9, 10, 11] |
|
107 \end{lstlisting} |
|
108 \typ{list} provides \typ{append} function to append objects at the end. |
|
109 \begin{lstlisting} |
|
110 In []: num = [1, 2, 3, 4] |
|
111 In []: num.append(-2) |
|
112 In []: num |
|
113 Out[]: [1, 2, 3, 4, -2] |
|
114 \end{lstlisting} |
|
115 %% In []: num += [-5] |
|
116 %% In []: num |
|
117 %% Out[]: [1, 2, 3, 4, -2, -5] |
|
118 Working of \typ{append} is different from \typ{+} operator on list. Till here both will behave as same. But in following case: |
47 \begin{lstlisting} |
119 \begin{lstlisting} |
48 In []: num = [1, 2, 3, 4] |
120 In []: num = [1, 2, 3, 4] |
49 In []: num.append([9, 10, 11]) |
121 |
50 In []: num |
122 In []: num + [9, 10, 11] |
51 Out[]: [1, 2, 3, 4, [9, 10, 11]] |
123 Out[]: [1, 2, 3, 4, 9, 10, 11] |
|
124 |
|
125 In []: num.append([9, 10, 11]) # appending a list to a list |
|
126 |
|
127 In []: num |
|
128 Out[]: [1, 2, 3, 4, [9, 10, 11]] # last element is a list |
|
129 \end{lstlisting} |
|
130 when one attempts to append a list(in above case [9, 10, 11]) to a list(num) it adds list as a single element. So the resulting list will have a element which itself is a list. But \typ{+} operator would simply add the elements of second list.\\ |
|
131 \subsection{Miscellaneous} |
|
132 \begin{lstlisting} |
52 In []: num = [1, 2, 3, 4] |
133 In []: num = [1, 2, 3, 4] |
53 In []: num.extend([5, 6, 7]) |
134 In []: num.extend([5, 6, 7]) # extend list by adding elements |
54 In []: num |
135 In []: num |
55 Out[]: [1, 2, 3, 4, 5, 6, 7] |
136 Out[]: [1, 2, 3, 4, 5, 6, 7] |
56 In []: num.reverse() |
137 In []: num.reverse() # reverse the current list |
57 In []: num |
138 In []: num |
58 Out[]: [7, 6, 5, 4, 3, 2, 1] |
139 Out[]: [7, 6, 5, 4, 3, 2, 1] |
59 In []: num.remove(6) |
140 In []: num.remove(6) # removing first occurrence of 6 |
60 In []: num |
141 In []: num |
61 \end{lstlisting} |
142 Out[]: [7, 5, 4, 3, 2, 1] |
62 Slicing: \typ{list[initial:final:step]} |
143 In []: len(num) # returns the length of list |
63 \begin{lstlisting} |
144 Out[]: 6 |
64 In []: a[1:-1:2] |
145 In []: a = [1, 5, 3, 7, -2, 4] |
|
146 In []: min(a) # returns smallest item in a list. |
|
147 Out[]: -2 |
|
148 In []: max(a) # returns largest item in a list. |
|
149 Out[]: 7 |
|
150 \end{lstlisting} |
|
151 |
|
152 \subsection{Slicing} |
|
153 General syntax for getting slice out of a list is \\ |
|
154 \typ{list[initial:final:step]} |
|
155 \begin{lstlisting} |
|
156 In []: a = [1, 2, 3, 4, 5] |
|
157 In []: a[1:-1:2] |
65 Out[]: [2, 4] |
158 Out[]: [2, 4] |
|
159 \end{lstlisting} |
|
160 Start slice from second element(1), till the last element(-1) with step size of 2. |
|
161 \begin{lstlisting} |
66 In []: a[::2] |
162 In []: a[::2] |
67 Out[]: [1, 3, 5] |
163 Out[]: [1, 3, 5] |
|
164 \end{lstlisting} |
|
165 Start from beginning(since \typ{initial} is blank), till last of list(this time last element is included, as \typ{final} is blank), with step size of 2.\\ |
|
166 Apart from using \typ{reverse} command on list, one can also use slicing in special way to get reverse of a list. |
|
167 \begin{lstlisting} |
|
168 In []: a[-1:-4:-1] |
|
169 Out[]: [5, 4, 3] |
|
170 \end{lstlisting} |
|
171 Above syntax of slice can be expressed as, ``start from last element(\typ{-1}), go till fourth last element(\typ{-4}), with step size \typ{-1}, which implies, go in reverse direction. That is, first element would be \typ{a[-1]}, second element would be \typ{a[-2]} and so on and so forth.''\\ |
|
172 So to get reverse of whole list one can write following slice syntax: |
|
173 \begin{lstlisting} |
68 In []: a[-1::-1] |
174 In []: a[-1::-1] |
69 Out[]: [5, 4, 3, 2, 1] |
175 Out[]: [5, 4, 3, 2, 1] |
70 \end{lstlisting} |
176 \end{lstlisting} |
71 Tuples(Immutable lists) |
177 Since \typ{final} is left blank, it will traverse through whole list in reverse manner.\\ |
72 \begin{lstlisting} |
178 \textbf{Note:} While \typ{reverse} reverses the original list, slicing will just result in a instance list with reverse of original, which can be used and worked upon independently. |
73 In []: t = (1, 2, 3, 4, 5, 6, 7, 8) |
179 %%Should we include list copy concept here? |
74 In []: t[0] + t[3] + t[-1] |
180 \subsection{Containership} |
|
181 \typ{in} keyword is used to check for containership of any element in a given list. |
|
182 \begin{lstlisting} |
|
183 In []: a = [2, 5, 4, 6, 9] |
|
184 In []: 4 in a |
|
185 Out[]: True |
|
186 |
|
187 In []: b = 15 |
|
188 In []: b in a |
|
189 Out[]: False |
|
190 \end{lstlisting} |
|
191 \section{Tuples} |
|
192 Tuples are sequences just like Lists, but they are \textbf{immutable}, or items/elements cannot be changed in any way. |
|
193 \begin{lstlisting} |
|
194 In []: t = (1, 2, 3, 4, 5, 6, 7, 8) |
|
195 \end{lstlisting} |
|
196 \textbf{Note:} For tupels we use parantheses in place of square brackets, rest is same as lists. |
|
197 \begin{lstlisting} |
|
198 In []: t[0] + t[3] + t[-1] # elements are accessed via indices |
75 Out[]: 13 |
199 Out[]: 13 |
76 In []: t[4] = 7 # ERROR: tuples are immutable |
200 In []: t[4] = 7 # ERROR: tuples are immutable |
77 \end{lstlisting} |
201 \end{lstlisting} |
78 Sets |
202 \textbf{Note:} elements cant be changed! |
|
203 \section{Dictionaries} |
|
204 statistics section? |
|
205 \section{Sets} |
|
206 are an unordered collection of unique elements.\\ |
|
207 Creation: |
|
208 \begin{lstlisting} |
|
209 In []: s = set([2,4,7,8,5]) # creating a basic set |
|
210 In []: s |
|
211 Out[]: set([2, 4, 5, 7, 8]) |
|
212 In []: g = set([2, 4, 5, 7, 4, 0, 5]) |
|
213 In []: g |
|
214 Out[]: set([0, 2, 4, 5, 7]) # No repetition allowed. |
|
215 \end{lstlisting} |
|
216 Some other operations which can be performed on sets are: |
79 \begin{lstlisting} |
217 \begin{lstlisting} |
80 In []: f = set([1,2,3,5,8]) |
218 In []: f = set([1,2,3,5,8]) |
81 In []: p = set([2,3,5,7]) |
219 In []: p = set([2,3,5,7]) |
82 In []: f | p # Union of two sets |
220 In []: f | p # Union of two sets |
83 Out[]: set([1, 2, 3, 5, 7, 8]) |
221 Out[]: set([1, 2, 3, 5, 7, 8]) |
84 In []: g = set([2, 4, 5, 7, 4, 0, 5]) |
222 In []: f & p # Intersection of two sets |
85 In []: g |
223 Out[]: set([2, 3, 5]) |
86 Out[]: set([2, 4, 5, 7, 0]) # No repetition allowed. |
224 In []: f - p # Elements in f not is p |
|
225 Out[]: set([1, 8]) |
|
226 In []: f ^ p # (f - p) | (p - f) |
|
227 Out[]: set([1, 7, 8])) |
|
228 In []: set([2,3]) < p # Test for subset |
|
229 Out[]: True |
87 \end{lstlisting} |
230 \end{lstlisting} |
88 \end{document} |
231 \end{document} |
89 |
232 |