input_output/script.rst
changeset 457 68813d8d80fb
parent 420 27d2561e617a
child 503 a858141ad884
equal deleted inserted replaced
456:be96dc6c9743 457:68813d8d80fb
    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 ------
    21 
    21 
    22 Hello friends and welcome to the tutorial on Input/Output
    22 Hello friends and welcome to this tutorial on Input/Output
    23 
    23 
    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 
       
    42 ``print a``, obviously, is printing the value of ``a``.
       
    43 
    42 As you can see, even when you type just a, the value of a is shown.
    44 As you can see, even when you type just a, the value of a is shown.
    43 But there is a difference.
    45 But there is a difference.
       
    46 
       
    47 .. #[Amit: The next sentence does seem to be clear enough]
    44 
    48 
    45 Typing a shows the value of a while print a prints the string. This difference
    49 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.
    50 becomes more evident when we use strings with newlines in them.
    47 type
    51 type
    48 ::
    52 ::
    55 While typing print b prints the string and hence the newline.
    59 While typing print b prints the string and hence the newline.
    56 
    60 
    57 Moreover when we type just a, the value a is shown only in interactive mode and
    61 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.
    62 does not have any effect on the program while running it as a script.
    59 
    63 
       
    64 .. #[punch: I think we could show that?]
       
    65 
    60 We shall look at different ways of outputting the data.
    66 We shall look at different ways of outputting the data.
    61 
    67 
    62 print statement also accepts the syntax of C's printf statement.
    68 
       
    69 .. #[Amit: C's printf syntax ?? i think its better to elaborate the
       
    70    idea]
       
    71 
       
    72 print statement  in python supports string formatting.
    63 Various arguments can be passed to print using modifiers.
    73 Various arguments can be passed to print using modifiers.
       
    74 
    64 type
    75 type
    65 ::
    76 ::
    66 
    77 
    67     x = 1.5
    78     x = 1.5
    68     y = 2
    79     y = 2
    69     z = "zed"
    80     z = "zed"
    70     print "x is %2.1f y is %d z is %s"%(x,y)
    81     print "x is %2.1f y is %d z is %s"%(x,y)
    71 
    82 
    72 As you can see, the values of x and y are substituted in place of %2.1f and %d
    83 As you can see, the values of x and y are substituted in place of
       
    84 ``%2.1f`` and ``%d`` 
    73 
    85 
    74 {{{ Pause here and try out the following exercises }}}
    86 {{{ Pause here and try out the following exercises }}}
    75 
    87 
    76 %% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)``
    88 %% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)``
    77 
    89 
    78 {{{ continue from paused state }}}
    90 {{{ continue from paused state }}}
    79 
    91 
    80 We see that the int value of x and float value of y are printed corresponding
    92 We see that the ``int`` value of x and ``float`` value of y are
    81 to the modifiers used in the print statement.
    93 printed corresponding to the modifiers used in the print statement.
    82 
    94 
    83 We can also see that print statement prints a new line character at the end of
    95 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
    96 at the end of the line, everytime it is called. This can be suppressed
    85 print statement.
    97 by using a "," at the end ``print`` statement.
    86 
    98 
    87 Let us see this by typing out following code on an editor as print_example.py
    99 Let us see this by typing out following code on an editor as print_example.py
    88 
   100 
    89 {{{ open an editor }}}
   101 {{{ open an editor }}}
    90 type
   102 type
   166 
   178 
   167 prints the string given as argument and then waits for the user input.
   179 prints the string given as argument and then waits for the user input.
   168 
   180 
   169 {{{ Pause here and try out the following exercises }}}
   181 {{{ Pause here and try out the following exercises }}}
   170 
   182 
   171 %% 4 %% How do you display a prompt and let the user enter input in a new line
   183 %% 4 %% How do you display a prompt and let the user enter input in next line
   172 
   184 
   173 {{{ continue from paused state }}}
   185 {{{ continue from paused state }}}
   174 
   186 
   175 .. #[Puneeth: We didn't talk of new-line character till now, did we?]
   187 .. #[Puneeth: We didn't talk of new-line character till now, did we?]
   176 .. #[Puneeth: non-programmers might not know?]
   188 .. #[Puneeth: non-programmers might not know?]
   177 
   189 
       
   190 .. #[Amit: Well there is a discussion earlier about new lines, I think its good
       
   191 .. as a slight trick question. But may be next line is a more easier lexicon]
       
   192 
   178 The trick is to include a newline character at the end of the prompt string.
   193 The trick is to include a newline character at the end of the prompt string.
   179 ::
   194 ::
   180 
   195 
   181     ip = raw_input("Please enter a number in the next line\n> ")
   196     ip = raw_input("Please enter a number in the next line\n> ")
   182 
   197 
   183 prints the newline character and hence the user enters input in the new line
   198 prints the newline character and hence the user enters input in the next line
   184 
   199 
   185 {{{ Show summary slide }}}
   200 {{{ Show summary slide }}}
   186 
   201 
   187 This brings us to the end of the tutorial.
   202 This brings us to the end of the tutorial.
   188 we have learnt
   203 In this totorial we have learnt
   189 
   204 
   190  * How to print some value
   205  * How to print some value
   191  * How to print using modifiers
   206  * How to print using modifiers
   192  * How to take input from user
   207  * How to take input from user
   193  * How to display a prompt to the user before taking the input
   208  * How to display a prompt to the user before taking the input
   194 
   209 
   195 {{{ Show the "sponsored by FOSSEE" slide }}}
   210 {{{ Show the "sponsored by FOSSEE" slide }}}
   196 
   211 
   197 #[Nishanth]: Will add this line after all of us fix on one.
   212 
   198 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   213 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   199 
   214 
   200 Hope you have enjoyed and found it useful.
   215 Hope you have enjoyed and found it useful.
   201 Thankyou
   216 Thank You.
   202  
   217