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