71 % } |
71 % } |
72 |
72 |
73 |
73 |
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
75 % Title page |
75 % Title page |
76 \title[Basic Python]{Python:\\A formal approach} |
76 \title[Basic Python]{Python language: Basics} |
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, November 2009\\Day 2, Session 1} |
81 \date[] {1, November 2009\\Day 2, Session 1} |
367 In [3]: a = "Now I am a string!" |
367 In [3]: a = "Now I am a string!" |
368 \end{lstlisting} |
368 \end{lstlisting} |
369 \item Comments: |
369 \item Comments: |
370 \begin{lstlisting} |
370 \begin{lstlisting} |
371 In [4]: a = 1 # In-line comments |
371 In [4]: a = 1 # In-line comments |
372 In [5]: # Comment in a line to itself. |
372 In [5]: # A comment line. |
373 In [6]: a = "# This is not a comment!" |
373 In [6]: a = "# Not a comment!" |
374 \end{lstlisting} |
374 \end{lstlisting} |
375 \end{itemize} |
375 \end{itemize} |
376 \inctime{15} |
376 \inctime{15} |
377 \end{frame} |
377 \end{frame} |
378 |
378 |
379 \section{Simple IO} |
379 \section{Simple IO} |
380 \begin{frame}{Simple IO} |
380 \begin{frame}[fragile] |
381 \begin{block} |
381 \frametitle{Simple IO: Console Input} |
382 {Console Input} |
382 \begin{itemize} |
383 \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\ |
383 \item raw\_input() waits for user input. |
384 All keystrokes are Strings!\\\texttt{int()} converts string to int. |
384 \begin{lstlisting} |
385 \end{block} |
385 In [1]: a = raw_input() |
386 \begin{block} |
386 5 |
387 {Console output} |
387 |
388 \texttt{print} is straight forward. Note the distinction between \texttt{print x} and \texttt{print x,} |
388 In [2]: a = raw_input('prompt > ') |
389 \end{block} |
389 prompt > 5 |
|
390 \end{lstlisting} |
|
391 \item Prompt string is optional. |
|
392 \item All keystrokes are Strings! |
|
393 \item \texttt{int()} converts string to int. |
|
394 \end{itemize} |
|
395 \end{frame} |
|
396 |
|
397 \begin{frame}{Simple IO: Console output} |
|
398 \begin{itemize} |
|
399 \item \texttt{print} is straight forward |
|
400 \item Note the distinction between \texttt{print x} and \texttt{print x,} |
|
401 \end{itemize} |
390 \end{frame} |
402 \end{frame} |
391 |
403 |
392 \section{Control flow} |
404 \section{Control flow} |
393 \begin{frame} |
405 \begin{frame} |
394 \frametitle{Control flow constructs} |
406 \frametitle{Control flow constructs} |
435 |
447 |
436 \begin{frame}[fragile] |
448 \begin{frame}[fragile] |
437 \frametitle{\typ{range()}} |
449 \frametitle{\typ{range()}} |
438 \kwrd{range([start,] stop[, step])}\\ |
450 \kwrd{range([start,] stop[, step])}\\ |
439 \begin{itemize} |
451 \begin{itemize} |
440 \item \alert {range() returns a list of integers} |
452 \item range() returns a list of integers |
441 \item \alert {The start and the step arguments are optional} |
453 \item The \emph{start} and the \emph{step} arguments are optional |
|
454 \item \emph{stop} argument is not included in the list |
442 \end{itemize} |
455 \end{itemize} |
443 \end{frame} |
456 \end{frame} |
444 |
457 |
445 \begin{frame}[fragile] |
458 \begin{frame}[fragile] |
446 \frametitle{\typ{for} \ldots \typ{range()}} |
459 \frametitle{\typ{for} \ldots \typ{range()}} |
447 Example: print squares of first \typ{n} numbers |
460 Example: print squares of first \typ{n} numbers |
448 \begin{lstlisting} |
461 \begin{lstlisting} |
449 In []: for i in range(5): |
462 In []: for i in range(5): |
450 ....: print i, i * i |
463 ....: print i, i * i |
451 ....: |
464 ....: |
452 ....: |
465 ....: |
453 0 0 |
466 0 0 |
454 1 1 |
467 1 1 |
455 2 4 |
468 2 4 |
456 3 9 |
469 3 9 |
457 4 16 |
470 4 16 |
458 \end{lstlisting} |
471 \end{lstlisting} |
459 \inctime{5} |
472 \inctime{5} |
460 \end{frame} |
473 \end{frame} |
461 |
474 |
462 \subsection{Exercises} |
475 \subsection{Exercises} |
463 \begin{frame} |
476 |
464 \frametitle{Problem set 1} |
477 \begin{frame}{Problem set 1: Problem 1.1} |
465 \begin{itemize} |
|
466 \item All the problems can be\\ |
|
467 solved using \kwrd{if} and \kwrd{while} |
|
468 \end{itemize} |
|
469 \end{frame} |
|
470 |
|
471 \begin{frame}{Problem 1.1} |
|
472 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$\\ |
478 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$\\ |
473 \vspace*{0.2in} |
479 \vspace*{0.2in} |
474 \emphbar{These are called $Armstrong$ numbers.} |
480 \emphbar{These are called $Armstrong$ numbers.} |
475 \end{frame} |
481 \end{frame} |
476 |
482 |
477 \begin{frame}{Problem 1.2 - Collatz sequence} |
483 \begin{frame}{Problem 1.2 - Collatz sequence} |
478 \begin{enumerate} |
484 \begin{enumerate} |
479 \item Start with an arbitrary (positive) integer. |
485 \item Start with an arbitrary (positive) integer. |
480 \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1. |
486 \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1. |
481 \item Repeat the procedure with the new number. |
487 \item Repeat the procedure with the new number. |
493 4 4 4 4 |
499 4 4 4 4 |
494 \end{lstlisting} |
500 \end{lstlisting} |
495 The number of lines must be obtained from the user as input.\\ |
501 The number of lines must be obtained from the user as input.\\ |
496 \pause |
502 \pause |
497 \emphbar{When can your code fail?} |
503 \emphbar{When can your code fail?} |
498 \only<2->{\inctime{10}} |
504 \inctime{5} |
499 \end{frame} |
505 \end{frame} |
500 |
506 |
501 \begin{frame}[fragile] |
507 \begin{frame}[fragile] |
502 \frametitle{What did we learn?} |
508 \frametitle{What did we learn?} |
503 \begin{itemize} |
509 \begin{itemize} |
504 \item Basic data types |
510 \item Basic data types |
505 \item Arithematic, logical and relational operations |
511 \item Operators |
506 \item Conditional structures |
512 \item Conditional structures |
507 \item Loops |
513 \item Loops |
508 \end{itemize} |
514 \end{itemize} |
509 \end{frame} |
515 \end{frame} |
510 |
516 |