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