input_output/script.rst
author Amit Sethi
Tue, 09 Nov 2010 15:40:53 +0530
changeset 418 8a42b4203f6d
parent 416 06ac45f4de88
parent 410 226b6e789da5
child 420 27d2561e617a
permissions -rw-r--r--
"Merging heads"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
276
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
     4
.. #. How to print some value
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
     5
.. #. How to print using modifiers
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
     6
.. #. How to take input from user
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
     7
.. #. How to display a prompt to the user before taking the input
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
     8
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     9
.. Prerequisites
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    10
.. -------------
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    12
..   1. Loops
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
     
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
.. Author              : Nishanth Amuluru
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    15
   Internal Reviewer   : Puneeth 
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   External Reviewer   :
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
Script
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
------
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    22
Hello friends and welcome to this tutorial on Input/Output
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
{{{ Show the slide containing title }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
{{{ Show the slide containing the outline slide }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
Input and Output are used in almost every program we use.
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    29
In this tutorial, we shall learn how to 
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    31
 * Output data
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    32
 * Take input from the user
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    34
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
 
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
    a = "This is a string"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
    a
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
    print a
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
     
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents: 416 410
diff changeset
    41
<<<<<<< local
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    42
print a, prints the value of a.
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents: 416 410
diff changeset
    43
=======
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    44
``print a``, obviously, is printing the value of ``a``.
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents: 416 410
diff changeset
    45
>>>>>>> other
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
As you can see, even when you type just a, the value of a is shown.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
But there is a difference.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    49
.. #[Amit: The next sentence does seem to be clear enough]
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    50
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
Typing a shows the value of a while print a prints the string. This difference
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
becomes more evident when we use strings with newlines in them.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
    b = "A line \n New line"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
    b
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
    print b
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
As you can see, just typing b shows that b contains a newline character.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
While typing print b prints the string and hence the newline.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
Moreover when we type just a, the value a is shown only in interactive mode and
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
does not have any effect on the program while running it as a script.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    66
.. #[punch: I think we could show that?]
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    67
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
We shall look at different ways of outputting the data.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents: 416 410
diff changeset
    70
<<<<<<< local
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    71
.. #[Amit: C's printf syntax ?? i think its better to elaborate the
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    72
   idea]
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    73
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
    74
print statement  in python supports string formatting.
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
Various arguments can be passed to print using modifiers.
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents: 416 410
diff changeset
    76
=======
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    77
``print`` statement also accepts the syntax of C's ``printf`` statement.
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    78
Various arguments can be passed to ``print`` using modifiers.
418
8a42b4203f6d "Merging heads"
Amit Sethi
parents: 416 410
diff changeset
    79
>>>>>>> other
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
    x = 1.5
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
    y = 2
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
    z = "zed"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
    print "x is %2.1f y is %d z is %s"%(x,y)
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    88
As you can see, the values of x and y are substituted in place of
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    89
``%2.1f`` and ``%d`` 
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    90
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
{{{ Pause here and try out the following exercises }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
276
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
    93
%% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)``
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
{{{ continue from paused state }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    97
We see that the ``int`` value of x and ``float`` value of y are
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
    98
printed corresponding to the modifiers used in the print statement.
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
   100
We can also see that ``print`` statement prints a new line character
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
   101
at the end of the line, everytime it is called. This can be suppressed
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 329
diff changeset
   102
by using a "," at the end ``print`` statement.
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
Let us see this by typing out following code on an editor as print_example.py
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
{{{ open an editor }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
    print "Hello"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
    print "World"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
    print "Hello",
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
    print "World"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
Now we run the script using %run /home/fossee/print_example.py
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
As we can see, the print statement when used with comma in the end, prints a
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
space instead of a new line.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
Now we shall look at taking input from the user.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
We will use the ~~raw_input~~ for this.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
    ip = raw_input()
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
The cursor is blinking indicating that it is waiting for input    
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
    an input
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
and hit enter.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
Now let us see what is the value of ip by typing.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
    ip
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
We can see that it contains the string "an input"
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
{{{ Pause here and try out the following exercises }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
%% 2 %% enter the number 5.6 as input and store it in a variable called c.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
{{{ continue from paused state }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   147
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   148
We have to use the raw_input command with variable c.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
type
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   152
    c = raw_input()
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   153
    5.6
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   154
    c
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   156
Now let us see the type of c.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   157
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   158
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   159
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   160
    type(c)
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   161
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   162
We see that c is a string. This implies that anything you enter as input, will
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   163
be taken as a string no matter what you enter.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   164
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   165
{{{ Pause here and try out the following exercises }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   166
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   167
%% 3 %% What happens when you do not enter anything and hit enter
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   168
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   169
{{{ continue from paused state }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   170
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   171
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   172
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   173
    d = raw_input()
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   174
    <RET>
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   175
    d
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   176
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   177
We see that when nothing is entered, an empty string is considered as input.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   178
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   179
raw_input also can display a prompt to assist the user.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   180
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   181
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   182
    name = raw_input("Please enter your name: ")
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   183
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   184
prints the string given as argument and then waits for the user input.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   185
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   186
{{{ Pause here and try out the following exercises }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   187
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   188
%% 4 %% How do you display a prompt and let the user enter input in next line
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   189
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   190
{{{ continue from paused state }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   191
276
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
   192
.. #[Puneeth: We didn't talk of new-line character till now, did we?]
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
   193
.. #[Puneeth: non-programmers might not know?]
89437325b326 Slides for I/O LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 236
diff changeset
   194
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   195
.. #[Amit: Well there is a discussion earlier about new lines, I think its good
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   196
.. as a slight trick question. But may be next line is a more easier lexicon]
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   197
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   198
The trick is to include a newline character at the end of the prompt string.
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   199
::
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   200
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   201
    ip = raw_input("Please enter a number in the next line\n> ")
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   202
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   203
prints the newline character and hence the user enters input in the next line
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   204
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   205
{{{ Show summary slide }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   206
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   207
This brings us to the end of the tutorial.
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   208
In this totorial we have learnt
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   209
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   210
 * How to print some value
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   211
 * How to print using modifiers
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   212
 * How to take input from user
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   213
 * How to display a prompt to the user before taking the input
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   214
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   215
{{{ Show the "sponsored by FOSSEE" slide }}}
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   216
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   217
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   218
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   219
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   220
Hope you have enjoyed and found it useful.
416
06ac45f4de88 Changed Plotting Data according to review
Amit Sethi
parents: 329
diff changeset
   221
Thank You.
236
33828497b5da Converted input_output to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   222