|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you will be able to |
|
5 |
|
6 .. 1. Write blocks of code in python. |
|
7 .. #. Use for loop. |
|
8 .. #. Use ``range()`` function. |
|
9 .. #. Write blocks in python interpreter |
|
10 .. #. Write blocks in ipython interpreter. |
|
11 |
|
12 |
|
13 .. Prerequisites |
|
14 .. ------------- |
|
15 |
|
16 .. 1. should have ``ipython`` and ``pylab`` installed. |
|
17 .. #. getting started with ``ipython``. |
|
18 .. #. getting started with lists. |
|
19 |
|
20 |
|
21 .. Author : Anoop Jacob Thomas <anoop@fossee.in> |
|
22 Internal Reviewer : Nishanth |
|
23 Internal Reviewer(2): Amit |
|
24 External Reviewer : |
|
25 Language Reviewer : Bhanukiran |
|
26 Checklist OK? : <10-11-2010, Anand, OK> [2010-10-05] |
|
27 |
|
28 |
|
29 ============================= |
|
30 Getting started with for loop |
|
31 ============================= |
|
32 |
|
33 {{{ show welcome slide }}} |
|
34 |
|
35 Hello and welcome to the tutorial `Getting started with ``for`` loop`. |
|
36 |
|
37 {{{ switch to next slide, outline slide }}} |
|
38 |
|
39 In this tutorial we will learn about ``for`` loops in python, and also |
|
40 learn how to write blocks of code in Python. |
|
41 |
|
42 .. #[Nishanth]: Instead of saying basics of indenting code, |
|
43 say How to define code blocks in Python |
|
44 |
|
45 {{{ switch to next slide, about whitespaces }}} |
|
46 |
|
47 In Python whitespace is significant, and the blocks are visually |
|
48 separated. |
|
49 |
|
50 .. #[nishanth]: Simply tell how blocks are defined in python. |
|
51 The details like braces are not used and its |
|
52 advantages like neat code can be told after completely |
|
53 explaining the indentation |
|
54 |
|
55 .. #[Amit]: Do you want to do that here. May be its better to talk about |
|
56 this after some initiation into the idea of blocks. |
|
57 |
|
58 The best practice is to indent the code using four spaces. |
|
59 |
|
60 .. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces |
|
61 for indentation. Do that while typing so that they can |
|
62 actually see what is being typed. |
|
63 |
|
64 As you can see in the slide, ``Block B`` is an inner block and it is |
|
65 indented using 4 spaces, and after ``Block B`` the next statement in |
|
66 ``Block A`` starts from the same indentation level of other ``Block |
|
67 A`` statements. |
|
68 |
|
69 Now let us move straight into ``for`` loop. |
|
70 |
|
71 {{{ switch to next slide, problem statement of exercise 1 }}} |
|
72 |
|
73 |
|
74 Write a for loop which iterates through a list of numbers and find the |
|
75 square root of each number. |
|
76 :: |
|
77 |
|
78 numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916 |
|
79 |
|
80 .. #[nishanth]: making new list with square roots induces extra complication |
|
81 like appending which has no use case here |
|
82 |
|
83 .. #[Nishanth]: The problem focuses more on square root and creation |
|
84 of list. The problem must be simple and focusing on |
|
85 nothing more but the indentation and for loop. |
|
86 May be change the problem to print squares than to |
|
87 print square roots. |
|
88 |
|
89 For the problem, first we need to create a ``list`` of numbers and |
|
90 then iterate over the list and find the square root of each element in |
|
91 it. And let us create a script, rather than typing it out in the |
|
92 interpreter itself. Create a script called list_roots.py and type the |
|
93 following. |
|
94 |
|
95 {{{ open the text editor and paste the following code there }}} |
|
96 :: |
|
97 |
|
98 numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] |
|
99 for each in numbers: |
|
100 print "Square root of", each, "is", sqrt(each) |
|
101 print "This is not in for loop!" |
|
102 |
|
103 .. numbers = [1, 12, 3, 4, 21, 17] |
|
104 for each in numbers: |
|
105 print each, each * each |
|
106 |
|
107 .. #[nishanth]: I don't see a use case to append the sq_root to |
|
108 square_roots. It is only complicating stuff. |
|
109 Simply iterate and print. |
|
110 |
|
111 {{{ switch to next slide, save and run script }}} |
|
112 |
|
113 {{{ save the script }}} |
|
114 |
|
115 Now save the script, and run it from your IPython interpreter. I |
|
116 assume that you have started your IPython interpreter using ``-pylab`` |
|
117 option. |
|
118 |
|
119 Run the script as, |
|
120 :: |
|
121 |
|
122 %run -i list_roots.py |
|
123 |
|
124 .. #[Nishanth]: you don't have to use the -i option here |
|
125 |
|
126 {{{ run the script }}} |
|
127 |
|
128 So that was easy! All what we did was iterate over the list element by |
|
129 element and then use the element for calculation. Note that here we |
|
130 used two variables. One the variable ``numbers``, which is a list, |
|
131 another one ``each``, which is the element of list under consideration |
|
132 in each cycle of the ``for`` loop. The variable names can be chosen by |
|
133 you. |
|
134 |
|
135 .. #[Nishanth]: The details like we didn't have to find the length |
|
136 are relevant for people who have programmed in C or |
|
137 other languages earlier. But for a newbie it is more |
|
138 of confusing extra info. That part may be skipped. |
|
139 Simply go ahead and focus on the syntax of for loop. |
|
140 And how the variable name is used inside the for loop. |
|
141 If you modify the question to only print, the extra |
|
142 variable sq_root can also be avoided. let it be more |
|
143 about "each", "numbers" and "for". no other new names. |
|
144 |
|
145 {{{ show the script which was created }}} |
|
146 |
|
147 Note that the lines after ``for`` statement, is indented using four |
|
148 spaces. |
|
149 |
|
150 {{{ highlight the line after for statement }}} |
|
151 |
|
152 It means that line is part of the for loop. And it is a block of code, |
|
153 although it is only a single statement in the block. And the fourth |
|
154 line or the immediate line after the ``for`` block is not indented, |
|
155 |
|
156 {{{ highlight the fourth line - the line just after for loop }}} |
|
157 |
|
158 it means that it is not part of the ``for`` loop and the lines after |
|
159 that doesn't fall in the scope of the ``for`` loop. Thus each block is |
|
160 separated by the indentation level and that marks the importance of |
|
161 white-spaces in Python. |
|
162 |
|
163 {{{ switch to the slide which shows the problem statement of the first |
|
164 problem to be tried out }}} |
|
165 |
|
166 Now a question for you to try, from the given numbers make a list of |
|
167 perfect squares and a list of those which are not. The numbers are, |
|
168 :: |
|
169 |
|
170 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916 |
|
171 |
|
172 Pause here and try to solve the problem before proceeding further. |
|
173 |
|
174 {{{ switch to next slide, problem statement of second problem in |
|
175 solved exercise}}} |
|
176 |
|
177 Now let us try a simple one, to print the square root of numbers in |
|
178 the list. And this time let us do it right in the IPython |
|
179 interpreter. |
|
180 |
|
181 {{{ switch to next slide, Indentation in ``ipython`` }}} |
|
182 |
|
183 {{{ switch focus to the IPython interpreter }}} |
|
184 |
|
185 So let us start with making a list. Type the following |
|
186 :: |
|
187 |
|
188 numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] |
|
189 for each in numbers: |
|
190 |
|
191 and now you will notice that, as soon as you press the return key |
|
192 after for statement, the prompt changes to four dots and the cursor is |
|
193 not right after the four dots but there are four spaces from the |
|
194 dots. Please note that IPython automatically indents the block. The |
|
195 four dots tell you that you are inside a block. Now type the rest of |
|
196 the ``for`` loop, |
|
197 |
|
198 {{{ switch to next slide, Indentation in ``ipython`` (cont'd) }}} |
|
199 |
|
200 .. #[Nishanth]: Tell that IPython does auto indentation. |
|
201 |
|
202 :: |
|
203 |
|
204 print "Square root of", each, |
|
205 print "is", sqrt(each) |
|
206 |
|
207 Now we have finished the statements in the block, and still the |
|
208 interpreter is showing four dots, this means that you are still inside the |
|
209 block. To exit from the block press the return key or the enter key twice |
|
210 without entering anything else. It printed the square root of each |
|
211 number in the list, and that is executed in a ``for`` loop. |
|
212 |
|
213 {{{ switch to next slide, Indentation in ``python`` interpreter }}} |
|
214 |
|
215 Now, let us find the cube of all the numbers from one to ten. But this |
|
216 time let us try it in the vanilla version of Python interpreter. |
|
217 |
|
218 Start the vanilla version of Python interpreter by issuing the command |
|
219 ``python`` in your terminal. |
|
220 |
|
221 {{{ open the python interpreter in the terminal using the command |
|
222 python to start the vanilla Python interpreter }}} |
|
223 |
|
224 {{{ switch to next slide, Indentation in ``python`` interpreter |
|
225 (cont'd) }}} |
|
226 |
|
227 Start with, |
|
228 :: |
|
229 |
|
230 for i in range(1,11): |
|
231 |
|
232 and press enter once, and we will see that this time it shows four |
|
233 dots, but the cursor is close to the dots, so we have to indent the |
|
234 block. The vanilla version of Python interpreter does not indent the |
|
235 code automatically. So enter four spaces there and then type the |
|
236 following |
|
237 :: |
|
238 |
|
239 print i, "cube is", i**3 |
|
240 |
|
241 Now when we hit enter, we still see the four dots, to get out of the |
|
242 block, hit enter once again |
|
243 |
|
244 .. #[Nishanth]: Here also the overhead on print can be reduced. |
|
245 Think of a simple print statement. This statement |
|
246 will be confusing for a newbie. |
|
247 We can focus more on indentation in python. |
|
248 |
|
249 .. #[nishanth]: Not sure if you must use range here. You can |
|
250 define a list of numbers and iterate on it. |
|
251 Then say this list can also be generated using |
|
252 the range function and hence introduce range. |
|
253 |
|
254 {{{ switch to the next slide, ``range()`` function }}} |
|
255 |
|
256 Okay! so the main thing that we learned here is how to use Python |
|
257 interpreter and IPython interpreter to specify blocks. But while we |
|
258 were generating the multiplication table we used something new, |
|
259 ``range()`` function. ``range()`` is an inbuilt function in Python |
|
260 which can be used to generate a ``list`` of integers from a starting |
|
261 number to an ending number. Note that the ending number that you |
|
262 specify will not be included in the ``list``. |
|
263 |
|
264 .. #[Nishanth]: Show some examples of range without the step argument |
|
265 May be give an exercise with negative numbers as arguments |
|
266 |
|
267 {{{ switch to next slide, problem statement of the next problem in |
|
268 solved exercises }}} |
|
269 |
|
270 Now, let us print all the odd numbers from 1 to 50. Pause here and try |
|
271 to solve the problem yourself. |
|
272 |
|
273 Let us do it in our IPython interpreter for ease of use. |
|
274 |
|
275 {{{ switch focus to ipython interpreter }}} |
|
276 |
|
277 The problem can be solved by just using the ``range()`` function. |
|
278 |
|
279 It can be solved as, |
|
280 :: |
|
281 |
|
282 print range(1,51,2) |
|
283 |
|
284 This time we passed three parameters to ``range()`` function unlike |
|
285 the previous case where we passed only two parameters. The first two |
|
286 parameters are the same in both the cases. The first parameter is the |
|
287 starting number of the sequence and the second parameter is the end of |
|
288 the range. Note that the sequence doesn't include the ending |
|
289 number. The third parameter is for stepping through the sequence. Here |
|
290 we gave two which means we are skipping every alternate element. |
|
291 |
|
292 {{{ switch to next slide, summary slide }}} |
|
293 |
|
294 Thus we come to the end of this tutorial. We learned about blocks in |
|
295 Python, indentation, blocks in IPython, for loop, iterating over a |
|
296 list and then the ``range()`` function. |
|
297 |
|
298 .. #[Amit]: There does seem to too much overhead of details. Should |
|
299 the first example be done using script is it necessary. |
|
300 Do add some things in evolutionary manner. Like introducing |
|
301 range as a list and doing a very very simple for loop.Like |
|
302 iterating over [1,2,3] .Before getting into a problem. |
|
303 And club details about problem in one paragraph and syntactic details |
|
304 in other. |
|
305 |
|
306 {{{ switch to next slide, thank you slide }}} |
|
307 |
|
308 Thank you! |