|
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 : |