changeset 359 | cb17c87b090e |
parent 344 | 19754ed6050f |
child 366 | ec4cb3ba7f09 |
358:162e3e453920 | 359:cb17c87b090e |
---|---|
449 \item raw\_input() waits for user input. |
449 \item raw\_input() waits for user input. |
450 \begin{lstlisting} |
450 \begin{lstlisting} |
451 In []: a = raw_input() |
451 In []: a = raw_input() |
452 5 |
452 5 |
453 |
453 |
454 In []: a |
|
455 Out[]: '5' |
|
456 |
|
454 In []: a = raw_input('Enter a value: ') |
457 In []: a = raw_input('Enter a value: ') |
455 Enter a value: 5 |
458 Enter a value: 5 |
456 \end{lstlisting} |
459 \end{lstlisting} |
457 \item Prompt string is optional. |
460 \item Prompt string is optional. |
458 \item All keystrokes are Strings! |
461 \item All keystrokes are Strings! |
461 \end{frame} |
464 \end{frame} |
462 |
465 |
463 \begin{frame}[fragile] |
466 \begin{frame}[fragile] |
464 \frametitle{Simple IO: Console output} |
467 \frametitle{Simple IO: Console output} |
465 \begin{itemize} |
468 \begin{itemize} |
466 \item \texttt{print} is straight forward |
469 \item \typ{print} is straight forward |
467 \item Put the following code snippet in a file \emph{hello1.py} |
470 \item Put the following code snippet in a file \typ{hello1.py} |
468 \end{itemize} |
471 \end{itemize} |
469 \begin{lstlisting} |
472 \begin{lstlisting} |
470 print "Hello" |
473 print "Hello" |
471 print "World" |
474 print "World" |
472 |
475 |
477 \end{lstlisting} |
480 \end{lstlisting} |
478 \end{frame} |
481 \end{frame} |
479 |
482 |
480 \begin{frame}[fragile] |
483 \begin{frame}[fragile] |
481 \frametitle{Simple IO: Console output \ldots} |
484 \frametitle{Simple IO: Console output \ldots} |
482 Put the following code snippet in a file \emph{hello2.py} |
485 Put the following code snippet in a file \typ{hello2.py} |
483 \begin{lstlisting} |
486 \begin{lstlisting} |
484 print "Hello", |
487 print "Hello", |
485 print "World" |
488 print "World" |
486 |
489 |
487 |
490 |
488 In []: %run -i hello2.py |
491 In []: %run -i hello2.py |
489 Hello World |
492 Hello World |
490 \end{lstlisting} |
493 \end{lstlisting} |
491 |
494 |
492 \emphbar{Note the distinction between \texttt{print x} and \texttt{print x,}} |
495 \emphbar{Note the distinction between \typ{print x} and \typ{print x,}} |
493 \end{frame} |
496 \end{frame} |
494 |
497 |
495 \section{Control flow} |
498 \section{Control flow} |
496 \begin{frame} |
499 \begin{frame} |
497 \frametitle{Control flow constructs} |
500 \frametitle{Control flow constructs} |
498 \begin{itemize} |
501 \begin{itemize} |
499 \item \kwrd{if/elif/else}: branching |
502 \item \kwrd{if/elif/else}: branching |
500 \item \kwrd{C if X else Y}: Ternary conditional operator |
503 \item \kwrd{C if X else D}: Ternary conditional operator |
501 \item \kwrd{while}: looping |
504 \item \kwrd{while}: looping |
502 \item \kwrd{for}: iterating |
505 \item \kwrd{for}: iterating |
503 \item \kwrd{break, continue}: modify loop |
506 \item \kwrd{break, continue}: modify loop |
504 \item \kwrd{pass}: syntactic filler |
507 \item \kwrd{pass}: syntactic filler |
505 \end{itemize} |
508 \end{itemize} |
506 \end{frame} |
509 \end{frame} |
507 |
510 |
508 \subsection{Basic Conditional flow} |
511 \subsection{Basic Conditional flow} |
509 \begin{frame}[fragile] |
512 \begin{frame}[fragile] |
510 \frametitle{\typ{If...elif...else} example} |
513 \frametitle{\typ{If...elif...else} example} |
514 Type out the code below in an editor. |
|
511 \small |
515 \small |
512 \begin{lstlisting} |
516 \begin{lstlisting} |
513 In []: x = int(raw_input("Enter an integer:")) |
517 x = int(raw_input("Enter an integer:")) |
514 |
518 if x < 0: |
515 In []: if x < 0: |
519 print 'Be positive!' |
516 ...: print 'Be positive!' |
520 elif x == 0: |
517 ...: elif x == 0: |
521 print 'Zero' |
518 ...: print 'Zero' |
522 elif x == 1: |
519 ...: elif x == 1: |
523 print 'Single' |
520 ...: print 'Single' |
524 else: |
521 ...: else: |
525 print 'More' |
522 ...: print 'More' |
526 |
523 ...: |
|
524 ...: |
|
525 \end{lstlisting} |
527 \end{lstlisting} |
526 \inctime{10} |
528 \inctime{10} |
527 \end{frame} |
529 \end{frame} |
528 |
530 |
529 \begin{frame}[fragile] |
531 \begin{frame}[fragile] |
540 \frametitle{What did we learn?} |
542 \frametitle{What did we learn?} |
541 \begin{itemize} |
543 \begin{itemize} |
542 \item Data types: int, float, complex, boolean, string |
544 \item Data types: int, float, complex, boolean, string |
543 \item Operators: +, -, *, /, \%, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c |
545 \item Operators: +, -, *, /, \%, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c |
544 \item Simple IO: \kwrd{raw\_input} and \kwrd{print} |
546 \item Simple IO: \kwrd{raw\_input} and \kwrd{print} |
545 \item Conditional structures: \kwrd{if/elif/else},\\ \kwrd{C if X else Y} |
547 \item Conditional structures: \kwrd{if/elif/else},\\ \kwrd{C if X else D} |
546 \end{itemize} |
548 \end{itemize} |
547 \end{frame} |
549 \end{frame} |
548 |
550 |
549 \end{document} |
551 \end{document} |
550 |
552 |