input_output.rst
author Nishanth <nishanth@fossee.in>
Thu, 07 Oct 2010 14:25:44 +0530
changeset 235 80e4016d747a
parent 231 e78c284d644b
permissions -rw-r--r--
Converted the embellishing_a_plot to new template form
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   :
231
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   183
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   184
Questions
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   185
=========
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   186
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   187
 1. ``a = 2.5``. What is the output of ``print "a is %d"%(a)``
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   188
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   189
   a. a is 2.5
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   190
   #. a is 2.0
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   191
   #. 2.0
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   192
   #. a is 2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   193
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   194
   Answer: a is 2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   195
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   196
 2. What does ``print "This is",     "a line ", "with  spaces"`` print?
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   197
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   198
   a. This is a line with spaces
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   199
   #. This is a line with  spaces
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   200
   #. This is     a line   with   spaces
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   201
   #. This is a line  with  spaces
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   202
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   203
   Answer: This is a line  with  spaces
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   204
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   205
 3. What does ``print "%2.5f"%(1.2)`` print?
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   206
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   207
   a. 1.2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   208
   #. 1.20
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   209
   #. 1.20000
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   210
   #. 00001.2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   211
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   212
   Answer: 1.20000
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   213
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   214
 4. What is the output of the following code::
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   215
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   216
     for i in range(1,10,2):
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   217
         print i,
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   218
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   219
    Answer::
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   220
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   221
      1 3 5 7 9
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   222
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   223
 5. ``a = 2`` and ``b = 4.5``. What does ``print "a is %d and b is %2.1f"%(b, a)``
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   224
    print?
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   225
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   226
   a. a is 2 and b is 4.5
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   227
   #. a is 4 and b is 2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   228
   #. a is 4 and b is 2.0
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   229
   #. a is 4.5 and b is 2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   230
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   231
   Answer: a is 4 and b is 2.0
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   232
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   233
 6. What is the prompt displayed by ``raw_input("Say something\nType here:")``
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   234
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   235
   Answer::
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   236
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   237
     Say something 
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   238
     Type here:
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   239
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   240
 6. What is the prompt displayed by ``raw_input("value of a is %d\nInput b
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   241
    value:"a)`` and ``a = 2.5``
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   242
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   243
   Answer::
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   244
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   245
     value of a is 2
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   246
     Input ba value:
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   247
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   248
 7. ``a = raw_input()`` and user enters ``2.5``. What is the type of a?
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   249
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   250
   a. str
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   251
   #. int
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   252
   #. float
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   253
   #. char
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   254
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   255
   Answer: str
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   256
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   257
 8. ``a = int(raw_input())`` and user enters ``4.5``. What happens?
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   258
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   259
   a. a = 4.5
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   260
   #. a = 4
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   261
   #. a = 4.0
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   262
   #. Error
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   263
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   264
   Answer: Error
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   265
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   266
 9. ``a = raw_input()`` and user enters ``"this is a string"``. What does 
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   267
    ``print a`` produce?
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   268
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   269
   a. 'this is a string'
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   270
   b. 'this is a string"
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   271
   c. "this is a string"
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   272
   #. this is a string
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   273
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   274
   Answer: "this is a string"
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   275
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   276
Problems
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   277
========
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   278
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   279
 1. Answer to universe and everything. Keep taking input from user and print it
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   280
    back until the input is 42.
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   281
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   282
  Answer::
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   283
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   284
    ip = raw_input()
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   285
    while ip != "42":
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   286
        print ip
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   287
e78c284d644b Added questions
Nishanth <nishanth@fossee.in>
parents: 141
diff changeset
   288
 2.