1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
.. Objectives
.. ----------
.. #. How to print some value
.. #. How to print using modifiers
.. #. How to take input from user
.. #. How to display a prompt to the user before taking the input
.. Prerequisites
.. -------------
.. none
.. Author : Nishanth Amuluru
Internal Reviewer : Puneeth
External Reviewer :
Language Reviewer : Bhanukiran
Checklist OK? : <put date stamp here, not OK> [2010-10-05]
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello friends and welcome to this tutorial on 'Input/Output'.
.. L2
{{{ Show the slide containing the objectives }}}
.. R2
At the end of this tutorial,you will be able to,
1. Print some value.
#. Print using modifiers.
#. Take input from user.
#. Display a prompt to the user before taking the input.
.. R3
Let us first start ipython on our teminal
.. L3
::
ipython
.. R4
Let us start this tutorial by typing a string
.. L4
::
a = "This is a string"
a
print a
.. R5
``print a``, obviously, prints the value of ``a``.
As you can see, even when you type just a, the value of a is shown.
But there is a difference.
Typing just ``a`` displays the content of ``a`` whereas the
statement ``print a`` prints the string itself.This difference becomes
more evident when we use strings with newlines in them.
.. L5
::
b = "A line \n New line"
b
print b
.. R6
As you can see, just typing ``b`` shows that b contains a newline
character but While typing ``print b``,it prints the string and hence
the newline.
Moreover when we type just ``a``, the value a is shown only in
interactive mode and does not have any effect on the program while
running it as a script.
We shall look at different ways of outputting the data.
print statement in python supports string formatting.
Various arguments can be passed to print using modifiers.
type
.. L6
::
x = 1.5
y = 2
z = "red"
print "x is %2.1f, y is %d, z is %s"%(x,y,z)
.. R7
As you can see, the values of x, y and z are substituted in place of
the modifiers ``%2.1f``, ``%d`` and ``%s`` respectively.
Pause the video here, try out the following exercise and resume the video.
.. L7
.. L8
{{{ Show slide with exercise 1 }}}
.. R8
What happens when you do ``print "x is %d, y is %f" %(x, y)``
.. R9
Switch to the terminal for solution.
.. L9
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
print "x is %d, y is %f" %(x, y)
.. R10
We see that the ``int`` value of x and ``float`` value of y are
printed corresponding to the modifiers used in the print statement.
We have seen that ``print`` statement prints a new line character
everytime it is called. This can be suppressed
by using a "," at the end of the ``print`` statement.
Let us see this by typing out following code on an editor as
print_example.py
.. L10
{{{ open an editor }}}
::
print "Hello"
print "World"
print "Hello",
print "World"
.. R11
Save the script as 'print_example.py' and run it using
%run /home/fossee/print_example.py
As we can see, the print statement when used with comma in the
end, prints a space instead of a new line.
Now we shall look at taking input from the user.
We will use the ~~raw_input~~ for this.
type
.. L11
::
ip = raw_input()
.. R12
The cursor is blinking indicating that it is waiting for input
type something and hit enter.
.. L12
::
an input
.. R13
Now let us see what is the value of ip by typing it.
.. L13
::
ip
.. R14
We can see that it contains the string "an input"
Pause the video here, try out the following exercise and resume the video.
.. L14
.. L15
{{{ Show slide with exercise 2 }}}
.. R15
Enter the number 5.6 as input and store it in a variable called c.
.. R16
Switch to the terminal for solution.
.. L16
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
.. R17
We have to use the raw_input command with variable c.
type
.. L17
::
c = raw_input()
5.6
c
.. R18
Now let us see the type of c.
.. L18
::
type(c)
.. R19
We see that c is a string. This implies that anything you enter as input,
it will be taken as a string no matter what you enter.
Pause the video here, try out the following exercise and resume the video.
.. L19
.. L20
{{{ Show slide with exercise 3 }}}
.. R20
What happens when you do not enter anything and hit enter.
.. R21
Switch to the terminal for solution.
.. L21
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
d = raw_input()
<RET>
d
.. R22
We see that when nothing is entered, an empty string is considered
as input.
raw_input also can display a prompt to assist the user.
.. L22
::
name = raw_input("Please enter your name: ")
.. R23
It prints the string given as argument and then waits for the user input.
Let us do one more exercise.
Pause the video here, try out the following exercise and resume the video.
.. L23
.. L24
{{{ Show slide with exercise 3 }}}
.. R24
How do you display a prompt and let the user enter input in next line.
.. R25
Switch to the terminal for solution.
The trick is to include a newline character at the end of the
prompt string.
.. L25
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
ip = raw_input("Please enter a number in the next line\n> ")
.. R26
It prints the newline character and hence the user enters input in the
next line
.. L26
.. L27
{{{ Show summary slide }}}
.. R27
This brings us to the end of the tutorial.
In this totorial, we have learnt to,
1. Use the print statement.
#. Use the modifiers %d, %f, %s in the print statement.
#. Take input from user by using ``raw_input()``.
#. Display a prompt to the user before taking the input by passing
a string as an argument to ``raw_input``.
.. L28
{{{Show self assessment questions slide}}}
.. R28
Here are some self assessment questions for you to solve
1. ``a = raw_input()`` and user enters ``2.5``. What is the type of a?
- str
- int
- float
- char
2. ``a = 2`` and ``b = 4.5``. What does ``print "a is %d and b is
%2.1f" %(b, a)`` print?
- a is 2 and b is 4.5
- a is 4 and b is 2
- a is 4 and b is 2.0
- a is 4.5 and b is 2
.. L29
{{{solution of self assessment questions on slide}}}
.. R29
And the answers,
1. No matter what you enter, it will be taken as a string.Hence 2.5 is
a string.
2. Since 'b' is called first, It will display integer value of 'a'
because the modifier used is %d. Similarly, 'b' will get the float
value of 'a' due to it's modifier %2.1f. Hence 'a' will be 4
and 'b' 2.0 .
.. L30
{{{ Show the Thankyou slide }}}
.. R30
Hope you have enjoyed this tutorial and found it useful.
Thank You!
|