19 {{{ switch to next slide, outline slide }}} |
19 {{{ switch to next slide, outline slide }}} |
20 |
20 |
21 In this tutorial we will see ``for`` loops in python, and also cover |
21 In this tutorial we will see ``for`` loops in python, and also cover |
22 the basics of indenting code in python. |
22 the basics of indenting code in python. |
23 |
23 |
|
24 .. #[Nishanth]: Instead of saying basics of indenting code, |
|
25 say How to define code blocks in Python |
|
26 |
24 {{{ switch to next slide, about whitespaces }}} |
27 {{{ switch to next slide, about whitespaces }}} |
25 |
28 |
26 In Python whitespace is significant, and the blocks are visually |
29 In Python whitespace is significant, and the blocks are visually |
27 separated rather than using braces or any other mechanisms for |
30 separated rather than using braces or any other mechanisms for |
28 defining blocks. And by this method Python forces the programmers to |
31 defining blocks. And by this method Python forces the programmers to |
29 stick on to one way of writing or beautifying the code rather than |
32 stick on to one way of writing or beautifying the code rather than |
30 debating over where to place the braces. This way it produces uniform |
33 debating over where to place the braces. This way it produces uniform |
31 code than obscure or unreadable code. |
34 code than obscure or unreadable code. |
32 |
35 |
|
36 .. #[nishanth]: Simply tell how blocks are defined in python. |
|
37 The details like braces are not used and its |
|
38 advantages like neat code can be told after completely |
|
39 explaining the indentation |
|
40 |
33 A block may be defined by a suitable indentation level which can be |
41 A block may be defined by a suitable indentation level which can be |
34 either be a tab or few spaces. And the best practice is to indent the |
42 either be a tab or few spaces. And the best practice is to indent the |
35 code using four spaces. |
43 code using four spaces. |
36 |
44 |
|
45 .. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces |
|
46 for indentation. Do that while typing so that they can |
|
47 actually see what is being typed. |
|
48 |
37 Now let us move straight into ``for`` loop. |
49 Now let us move straight into ``for`` loop. |
38 |
50 |
39 {{{ switch to next slide, problem statement of exercise 1 }}} |
51 {{{ switch to next slide, problem statement of exercise 1 }}} |
40 |
52 |
41 Write a for loop which iterates through a list of numbers and find the |
53 Write a for loop which iterates through a list of numbers and find the |
42 square root of each number. Also make a new list with the square roots |
54 square root of each number. Also make a new list with the square roots |
43 and print it at the end. |
55 and print it at the end. |
|
56 |
|
57 .. #[nishanth]: making new list with square roots induces extra complication |
|
58 like appending which has no use case here |
44 :: |
59 :: |
45 |
60 |
46 numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916 |
61 numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916 |
|
62 |
|
63 .. #[Nishanth]: The problem focuses more on square root and creation |
|
64 of list. The problem must be simple and focusing on |
|
65 nothing more but the indentation and for loop. |
|
66 May be change the problem to print squares than to |
|
67 print square roots. |
47 |
68 |
48 For the problem, first we need to create a ``list`` of numbers and |
69 For the problem, first we need to create a ``list`` of numbers and |
49 then iterate over the list and find the square root of each element in |
70 then iterate over the list and find the square root of each element in |
50 it. And let us create a script, rather than typing it out in the |
71 it. And let us create a script, rather than typing it out in the |
51 interpreter itself. Create a script called list_roots.py and type the |
72 interpreter itself. Create a script called list_roots.py and type the |
61 print "Square root of", each, "is", sq_root |
82 print "Square root of", each, "is", sq_root |
62 square_roots.append(sq_root) |
83 square_roots.append(sq_root) |
63 print |
84 print |
64 print square_roots |
85 print square_roots |
65 |
86 |
|
87 .. numbers = [1, 12, 3, 4, 21, 17] |
|
88 for each in numbers: |
|
89 print each, each * each |
|
90 |
|
91 .. #[nishanth]: I don't see a use case to append the sq_root to |
|
92 square_roots. It is only complicating stuff. |
|
93 Simply iterate and print. |
|
94 |
66 {{{ save the script }}} |
95 {{{ save the script }}} |
67 |
96 |
68 Now save the script, and run it from your IPython interpreter. I |
97 Now save the script, and run it from your IPython interpreter. I |
69 assume that you have started your IPython interpreter using ``-pylab`` |
98 assume that you have started your IPython interpreter using ``-pylab`` |
70 option. |
99 option. |
71 |
100 |
72 Run the script as, |
101 Run the script as, |
73 :: |
102 :: |
74 |
103 |
75 %run -i list_roots.py |
104 %run -i list_roots.py |
|
105 |
|
106 .. #[Nishanth]: you don't have to use the -i option here |
76 |
107 |
77 {{{ run the script }}} |
108 {{{ run the script }}} |
78 |
109 |
79 So that was easy! We didn't have to find the length of the string nor |
110 So that was easy! We didn't have to find the length of the string nor |
80 address of each element of the list one by one. All what we did was |
111 address of each element of the list one by one. All what we did was |
83 ``numbers``, which is a list, another one ``each``, which is the |
114 ``numbers``, which is a list, another one ``each``, which is the |
84 element of list under consideration in each cycle of the ``for`` loop, |
115 element of list under consideration in each cycle of the ``for`` loop, |
85 and then a variable ``sq_root`` for storing the square root in each |
116 and then a variable ``sq_root`` for storing the square root in each |
86 cycle of the ``for`` loop. The variable names can be chosen by you. |
117 cycle of the ``for`` loop. The variable names can be chosen by you. |
87 |
118 |
|
119 .. #[Nishanth]: The details like we didn't have to find the length |
|
120 are relevant for people who have programmed in C or |
|
121 other languages earlier. But for a newbie it is more |
|
122 of confusing extra info. That part may be skipped. |
|
123 Simply go ahead and focus on the syntax of for loop. |
|
124 And how the variable name is used inside the for loop. |
|
125 If you modify the question to only print, the extra |
|
126 variable sq_root can also be avoided. let it be more |
|
127 about "each", "numbers" and "for". no other new names. |
|
128 |
88 {{{ show the script which was created }}} |
129 {{{ show the script which was created }}} |
89 |
130 |
90 Note that three lines after ``for`` statement, are indented using four |
131 Note that three lines after ``for`` statement, are indented using four |
91 spaces. |
132 spaces. |
92 |
133 |
93 {{{ highlight the threee lines after for statement }}} |
134 {{{ highlight the three lines after for statement }}} |
94 |
135 |
95 It means that those three lines are part of the for loop. And it is |
136 It means that those three lines are part of the for loop. And it is |
96 called a block of statements. And the seventh line or the immediate |
137 called a block of statements. And the seventh line or the immediate |
97 line after the third line in the ``for`` loop is not indented, |
138 line after the third line in the ``for`` loop is not indented, |
98 |
139 |
130 and now you will notice that, as soon as you press the return key |
171 and now you will notice that, as soon as you press the return key |
131 after for statement, the prompt changes to four dots and the cursor is |
172 after for statement, the prompt changes to four dots and the cursor is |
132 not right after the four dots but there are four spaces from the |
173 not right after the four dots but there are four spaces from the |
133 dots. The four dots tell you that you are inside a block. Now type the |
174 dots. The four dots tell you that you are inside a block. Now type the |
134 rest of the ``for`` loop, |
175 rest of the ``for`` loop, |
|
176 |
|
177 .. #[Nishanth]: Tell that IPython does auto indentation. |
|
178 |
135 :: |
179 :: |
136 |
180 |
137 sq_root = sqrt(each) |
181 sq_root = sqrt(each) |
138 print "Square root of", each, "is", sq_root |
182 print "Square root of", each, "is", sq_root |
139 |
183 |
157 :: |
201 :: |
158 |
202 |
159 for i in range(1,11): |
203 for i in range(1,11): |
160 |
204 |
161 and press enter once, and we will see that this time it shows four |
205 and press enter once, and we will see that this time it shows four |
162 dots, but the cursor is close to the dots, so we have to intend the |
206 dots, but the cursor is close to the dots, so we have to indent the |
163 block. So enter four spaces there and then type the following |
207 block. So enter four spaces there and then type the following |
164 :: |
208 :: |
165 |
209 |
166 |
210 |
167 print "10 *",i,"=",i*10 |
211 print "10 x",i,"=",i*10 |
168 |
212 |
169 Now when we hit enter, we still see the four dots, to get out of the |
213 Now when we hit enter, we still see the four dots, to get out of the |
170 block type enter once. |
214 block, hit enter once again |
|
215 |
|
216 .. #[Nishanth]: Here also the overhead on print can be reduced. |
|
217 Think of a simple print statement. This statement |
|
218 will be confusing for a newbie. |
|
219 We can focus more on indentation in python. |
|
220 |
|
221 .. #[nishanth]: Not sure if you must use range here. You can |
|
222 define a list of numbers and iterate on it. |
|
223 Then say this list can also be generated using |
|
224 the range function and hence introduce range. |
171 |
225 |
172 Okay! so the main thing here we learned is how to use Python |
226 Okay! so the main thing here we learned is how to use Python |
173 interpreter and IPython interpreter to specify blocks. But while we |
227 interpreter and IPython interpreter to specify blocks. But while we |
174 were generating the multiplication table we used something new, |
228 were generating the multiplication table we used something new, |
175 ``range()`` function. ``range()`` is an inbuilt function in Python |
229 ``range()`` function. ``range()`` is an inbuilt function in Python |
176 which can be used to generate a ``list`` of integers from a starting |
230 which can be used to generate a ``list`` of integers from a starting |
177 range to an ending range. Note that the ending number that you specify |
231 number to an ending number. Note that the ending number that you specify |
178 will not be included in the ``list``. |
232 will not be included in the ``list``. |
|
233 |
|
234 .. #[Nishanth]: Show some examples of range without the step argument |
|
235 May be give an exercise with negative numbers as arguments |
179 |
236 |
180 Now, let us print all the odd numbers from 1 to 50. Let us do it in |
237 Now, let us print all the odd numbers from 1 to 50. Let us do it in |
181 our IPython interpreter for ease of use. |
238 our IPython interpreter for ease of use. |
182 |
239 |
183 {{{ switch focus to ipython interpreter }}} |
240 {{{ switch focus to ipython interpreter }}} |