day2/cheatsheet2.tex
changeset 334 2214b5dba4d4
parent 330 46533051b9d3
equal deleted inserted replaced
333:25b18b51be41 334:2214b5dba4d4
    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 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}, default is taken as 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{first} argument (in this case \typ{start}), 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 third argument is mentioned(\typ{step}), 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 to 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{\typ{list} operations}
       
   103 \begin{lstlisting}
       
   104 In []: num += [9, 10, 11] # Concatenating 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(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 tuples we use parentheses 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 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 \begin{lstlisting}
       
   232 In []: player['Name'] = 'Dravid'
       
   233 In []: player
       
   234 Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, 
       
   235           'Name': 'Dravid', 'Runs': 10823}
       
   236 \end{lstlisting}
       
   237 \textbf{Note:} Duplicate keys are overwritten!
       
   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, never on '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}
       
   255 \section{Sets}
       
   256 are an unordered collection of unique elements.\\
       
   257 Creation:
       
   258 \begin{lstlisting}
       
   259 In []: s = set([2,4,7,8,5]) # creating a basic set
       
   260 In []: s
       
   261 Out[]: set([2, 4, 5, 7, 8])
       
   262 In []: g = set([2, 4, 5, 7, 4, 0, 5])
       
   263 In []: g
       
   264 Out[]: set([0, 2, 4, 5, 7]) # No repetition allowed.
       
   265 \end{lstlisting}
       
   266 Some other operations which can be performed on sets are:
    79 \begin{lstlisting}
   267 \begin{lstlisting}
    80 In []: f = set([1,2,3,5,8])
   268 In []: f = set([1,2,3,5,8])
    81 In []: p = set([2,3,5,7])
   269 In []: p = set([2,3,5,7])
    82 In []: f | p # Union of two sets
   270 In []: f | p # Union of two sets
    83 Out[]: set([1, 2, 3, 5, 7, 8])
   271 Out[]: set([1, 2, 3, 5, 7, 8])
    84 In []: g = set([2, 4, 5, 7, 4, 0, 5])
   272 In []: f & p # Intersection of two sets
    85 In []: g
   273 Out[]: set([2, 3, 5])
    86 Out[]: set([2, 4, 5, 7, 0]) # No repetition allowed.
   274 In []: f - p # Elements in f not is p
       
   275 Out[]: set([1, 8])
       
   276 In []: f ^ p # (f - p) | (p - f)
       
   277 Out[]: set([1, 7, 8])) 
       
   278 In []: set([2,3]) < p # Test for subset
       
   279 Out[]: True
    87 \end{lstlisting}
   280 \end{lstlisting}
    88 \end{document}
   281 \end{document}
    89