76 \title[Basic Python]{Python language: Data structures and functions} |
76 \title[Basic Python]{Python language: Data structures and functions} |
77 |
77 |
78 \author[FOSSEE Team] {The FOSSEE Group} |
78 \author[FOSSEE Team] {The FOSSEE Group} |
79 |
79 |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
81 \date[] {1 May, 2010\\Day 2, Session 2} |
81 \date[] {SciPy.in 2010, Tutorials} |
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
83 |
83 |
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
85 %\logo{\pgfuseimage{iitmlogo}} |
85 %\logo{\pgfuseimage{iitmlogo}} |
86 |
86 |
121 \frametitle{Outline} |
121 \frametitle{Outline} |
122 \tableofcontents |
122 \tableofcontents |
123 % You might wish to add the option [pausesections] |
123 % You might wish to add the option [pausesections] |
124 \end{frame} |
124 \end{frame} |
125 |
125 |
126 \section{Control flow} |
|
127 \subsection{Basic Looping} |
|
128 \begin{frame}[fragile] |
|
129 \frametitle{\typ{while}} |
|
130 \begin{block}{Example: Fibonacci series} |
|
131 Sum of previous two elements defines the next |
|
132 \end{block} |
|
133 \begin{lstlisting} |
|
134 In []: a, b = 0, 1 |
|
135 In []: while b < 10: |
|
136 ...: print b, |
|
137 ...: a, b = b, a + b |
|
138 ...: |
|
139 ...: |
|
140 \end{lstlisting} |
|
141 \typ{1 1 2 3 5 8}\\ |
|
142 \end{frame} |
|
143 |
|
144 \begin{frame}[fragile] |
|
145 \frametitle{\typ{range()}} |
|
146 \kwrd{range([start,] stop[, step])}\\ |
|
147 \begin{itemize} |
|
148 \item \typ{range()} returns a list of integers |
|
149 \item The \typ{start} and the \typ{step} arguments are optional |
|
150 \item \typ{stop} is not included in the list |
|
151 \end{itemize} |
|
152 \vspace*{.5in} |
|
153 \begin{block}{Documentation convention} |
|
154 \begin{itemize} |
|
155 \item \alert{Anything within \typ{[]} is optional} |
|
156 \item Nothing to do with Python. |
|
157 \end{itemize} |
|
158 \end{block} |
|
159 \end{frame} |
|
160 |
|
161 \begin{frame}[fragile] |
|
162 \frametitle{\texttt{for} \ldots \typ{range()}} |
|
163 Example: print squares of first \typ{5} numbers |
|
164 \begin{lstlisting} |
|
165 In []: for i in range(5): |
|
166 ....: print i, i * i |
|
167 ....: |
|
168 ....: |
|
169 0 0 |
|
170 1 1 |
|
171 2 4 |
|
172 3 9 |
|
173 4 16 |
|
174 \end{lstlisting} |
|
175 \end{frame} |
|
176 |
|
177 \begin{frame}[fragile] |
|
178 \frametitle{\texttt{for} \ldots \typ{range()}} |
|
179 Example: print squares of odd numbers from 3 to 9 |
|
180 \begin{lstlisting} |
|
181 In []: for i in range(3, 10, 2): |
|
182 ....: print i, i * i |
|
183 ....: |
|
184 ....: |
|
185 3 9 |
|
186 5 25 |
|
187 7 49 |
|
188 9 81 |
|
189 \end{lstlisting} |
|
190 \inctime{5} |
|
191 \end{frame} |
|
192 |
|
193 \subsection{Exercises} |
|
194 |
|
195 \begin{frame}{Problem set 1: Problem 1.1} |
|
196 Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbers $abc$ that have the property $abc = a^3 + b^3 + c^3$\\ |
|
197 For example, $153 = 1^3 + 5^3 + 3^3$\\ |
|
198 \vspace*{0.2in} |
|
199 \emphbar{These are called $Armstrong$ numbers.} |
|
200 \end{frame} |
|
201 |
|
202 \begin{frame}{Problem 1.2 - Collatz sequence} |
|
203 \begin{enumerate} |
|
204 \item Start with an arbitrary (positive) integer. |
|
205 \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1. |
|
206 \item Repeat the procedure with the new number. |
|
207 \item It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops. |
|
208 \end{enumerate} |
|
209 Write a program that accepts the starting value and prints out the Collatz sequence. |
|
210 \inctime{5} |
|
211 \end{frame} |
|
212 |
|
213 \section{Data structures} |
126 \section{Data structures} |
214 \subsection{Lists} |
127 \subsection{Lists} |
215 \begin{frame}[fragile] |
128 \begin{frame}[fragile] |
216 \frametitle{Lists} |
129 \frametitle{Lists} |
217 \begin{block}{We already know that} |
130 \begin{block}{We already know that} |
251 Out[]: [6, 5, 4, 7, 3, 2, 8, 9] |
164 Out[]: [6, 5, 4, 7, 3, 2, 8, 9] |
252 |
165 |
253 In []: num.remove(6) |
166 In []: num.remove(6) |
254 In []: num |
167 In []: num |
255 \end{lstlisting} |
168 \end{lstlisting} |
256 \end{frame} |
|
257 |
|
258 \begin{frame}[fragile] |
|
259 \frametitle{Lists: slicing} |
|
260 \begin{itemize} |
|
261 \item \typ{list[initial:final]} |
|
262 \end{itemize} |
|
263 \begin{lstlisting} |
|
264 In []: a = [1, 2, 3, 4, 5] |
|
265 |
|
266 In []: a[1:3] |
|
267 Out[]: [2, 3] |
|
268 |
|
269 In []: a[1:-1] |
|
270 Out[]: [2, 3, 4] |
|
271 |
|
272 In []: a[:3] |
|
273 Out[]: [1, 2, 3] |
|
274 \end{lstlisting} |
|
275 \end{frame} |
|
276 |
|
277 \begin{frame}[fragile] |
|
278 \frametitle{Lists: slicing} |
|
279 \begin{itemize} |
|
280 \item \typ{list[initial:final:step]} |
|
281 \end{itemize} |
|
282 \begin{lstlisting} |
|
283 In []: a[1:-1:2] |
|
284 Out[]: [2, 4] |
|
285 |
|
286 In []: a[::2] |
|
287 Out[]: [1, 3, 5] |
|
288 |
|
289 In []: a[::-1] |
|
290 Out[]: [5, 4, 3, 2, 1] |
|
291 \end{lstlisting} |
|
292 \end{frame} |
169 \end{frame} |
293 |
170 |
294 \begin{frame}[fragile] |
171 \begin{frame}[fragile] |
295 \frametitle{List containership} |
172 \frametitle{List containership} |
296 \emphbar{Recall \typ{num} is \typ{[9, 8, 2, 3, 7]}} |
173 \emphbar{Recall \typ{num} is \typ{[9, 8, 2, 3, 7]}} |
339 They need not be of the same type! |
216 They need not be of the same type! |
340 \end{block} |
217 \end{block} |
341 \end{frame} |
218 \end{frame} |
342 |
219 |
343 \subsection{Dictionaries} |
220 \subsection{Dictionaries} |
344 \begin{frame}[fragile] |
221 |
345 \frametitle{Dictionaries: recall} |
222 \begin{frame}[fragile] |
346 \begin{lstlisting} |
223 \frametitle{Dictionaries: Introduction} |
347 In []: player = {'Mat': 134,'Inn': 233, |
224 \begin{itemize} |
348 'Runs': 10823, 'Avg': 52.53} |
225 \item Lists index using integers\\ |
349 |
226 Recall \typ{p = [2, 3, 5, 7]} and\\ |
350 In []: player['Avg'] |
227 \typ{p[1]} is equal to \typ{3} |
351 Out[]: 52.530000000000001 |
228 \item Dictionaries index using strings |
352 \end{lstlisting} |
229 \end{itemize} |
353 \begin{block}{Note!} |
230 \end{frame} |
354 Duplicate keys $\Rightarrow$ overwritten!\\ |
231 |
355 You can iterate through a dictionary using keys. |
232 \begin{frame}[fragile] |
356 \end{block} |
233 \frametitle{Dictionaries \ldots} |
|
234 \begin{lstlisting} |
|
235 In []: d = {'png' : 'image file', |
|
236 'txt' : 'text file', |
|
237 'py' : 'python code', |
|
238 'java': 'bad code', |
|
239 'cpp': 'complex code'} |
|
240 |
|
241 In []: d['txt'] |
|
242 Out[]: 'text file' |
|
243 \end{lstlisting} |
|
244 \end{frame} |
|
245 |
|
246 \begin{frame}[fragile] |
|
247 \frametitle{Dictionaries \ldots} |
|
248 \begin{lstlisting} |
|
249 In []: 'py' in d |
|
250 Out[]: True |
|
251 |
|
252 In []: 'jpg' in d |
|
253 Out[]: False |
|
254 \end{lstlisting} |
|
255 \end{frame} |
|
256 |
|
257 \begin{frame}[fragile] |
|
258 \frametitle{Dictionaries \ldots} |
|
259 \begin{small} |
|
260 \begin{lstlisting} |
|
261 In []: d.keys() |
|
262 Out[]: ['cpp', 'py', 'txt', 'java', 'png'] |
|
263 |
|
264 In []: d.values() |
|
265 Out[]: ['complex code', 'python code', |
|
266 'text file', 'bad code', |
|
267 'image file'] |
|
268 \end{lstlisting} |
|
269 \end{small} |
|
270 %% \inctime{10} |
|
271 \end{frame} |
|
272 |
|
273 \begin{frame}[fragile] |
|
274 \frametitle{Inserting elements into dictionary} |
|
275 \emphbar{\alert{\typ{d[key] = value}}} |
|
276 \begin{lstlisting} |
|
277 In []: d['bin'] = 'binary file' |
|
278 In []: d |
|
279 Out[]: |
|
280 {'bin': 'binary file', |
|
281 'cpp': 'complex code', |
|
282 'java': 'bad code', |
|
283 'png': 'image file', |
|
284 'py': 'python code', |
|
285 'txt': 'text file'} |
|
286 \end{lstlisting} |
|
287 \emphbar{\alert{Duplicate keys are overwritten!}} |
357 \end{frame} |
288 \end{frame} |
358 |
289 |
359 \begin{frame}[fragile] |
290 \begin{frame}[fragile] |
360 \frametitle{Dictionaries: containership} |
291 \frametitle{Dictionaries: containership} |
361 \begin{lstlisting} |
292 \begin{lstlisting} |
362 In []: 'Inn' in player |
293 In []: 'bin' in d |
363 Out[]: True |
294 Out[]: True |
364 |
295 |
365 In []: 'Econ' in player |
296 In []: 'hs' in d |
366 Out[]: False |
297 Out[]: False |
367 \end{lstlisting} |
298 \end{lstlisting} |
368 \begin{block}{Note} |
299 \begin{block}{Note} |
369 \begin{itemize} |
300 \begin{itemize} |
370 \item We can check for the containership of keys only |
301 \item We can check for the containership of keys only |
374 \end{frame} |
305 \end{frame} |
375 |
306 |
376 \begin{frame}[fragile] |
307 \begin{frame}[fragile] |
377 \frametitle{Dictionaries: methods} |
308 \frametitle{Dictionaries: methods} |
378 \begin{lstlisting} |
309 \begin{lstlisting} |
379 In []: player.keys() |
310 In []: d.keys() |
380 Out[]: ['Runs', 'Inn', 'Avg', 'Mat'] |
311 Out[]: ['bin', 'java', 'py', 'cpp', 'txt', 'png'] |
381 |
312 |
382 In []: player.values() |
313 In []: d.values() |
383 Out[]: [10823, 233, |
314 Out[]: |
384 52.530000000000001, 134] |
315 ['binary file', |
|
316 'bad code', |
|
317 'python code', |
|
318 'complex code', |
|
319 'text file', |
|
320 'image file'] |
385 \end{lstlisting} |
321 \end{lstlisting} |
386 \end{frame} |
322 \end{frame} |
387 |
323 |
388 \begin{frame} {Problem Set 2.1: Problem 2.1.1} |
324 \begin{frame} {Problem Set 2.1: Problem 2.1.1} |
389 You are given date strings of the form ``29 Jul, 2009'', or ``4 January 2008''. In other words a number, a string and another number, with a comma sometimes separating the items.\\Write a program that takes such a string as input and prints a tuple (yyyy, mm, dd) where all three elements are ints. |
325 You are given date strings of the form ``29 Jul, 2009'', or ``4 January 2008''. In other words a number, a string and another number, with a comma sometimes separating the items.\\Write a program that takes such a string as input and prints a tuple (yyyy, mm, dd) where all three elements are ints. |
452 \frametitle{Problem 2.2.2} |
388 \frametitle{Problem 2.2.2} |
453 Given a list of words, find all the anagrams in the list. |
389 Given a list of words, find all the anagrams in the list. |
454 |
390 |
455 \end{frame} |
391 \end{frame} |
456 |
392 |
457 \section{Functions} |
|
458 \begin{frame}[fragile] |
|
459 \frametitle{Functions} |
|
460 \begin{itemize} |
|
461 \item \kwrd{def} - keyword to define a function |
|
462 \item Arguments are local to a function |
|
463 \item Functions can return multiple values |
|
464 \end{itemize} |
|
465 \end{frame} |
|
466 |
|
467 \begin{frame}[fragile] |
|
468 \frametitle{Functions: example} |
|
469 \begin{lstlisting} |
|
470 def signum( r ): |
|
471 """returns 0 if r is zero |
|
472 -1 if r is negative |
|
473 +1 if r is positive""" |
|
474 if r < 0: |
|
475 return -1 |
|
476 elif r > 0: |
|
477 return 1 |
|
478 else: |
|
479 return 0 |
|
480 \end{lstlisting} |
|
481 \emphbar{Note docstrings} |
|
482 \end{frame} |
|
483 |
|
484 \begin{frame}[fragile] |
|
485 \frametitle {What does this function do?} |
|
486 \begin{lstlisting} |
|
487 def what( n ): |
|
488 if n < 0: n = -n |
|
489 while n > 0: |
|
490 if n % 2 == 1: |
|
491 return False |
|
492 n /= 10 |
|
493 return True |
|
494 \end{lstlisting} |
|
495 \end{frame} |
|
496 |
|
497 \begin{frame}[fragile] |
|
498 {What does this function do?} |
|
499 \begin{lstlisting} |
|
500 def what( n ): |
|
501 i = 1 |
|
502 while i * i < n: |
|
503 i += 1 |
|
504 return i * i == n, i |
|
505 \end{lstlisting} |
|
506 \end{frame} |
|
507 |
|
508 \begin{frame}[fragile] |
|
509 \frametitle {What does this function do?} |
|
510 \begin{lstlisting} |
|
511 def what( x, n ): |
|
512 if n < 0: |
|
513 n = -n |
|
514 x = 1.0 / x |
|
515 |
|
516 z = 1.0 |
|
517 while n > 0: |
|
518 if n % 2 == 1: |
|
519 z *= x |
|
520 x *= x |
|
521 n /= 2 |
|
522 return z |
|
523 \end{lstlisting} |
|
524 \end{frame} |
|
525 |
|
526 \begin{frame} |
393 \begin{frame} |
527 \frametitle{What did we learn?} |
394 \frametitle{What did we learn?} |
528 \begin{itemize} |
395 \begin{itemize} |
529 \item Loops: \kwrd{while}, \kwrd{for} |
|
530 \item Advanced Data structures: |
396 \item Advanced Data structures: |
531 \begin{itemize} |
397 \begin{itemize} |
532 \item Lists |
398 \item Lists |
533 \item Tuples |
399 \item Tuples |
534 \item Dictionaries |
400 \item Dictionaries |
535 \item Sets |
401 \item Sets |
536 \end{itemize} |
402 \end{itemize} |
537 \item Functions |
|
538 \item Docstrings |
|
539 \end{itemize} |
403 \end{itemize} |
540 \end{frame} |
404 \end{frame} |
541 |
405 |
542 \end{document} |
406 \end{document} |
543 |
407 |