141
|
1 |
Hello friends and welcome to the tutorial on Input/Output
|
|
2 |
|
|
3 |
{{{ Show the slide containing title }}}
|
|
4 |
|
|
5 |
{{{ Show the slide containing the outline slide }}}
|
|
6 |
|
|
7 |
Input and Output are used in almost every program we use.
|
|
8 |
In this tutorial, we shall learn
|
|
9 |
|
|
10 |
* Outputting data
|
|
11 |
* Taking input from the user
|
|
12 |
|
|
13 |
type
|
|
14 |
::
|
|
15 |
|
|
16 |
a = "This is a string"
|
|
17 |
a
|
|
18 |
print a
|
|
19 |
|
|
20 |
print a prints the value of a which is obvious.
|
|
21 |
As you can see, even when you type just a, the value of a is shown.
|
|
22 |
But there is a difference.
|
|
23 |
|
|
24 |
Typing a shows the value of a while print a prints the string. This difference
|
|
25 |
becomes more evident when we use strings with newlines in them.
|
|
26 |
type
|
|
27 |
::
|
|
28 |
|
|
29 |
b = "A line \n New line"
|
|
30 |
b
|
|
31 |
print b
|
|
32 |
|
|
33 |
As you can see, just typing b shows that b contains a newline character.
|
|
34 |
While typing print b prints the string and hence the newline.
|
|
35 |
|
|
36 |
Moreover when we type just a, the value a is shown only in interactive mode and
|
|
37 |
does not have any effect on the program while running it as a script.
|
|
38 |
|
|
39 |
We shall look at different ways of outputting the data.
|
|
40 |
|
|
41 |
print statement also accepts the syntax of C's printf statement.
|
|
42 |
Various arguments can be passed to print using modifiers.
|
|
43 |
type
|
|
44 |
::
|
|
45 |
|
|
46 |
x = 1.5
|
|
47 |
y = 2
|
|
48 |
z = "zed"
|
|
49 |
print "x is %2.1f y is %d z is %s"%(x,y)
|
|
50 |
|
|
51 |
As you can see, the values of x and y are substituted in place of %2.1f and %d
|
|
52 |
|
|
53 |
{{{ Pause here and try out the following exercises }}}
|
|
54 |
|
|
55 |
%% 1 %% What happens when you do print "x is %d y is %f"%(x)
|
|
56 |
|
|
57 |
{{{ continue from paused state }}}
|
|
58 |
|
|
59 |
We see that the int value of x and float value of y are printed corresponding
|
|
60 |
to the modifiers used in the print statement.
|
|
61 |
|
|
62 |
We can also see that print statement prints a new line character at the end of
|
|
63 |
line, everytime it is called. This can be suppressed by using a "," at the end
|
|
64 |
print statement.
|
|
65 |
|
|
66 |
Let us see this by typing out following code on an editor as print_example.py
|
|
67 |
|
|
68 |
{{{ open an editor }}}
|
|
69 |
type
|
|
70 |
::
|
|
71 |
|
|
72 |
print "Hello"
|
|
73 |
print "World"
|
|
74 |
|
|
75 |
print "Hello",
|
|
76 |
print "World"
|
|
77 |
|
|
78 |
Now we run the script using %run /home/fossee/print_example.py
|
|
79 |
|
|
80 |
As we can see, the print statement when used with comma in the end, prints a
|
|
81 |
space instead of a new line.
|
|
82 |
|
|
83 |
Now we shall look at taking input from the user.
|
|
84 |
We will use the ~~raw_input~~ for this.
|
|
85 |
type
|
|
86 |
::
|
|
87 |
|
|
88 |
ip = raw_input()
|
|
89 |
|
|
90 |
The cursor is blinking indicating that it is waiting for input
|
|
91 |
type
|
|
92 |
::
|
|
93 |
|
|
94 |
an input
|
|
95 |
|
|
96 |
and hit enter.
|
|
97 |
Now let us see what is the value of ip by typing.
|
|
98 |
::
|
|
99 |
|
|
100 |
ip
|
|
101 |
|
|
102 |
We can see that it contains the string "an input"
|
|
103 |
|
|
104 |
{{{ Pause here and try out the following exercises }}}
|
|
105 |
|
|
106 |
%% 2 %% enter the number 5.6 as input and store it in a variable called c.
|
|
107 |
|
|
108 |
{{{ continue from paused state }}}
|
|
109 |
|
|
110 |
We have to use the raw_input command with variable c.
|
|
111 |
type
|
|
112 |
::
|
|
113 |
|
|
114 |
c = raw_input()
|
|
115 |
5.6
|
|
116 |
c
|
|
117 |
|
|
118 |
Now let us see the type of c.
|
|
119 |
|
|
120 |
::
|
|
121 |
|
|
122 |
type(c)
|
|
123 |
|
|
124 |
We see that c is a string. This implies that anything you enter as input, will
|
|
125 |
be taken as a string no matter what you enter.
|
|
126 |
|
|
127 |
{{{ Pause here and try out the following exercises }}}
|
|
128 |
|
|
129 |
%% 3 %% What happens when you do not enter anything and hit enter
|
|
130 |
|
|
131 |
{{{ continue from paused state }}}
|
|
132 |
|
|
133 |
::
|
|
134 |
|
|
135 |
d = raw_input()
|
|
136 |
<RET>
|
|
137 |
d
|
|
138 |
|
|
139 |
We see that when nothing is entered, an empty string is considered as input.
|
|
140 |
|
|
141 |
raw_input also can display a prompt to assist the user.
|
|
142 |
::
|
|
143 |
|
|
144 |
name = raw_input("Please enter your name: ")
|
|
145 |
|
|
146 |
prints the string given as argument and then waits for the user input.
|
|
147 |
|
|
148 |
{{{ Pause here and try out the following exercises }}}
|
|
149 |
|
|
150 |
%% 4 %% How do you display a prompt and let the user enter input in a new line
|
|
151 |
|
|
152 |
{{{ continue from paused state }}}
|
|
153 |
|
|
154 |
The trick is to include a newline character at the end of the prompt string.
|
|
155 |
::
|
|
156 |
|
|
157 |
ip = raw_input("Please enter a number in the next line\n> ")
|
|
158 |
|
|
159 |
prints the newline character and hence the user enters input in the new line
|
|
160 |
|
|
161 |
{{{ Show summary slide }}}
|
|
162 |
|
|
163 |
This brings us to the end of the tutorial.
|
|
164 |
we have learnt
|
|
165 |
|
|
166 |
* How to print some value
|
|
167 |
* How to print using modifiers
|
|
168 |
* How to take input from user
|
|
169 |
* How to display a prompt to the user before taking the input
|
|
170 |
|
|
171 |
{{{ Show the "sponsored by FOSSEE" slide }}}
|
|
172 |
|
|
173 |
#[Nishanth]: Will add this line after all of us fix on one.
|
|
174 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
|
|
175 |
|
|
176 |
Hope you have enjoyed and found it useful.
|
|
177 |
Thankyou
|
|
178 |
|
|
179 |
.. Author : Nishanth
|
|
180 |
Internal Reviewer 1 :
|
|
181 |
Internal Reviewer 2 :
|
|
182 |
External Reviewer :
|
231
|
183 |
|
|
184 |
Questions
|
|
185 |
=========
|
|
186 |
|
|
187 |
1. ``a = 2.5``. What is the output of ``print "a is %d"%(a)``
|
|
188 |
|
|
189 |
a. a is 2.5
|
|
190 |
#. a is 2.0
|
|
191 |
#. 2.0
|
|
192 |
#. a is 2
|
|
193 |
|
|
194 |
Answer: a is 2
|
|
195 |
|
|
196 |
2. What does ``print "This is", "a line ", "with spaces"`` print?
|
|
197 |
|
|
198 |
a. This is a line with spaces
|
|
199 |
#. This is a line with spaces
|
|
200 |
#. This is a line with spaces
|
|
201 |
#. This is a line with spaces
|
|
202 |
|
|
203 |
Answer: This is a line with spaces
|
|
204 |
|
|
205 |
3. What does ``print "%2.5f"%(1.2)`` print?
|
|
206 |
|
|
207 |
a. 1.2
|
|
208 |
#. 1.20
|
|
209 |
#. 1.20000
|
|
210 |
#. 00001.2
|
|
211 |
|
|
212 |
Answer: 1.20000
|
|
213 |
|
|
214 |
4. What is the output of the following code::
|
|
215 |
|
|
216 |
for i in range(1,10,2):
|
|
217 |
print i,
|
|
218 |
|
|
219 |
Answer::
|
|
220 |
|
|
221 |
1 3 5 7 9
|
|
222 |
|
|
223 |
5. ``a = 2`` and ``b = 4.5``. What does ``print "a is %d and b is %2.1f"%(b, a)``
|
|
224 |
print?
|
|
225 |
|
|
226 |
a. a is 2 and b is 4.5
|
|
227 |
#. a is 4 and b is 2
|
|
228 |
#. a is 4 and b is 2.0
|
|
229 |
#. a is 4.5 and b is 2
|
|
230 |
|
|
231 |
Answer: a is 4 and b is 2.0
|
|
232 |
|
|
233 |
6. What is the prompt displayed by ``raw_input("Say something\nType here:")``
|
|
234 |
|
|
235 |
Answer::
|
|
236 |
|
|
237 |
Say something
|
|
238 |
Type here:
|
|
239 |
|
|
240 |
6. What is the prompt displayed by ``raw_input("value of a is %d\nInput b
|
|
241 |
value:"a)`` and ``a = 2.5``
|
|
242 |
|
|
243 |
Answer::
|
|
244 |
|
|
245 |
value of a is 2
|
|
246 |
Input ba value:
|
|
247 |
|
|
248 |
7. ``a = raw_input()`` and user enters ``2.5``. What is the type of a?
|
|
249 |
|
|
250 |
a. str
|
|
251 |
#. int
|
|
252 |
#. float
|
|
253 |
#. char
|
|
254 |
|
|
255 |
Answer: str
|
|
256 |
|
|
257 |
8. ``a = int(raw_input())`` and user enters ``4.5``. What happens?
|
|
258 |
|
|
259 |
a. a = 4.5
|
|
260 |
#. a = 4
|
|
261 |
#. a = 4.0
|
|
262 |
#. Error
|
|
263 |
|
|
264 |
Answer: Error
|
|
265 |
|
|
266 |
9. ``a = raw_input()`` and user enters ``"this is a string"``. What does
|
|
267 |
``print a`` produce?
|
|
268 |
|
|
269 |
a. 'this is a string'
|
|
270 |
b. 'this is a string"
|
|
271 |
c. "this is a string"
|
|
272 |
#. this is a string
|
|
273 |
|
|
274 |
Answer: "this is a string"
|
|
275 |
|
|
276 |
Problems
|
|
277 |
========
|
|
278 |
|
|
279 |
1. Answer to universe and everything. Keep taking input from user and print it
|
|
280 |
back until the input is 42.
|
|
281 |
|
|
282 |
Answer::
|
|
283 |
|
|
284 |
ip = raw_input()
|
|
285 |
while ip != "42":
|
|
286 |
print ip
|
|
287 |
|
|
288 |
2.
|