day2/cheatsheet2.tex
changeset 329 0a6ab1d81491
parent 327 c78cad28c2f7
child 330 46533051b9d3
equal deleted inserted replaced
328:4075482a9770 329:0a6ab1d81491
    38 \begin{lstlisting}
    38 \begin{lstlisting}
    39 while condition:
    39 while condition:
    40     statement1
    40     statement1
    41     statement2
    41     statement2
    42 \end{lstlisting}
    42 \end{lstlisting}
    43 All the statements are executed, till the condition statement evaluates to True.
    43 All statements are executed, till the condition statement evaluates to True.
    44 \subsection{\typ{for} and \typ{range}}
    44 \subsection{\typ{for} and \typ{range}}
    45 \typ{range(start, stop, step)}\\
    45 \typ{range(start, stop, step)}\\
    46 returns a list containing an arithmetic progression of integers.\\
    46 returns a list containing an arithmetic progression of integers.\\
    47 Of the arguments mentioned above, both start and step are optional.\\
    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:
    48 For example, if we skip third argument, i.e \typ{step}, default is taken as 1. So:
    49 \begin{lstlisting}
    49 \begin{lstlisting}
    50 In []: range(1,10)
    50 In []: range(1,10)
    51 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    51 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    52 \end{lstlisting}
    52 \end{lstlisting}
    53 \textbf{Note:} stop value is not included in the list.\\
    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.
    54 Similarly if we don't pass \typ{first} argument (in this case \typ{start}), default is taken to be 0.
    55 \begin{lstlisting}
    55 \begin{lstlisting}
    56 In []: range(10)
    56 In []: range(10)
    57 Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    57 Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    58 \end{lstlisting}
    58 \end{lstlisting}
    59 In case \typ{step} is mentioned, the jump between consecutive members of the list would be equal to that.
    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}
    60 \begin{lstlisting}
    61 In []: range(1,10,2)
    61 In []: range(1,10,2)
    62 Out[]: [1, 3, 5, 7, 9]
    62 Out[]: [1, 3, 5, 7, 9]
    63 \end{lstlisting}
    63 \end{lstlisting}
    64 %Notice the jump between two consecutive elements is 2, i.e step.\\
    64 %Notice the jump between two consecutive elements is 2, i.e step.\\
    92 In []: num[5]  # ERROR: throws a index error
    92 In []: num[5]  # ERROR: throws a index error
    93 IndexError: list index out of range
    93 IndexError: list index out of range
    94 In []: num[-1]
    94 In []: num[-1]
    95 Out[]: 4
    95 Out[]: 4
    96 \end{lstlisting}
    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: 
    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}
    98 \begin{lstlisting}
    99 In []: num[-3]
    99 In []: num[-3]
   100 Out[]: 2  
   100 Out[]: 2  
   101 \end{lstlisting}
   101 \end{lstlisting}
   102 \subsection{Adding lists and elements}
   102 \subsection{\typ{list} operations}
   103 \begin{lstlisting}
   103 \begin{lstlisting}
   104 In []: num += [9, 10, 11] # Adding two lists
   104 In []: num += [9, 10, 11] # Concatenating two lists
   105 In []: num
   105 In []: num
   106 Out[]: [1, 2, 3, 4, 9, 10, 11]
   106 Out[]: [1, 2, 3, 4, 9, 10, 11]
   107 \end{lstlisting}
   107 \end{lstlisting}
   108 \typ{list} provides \typ{append} function to append objects at the end. 
   108 \typ{list} provides \typ{append} function to append objects at the end. 
   109 \begin{lstlisting}
   109 \begin{lstlisting}
   160 Start slice from second element(1), till the last element(-1) with step size of 2.
   160 Start slice from second element(1), till the last element(-1) with step size of 2.
   161 \begin{lstlisting}
   161 \begin{lstlisting}
   162 In []: a[::2]
   162 In []: a[::2]
   163 Out[]: [1, 3, 5]
   163 Out[]: [1, 3, 5]
   164 \end{lstlisting}
   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.\\
   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.
   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}
   167 \begin{lstlisting}
   168 In []: a[-1:-4:-1]
   168 In []: a[-1:-4:-1]
   169 Out[]: [5, 4, 3]
   169 Out[]: [5, 4, 3]
   170 \end{lstlisting}
   170 \end{lstlisting}
   226 Out[]: 134
   226 Out[]: 134
   227 In [21]: player
   227 In [21]: player
   228 Out[21]: {'Avg': 52.530000000000001, 'Inn': 233, 
   228 Out[21]: {'Avg': 52.530000000000001, 'Inn': 233, 
   229           'Name': 'Rahul Dravid', 'Runs': 10823}
   229           'Name': 'Rahul Dravid', 'Runs': 10823}
   230 \end{lstlisting}
   230 \end{lstlisting}
   231 \textbf{Note:} Duplicate keys are overwritten!\\
       
   232 \begin{lstlisting}
   231 \begin{lstlisting}
   233 In []: player['Name'] = 'Dravid'
   232 In []: player['Name'] = 'Dravid'
   234 In []: player
   233 In []: player
   235 Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, 
   234 Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, 
   236           'Name': 'Dravid', 'Runs': 10823}
   235           'Name': 'Dravid', 'Runs': 10823}
   237 \end{lstlisting}
   236 \end{lstlisting}
       
   237 \textbf{Note:} Duplicate keys are overwritten!
   238 \subsection{containership}
   238 \subsection{containership}
   239 \begin{lstlisting}
   239 \begin{lstlisting}
   240 In []: 'Inn' in player
   240 In []: 'Inn' in player
   241 Out[]: True
   241 Out[]: True
   242 In []: 'Econ' in player
   242 In []: 'Econ' in player
   243 Out[]: False
   243 Out[]: False
   244 \end{lstlisting}
   244 \end{lstlisting}
   245 \textbf{Note:} Containership is always checked on 'keys' of dictionary but not 'values'.\\
   245 \textbf{Note:} Containership is always checked on 'keys' of dictionary, never on 'values'.\\
   246 \subsection{Methods}
   246 \subsection{Methods}
   247 \begin{lstlisting}
   247 \begin{lstlisting}
   248 In []: player.keys() # returns list of all keys
   248 In []: player.keys() # returns list of all keys
   249 Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
   249 Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
   250 
   250 
   277 Out[]: set([1, 7, 8])) 
   277 Out[]: set([1, 7, 8])) 
   278 In []: set([2,3]) < p # Test for subset
   278 In []: set([2,3]) < p # Test for subset
   279 Out[]: True
   279 Out[]: True
   280 \end{lstlisting}
   280 \end{lstlisting}
   281 \end{document}
   281 \end{document}
   282