summaryrefslogtreecommitdiff
path: root/day2/cheatsheet2.tex
blob: 05f248d70c046b529d41083c284ddd456a9713ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
\documentclass[12pt]{article}


\title{Python: Data Structures}
\author{FOSSEE}
\usepackage{listings}
\lstset{language=Python,
    basicstyle=\ttfamily,
commentstyle=\itshape\bfseries, 
showstringspaces=false
}
\newcommand{\typ}[1]{\lstinline{#1}}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}

\begin{document}
\date{}
\vspace{-1in}
\begin{center}
\LARGE{Python: Data Structures}\\
\large{FOSSEE}
\end{center}
\section{Basic Looping}
\subsection{\typ{while}}
  \begin{lstlisting}
In []: a, b = 0, 1
In []: while b < 10:  
  ...:     print b,
  ...:     a, b = b, a + b # Fibonacci Sequence
  ...:
\end{lstlisting}
Basic syntax of \typ{while} loop is:
\begin{lstlisting}
while condition:
    statement1
    statement2
\end{lstlisting}
All the statements are executed, till the condition statement evaluates to True.
\subsection{\typ{for} and \typ{range}}
\typ{range(start, stop, step)}\\
returns a list containing an arithmetic progression of integers.\\
Of the arguments mentioned above, both start and step are optional.\\
For example, if we skip third argument, i.e \typ{step}, then default value is 1. So:
\begin{lstlisting}
In []: range(1,10)
Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
\end{lstlisting}
\textbf{Note:} stop value is not included in the list.\\
Similarly if we don't pass \typ{start} argument, default is taken to be 0.
\begin{lstlisting}
In []: range(10)
Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\end{lstlisting}
In case \typ{step} is mentioned, the jump between consecutive members of the list would be equal to that.
\begin{lstlisting}
In []: range(1,10,2)
Out[]: [1, 3, 5, 7, 9]
\end{lstlisting}
%Notice the jump between two consecutive elements is 2, i.e step.\\
\typ{for} and \typ{range}\\
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:
\begin{lstlisting}
In []: for i in range(5):
 ....:     print i, i * i
 ....:
 ....:
0 0
1 1
2 4
3 9
4 16
\end{lstlisting}

\section{list}
\begin{lstlisting}
In []: num = [1, 2, 3, 4] # Initializing a list
In []: num
Out[]: [1, 2, 3, 4]
\end{lstlisting}
\subsection{Accessing individual elements}
\begin{lstlisting}
In []: num[1]
Out[]: 2  
\end{lstlisting}
\textbf{Note:} Index of list starts from 0.
\begin{lstlisting}
In []: num[5]  # ERROR: throws a index error
IndexError: list index out of range
In []: num[-1]
Out[]: 4
\end{lstlisting}
\textbf{Note: }\typ{-1} points the last element in a list. Similarly to access third last element of a list one can use: 
\begin{lstlisting}
In []: num[-3]
Out[]: 2  
\end{lstlisting}
\subsection{Adding lists and elements}
\begin{lstlisting}
In []: num += [9, 10, 11] # Adding two lists
In []: num
Out[]: [1, 2, 3, 4, 9, 10, 11]
\end{lstlisting}
\typ{list} provides \typ{append} function to append objects at the end. 
\begin{lstlisting}
In []: num = [1, 2, 3, 4] 
In []: num.append(-2) 
In []: num
Out[]: [1, 2, 3, 4, -2]
\end{lstlisting}
%% In []: num += [-5] 
%% In []: num
%% Out[]: [1, 2, 3, 4, -2, -5]
Working of \typ{append} is different from \typ{+} operator on list. Till here both will behave as same. But in following case:
\begin{lstlisting}
In []: num = [1, 2, 3, 4]

In []: num + [9, 10, 11]
Out[]: [1, 2, 3, 4, 9, 10, 11]

In []: num.append([9, 10, 11]) # appending a list to a list

In []: num
Out[]: [1, 2, 3, 4, [9, 10, 11]] # last element is a list
\end{lstlisting}
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.\\
\subsection{Miscellaneous}
\begin{lstlisting}
In []: num = [1, 2, 3, 4]
In []: num.extend([5, 6, 7])  # extend list by adding elements
In []: num
Out[]: [1, 2, 3, 4, 5, 6, 7]
In []: num.reverse()  # reverse the current list
In []: num
Out[]: [7, 6, 5, 4, 3, 2, 1]
In []: num.remove(6) # removing first occurrence of 6
In []: num
Out[]: [7, 5, 4, 3, 2, 1]
In []: len(num) # returns the length of list
Out[]: 6
In []: a = [1, 5, 3, 7, -2, 4]
In []: min(a) # returns smallest item in a list.
Out[]: -2
In []: max(a) # returns largest item in a list.
Out[]: 7
\end{lstlisting}

\subsection{Slicing} 
General syntax for getting slice out of a list is \\
\typ{list[initial:final:step]}
\begin{lstlisting}
In []: a = [1, 2, 3, 4, 5]
In []: a[1:-1:2] 
Out[]: [2, 4]
\end{lstlisting}
Start slice from second element(1), till the last element(-1) with step size of 2.
\begin{lstlisting}
In []: a[::2]
Out[]: [1, 3, 5]
\end{lstlisting}
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.\\
Apart from using \typ{reverse} command on list, one can also use slicing in special way to get reverse of a list.
\begin{lstlisting}
In []: a[-1:-4:-1]
Out[]: [5, 4, 3]
\end{lstlisting}
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.''\\
So to get reverse of whole list one can write following slice syntax:
\begin{lstlisting}
In []: a[-1::-1]
Out[]: [5, 4, 3, 2, 1]
\end{lstlisting}
Since \typ{final} is left blank, it will traverse through whole list in reverse manner.\\
\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.
%%Should we include  list copy concept here?
\subsection{Containership}
\typ{in} keyword is used to check for containership of any element in a given list.
\begin{lstlisting}
In []: a = [2, 5, 4, 6, 9]
In []: 4 in a
Out[]: True

In []: b = 15
In []: b in a
Out[]: False
\end{lstlisting}
\section{Tuples}
Tuples are sequences just like Lists, but they are \textbf{immutable}, or items/elements cannot be changed in any way.
\begin{lstlisting}
In []: t = (1, 2, 3, 4, 5, 6, 7, 8) 
\end{lstlisting}
\textbf{Note:} For tupels we use parantheses in place of square brackets, rest is same as lists.
\begin{lstlisting}
In []: t[0] + t[3] + t[-1] # elements are accessed via indices
Out[]: 13
In []: t[4] = 7 # ERROR: tuples are immutable
\end{lstlisting}
\textbf{Note:} elements cant be changed!
\section{Dictionaries}
statistics section? 
\section{Sets}
are an unordered collection of unique elements.\\
Creation:
\begin{lstlisting}
In []: s = set([2,4,7,8,5]) # creating a basic set
In []: s
Out[]: set([2, 4, 5, 7, 8])
In []: g = set([2, 4, 5, 7, 4, 0, 5])
In []: g
Out[]: set([0, 2, 4, 5, 7]) # No repetition allowed.
\end{lstlisting}
Some other operations which can be performed on sets are:
\begin{lstlisting}
In []: f = set([1,2,3,5,8])
In []: p = set([2,3,5,7])
In []: f | p # Union of two sets
Out[]: set([1, 2, 3, 5, 7, 8])
In []: f & p # Intersection of two sets
Out[]: set([2, 3, 5])
In []: f - p # Elements in f not is p
Out[]: set([1, 8])
In []: f ^ p # (f - p) | (p - f)
Out[]: set([1, 7, 8])) 
In []: set([2,3]) < p # Test for subset
Out[]: True
\end{lstlisting}
\end{document}