input_output.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 06 Oct 2010 19:32:18 +0530
changeset 240 f4c36aa38a84
parent 141 05a7b0506134
child 231 e78c284d644b
permissions -rw-r--r--
Cleaned up the script of advanced features of functions LO.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
141
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Input/Output
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     2
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     4
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     6
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     7
Input and Output are used in almost every program we use.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     8
In this tutorial, we shall learn
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
     9
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    10
 * Outputting data
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    11
 * Taking input from the user
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    12
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    13
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    14
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    15
 
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    16
    a = "This is a string"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    17
    a
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    18
    print a
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    19
     
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    20
print a prints the value of a which is obvious.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    21
As you can see, even when you type just a, the value of a is shown.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    22
But there is a difference.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    23
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    24
Typing a shows the value of a while print a prints the string. This difference
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    25
becomes more evident when we use strings with newlines in them.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    26
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    27
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    28
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    29
    b = "A line \n New line"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    30
    b
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    31
    print b
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    32
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    33
As you can see, just typing b shows that b contains a newline character.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    34
While typing print b prints the string and hence the newline.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    35
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    36
Moreover when we type just a, the value a is shown only in interactive mode and
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    37
does not have any effect on the program while running it as a script.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    38
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    39
We shall look at different ways of outputting the data.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    40
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    41
print statement also accepts the syntax of C's printf statement.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    42
Various arguments can be passed to print using modifiers.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    43
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    44
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    45
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    46
    x = 1.5
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    47
    y = 2
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    48
    z = "zed"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    49
    print "x is %2.1f y is %d z is %s"%(x,y)
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    50
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    51
As you can see, the values of x and y are substituted in place of %2.1f and %d
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    52
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    53
{{{ Pause here and try out the following exercises }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    54
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    55
%% 1 %% What happens when you do print "x is %d y is %f"%(x)
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    56
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    57
{{{ continue from paused state }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    58
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    59
We see that the int value of x and float value of y are printed corresponding
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    60
to the modifiers used in the print statement.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    61
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    62
We can also see that print statement prints a new line character at the end of
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    63
line, everytime it is called. This can be suppressed by using a "," at the end
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    64
print statement.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    65
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    66
Let us see this by typing out following code on an editor as print_example.py
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    67
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    68
{{{ open an editor }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    69
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    70
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    71
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    72
    print "Hello"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    73
    print "World"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    74
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    75
    print "Hello",
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    76
    print "World"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    77
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    78
Now we run the script using %run /home/fossee/print_example.py
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    79
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    80
As we can see, the print statement when used with comma in the end, prints a
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    81
space instead of a new line.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    82
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    83
Now we shall look at taking input from the user.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    84
We will use the ~~raw_input~~ for this.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    85
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    86
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    87
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    88
    ip = raw_input()
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    89
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    90
The cursor is blinking indicating that it is waiting for input    
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    91
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    92
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    93
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    94
    an input
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    95
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    96
and hit enter.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    97
Now let us see what is the value of ip by typing.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    98
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
    99
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   100
    ip
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   101
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   102
We can see that it contains the string "an input"
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   103
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   104
{{{ Pause here and try out the following exercises }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   105
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   106
%% 2 %% enter the number 5.6 as input and store it in a variable called c.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   107
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   108
{{{ continue from paused state }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   109
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   110
We have to use the raw_input command with variable c.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   111
type
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   112
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   113
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   114
    c = raw_input()
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   115
    5.6
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   116
    c
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   117
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   118
Now let us see the type of c.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   119
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   120
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   121
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   122
    type(c)
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   123
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   124
We see that c is a string. This implies that anything you enter as input, will
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   125
be taken as a string no matter what you enter.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   126
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   127
{{{ Pause here and try out the following exercises }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   128
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   129
%% 3 %% What happens when you do not enter anything and hit enter
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   130
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   131
{{{ continue from paused state }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   132
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   133
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   134
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   135
    d = raw_input()
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   136
    <RET>
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   137
    d
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   138
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   139
We see that when nothing is entered, an empty string is considered as input.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   140
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   141
raw_input also can display a prompt to assist the user.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   142
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   143
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   144
    name = raw_input("Please enter your name: ")
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   145
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   146
prints the string given as argument and then waits for the user input.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   147
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   148
{{{ Pause here and try out the following exercises }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   149
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   150
%% 4 %% How do you display a prompt and let the user enter input in a new line
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   151
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   152
{{{ continue from paused state }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   153
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   154
The trick is to include a newline character at the end of the prompt string.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   155
::
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   156
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   157
    ip = raw_input("Please enter a number in the next line\n> ")
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   158
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   159
prints the newline character and hence the user enters input in the new line
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   160
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   161
{{{ Show summary slide }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   162
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   163
This brings us to the end of the tutorial.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   164
we have learnt
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   165
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   166
 * How to print some value
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   167
 * How to print using modifiers
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   168
 * How to take input from user
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   169
 * How to display a prompt to the user before taking the input
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   170
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   171
{{{ Show the "sponsored by FOSSEE" slide }}}
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   172
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   173
#[Nishanth]: Will add this line after all of us fix on one.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   174
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   175
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   176
Hope you have enjoyed and found it useful.
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   177
Thankyou
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   178
 
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   179
.. Author              : Nishanth
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   180
   Internal Reviewer 1 : 
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   181
   Internal Reviewer 2 : 
05a7b0506134 initial commit for input_output
nishanth
parents:
diff changeset
   182
   External Reviewer   :