input_output/script.rst
changeset 410 226b6e789da5
parent 329 5c8e88276e1f
child 418 8a42b4203f6d
equal deleted inserted replaced
409:43a24f7ab183 410:226b6e789da5
    10 .. -------------
    10 .. -------------
    11 
    11 
    12 ..   1. Loops
    12 ..   1. Loops
    13      
    13      
    14 .. Author              : Nishanth Amuluru
    14 .. Author              : Nishanth Amuluru
    15    Internal Reviewer   : 
    15    Internal Reviewer   : Puneeth 
    16    External Reviewer   :
    16    External Reviewer   :
    17    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    17    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    18 
    18 
    19 Script
    19 Script
    20 ------
    20 ------
    24 {{{ Show the slide containing title }}}
    24 {{{ Show the slide containing title }}}
    25 
    25 
    26 {{{ Show the slide containing the outline slide }}}
    26 {{{ Show the slide containing the outline slide }}}
    27 
    27 
    28 Input and Output are used in almost every program we use.
    28 Input and Output are used in almost every program we use.
    29 In this tutorial, we shall learn
    29 In this tutorial, we shall learn how to 
    30 
    30 
    31  * Outputting data
    31  * Output data
    32  * Taking input from the user
    32  * Take input from the user
    33 
    33 
    34 type
    34 type
    35 ::
    35 ::
    36  
    36  
    37     a = "This is a string"
    37     a = "This is a string"
    38     a
    38     a
    39     print a
    39     print a
    40      
    40      
    41 print a prints the value of a which is obvious.
    41 ``print a``, obviously, is printing the value of ``a``.
    42 As you can see, even when you type just a, the value of a is shown.
    42 As you can see, even when you type just a, the value of a is shown.
    43 But there is a difference.
    43 But there is a difference.
    44 
    44 
    45 Typing a shows the value of a while print a prints the string. This difference
    45 Typing a shows the value of a while print a prints the string. This difference
    46 becomes more evident when we use strings with newlines in them.
    46 becomes more evident when we use strings with newlines in them.
    55 While typing print b prints the string and hence the newline.
    55 While typing print b prints the string and hence the newline.
    56 
    56 
    57 Moreover when we type just a, the value a is shown only in interactive mode and
    57 Moreover when we type just a, the value a is shown only in interactive mode and
    58 does not have any effect on the program while running it as a script.
    58 does not have any effect on the program while running it as a script.
    59 
    59 
       
    60 .. #[punch: I think we could show that?]
       
    61 
    60 We shall look at different ways of outputting the data.
    62 We shall look at different ways of outputting the data.
    61 
    63 
    62 print statement also accepts the syntax of C's printf statement.
    64 ``print`` statement also accepts the syntax of C's ``printf`` statement.
    63 Various arguments can be passed to print using modifiers.
    65 Various arguments can be passed to ``print`` using modifiers.
    64 type
    66 type
    65 ::
    67 ::
    66 
    68 
    67     x = 1.5
    69     x = 1.5
    68     y = 2
    70     y = 2
    69     z = "zed"
    71     z = "zed"
    70     print "x is %2.1f y is %d z is %s"%(x,y)
    72     print "x is %2.1f y is %d z is %s"%(x,y)
    71 
    73 
    72 As you can see, the values of x and y are substituted in place of %2.1f and %d
    74 As you can see, the values of x and y are substituted in place of
       
    75 ``%2.1f`` and ``%d`` 
    73 
    76 
    74 {{{ Pause here and try out the following exercises }}}
    77 {{{ Pause here and try out the following exercises }}}
    75 
    78 
    76 %% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)``
    79 %% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)``
    77 
    80 
    78 {{{ continue from paused state }}}
    81 {{{ continue from paused state }}}
    79 
    82 
    80 We see that the int value of x and float value of y are printed corresponding
    83 We see that the ``int`` value of x and ``float`` value of y are
    81 to the modifiers used in the print statement.
    84 printed corresponding to the modifiers used in the print statement.
    82 
    85 
    83 We can also see that print statement prints a new line character at the end of
    86 We can also see that ``print`` statement prints a new line character
    84 line, everytime it is called. This can be suppressed by using a "," at the end
    87 at the end of the line, everytime it is called. This can be suppressed
    85 print statement.
    88 by using a "," at the end ``print`` statement.
    86 
    89 
    87 Let us see this by typing out following code on an editor as print_example.py
    90 Let us see this by typing out following code on an editor as print_example.py
    88 
    91 
    89 {{{ open an editor }}}
    92 {{{ open an editor }}}
    90 type
    93 type