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