199 Out[]: 13 |
199 Out[]: 13 |
200 In []: t[4] = 7 # ERROR: tuples are immutable |
200 In []: t[4] = 7 # ERROR: tuples are immutable |
201 \end{lstlisting} |
201 \end{lstlisting} |
202 \textbf{Note:} elements cant be changed! |
202 \textbf{Note:} elements cant be changed! |
203 \section{Dictionaries} |
203 \section{Dictionaries} |
204 statistics section? |
204 Dictionaries are data structures that provide key-value mappings. They are similar to lists except that instead of the values having integer indexes, they have keys or strings as indexes.\\ |
|
205 A simple dictionary can be created by: |
|
206 \begin{lstlisting} |
|
207 In []: player = {'Mat': 134,'Inn': 233, |
|
208 'Runs': 10823, 'Avg': 52.53} |
|
209 \end{lstlisting} |
|
210 For above case, value on left of ':' is key and value on right is corresponding value. To retrieve value related to key 'Avg' |
|
211 \begin{lstlisting} |
|
212 In []: player['Avg'] |
|
213 Out[]: 52.530000000000001 |
|
214 \end{lstlisting} |
|
215 \subsection{Element operations} |
|
216 \begin{lstlisting} |
|
217 In []: player['Name'] = 'Rahul Dravid' #Adds new key-value pair. |
|
218 In []: player |
|
219 Out[]: |
|
220 {'Avg': 52.530000000000001, |
|
221 'Inn': 233, |
|
222 'Mat': 134, |
|
223 'Name': 'Rahul Dravid', |
|
224 'Runs': 10823} |
|
225 In []: player.pop('Mat') # removing particular key-value pair |
|
226 Out[]: 134 |
|
227 In [21]: player |
|
228 Out[21]: {'Avg': 52.530000000000001, 'Inn': 233, |
|
229 'Name': 'Rahul Dravid', 'Runs': 10823} |
|
230 \end{lstlisting} |
|
231 \textbf{Note:} Duplicate keys are overwritten!\\ |
|
232 \begin{lstlisting} |
|
233 In []: player['Name'] = 'Dravid' |
|
234 In []: player |
|
235 Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, |
|
236 'Name': 'Dravid', 'Runs': 10823} |
|
237 \end{lstlisting} |
|
238 \subsection{containership} |
|
239 \begin{lstlisting} |
|
240 In []: 'Inn' in player |
|
241 Out[]: True |
|
242 In []: 'Econ' in player |
|
243 Out[]: False |
|
244 \end{lstlisting} |
|
245 \textbf{Note:} Containership is always checked on 'keys' of dictionary but not 'values'.\\ |
|
246 \subsection{Methods} |
|
247 \begin{lstlisting} |
|
248 In []: player.keys() # returns list of all keys |
|
249 Out[]: ['Runs', 'Inn', 'Avg', 'Mat'] |
|
250 |
|
251 In []: player.values() # returns list of all values. |
|
252 Out[]: [10823, 233, |
|
253 52.530000000000001, 134] |
|
254 \end{lstlisting} |
205 \section{Sets} |
255 \section{Sets} |
206 are an unordered collection of unique elements.\\ |
256 are an unordered collection of unique elements.\\ |
207 Creation: |
257 Creation: |
208 \begin{lstlisting} |
258 \begin{lstlisting} |
209 In []: s = set([2,4,7,8,5]) # creating a basic set |
259 In []: s = set([2,4,7,8,5]) # creating a basic set |