getting_started_with_for.rst
changeset 206 8835b2c071e6
parent 203 846d71a4e915
equal deleted inserted replaced
205:d95288e57cfc 206:8835b2c071e6
    16 
    16 
    17 Hello and welcome to the tutorial getting started with ``for`` loop. 
    17 Hello and welcome to the tutorial getting started with ``for`` loop. 
    18 
    18 
    19 {{{ switch to next slide, outline slide }}}
    19 {{{ switch to next slide, outline slide }}}
    20 
    20 
    21 In this tutorial we will learn about ``for`` loops in python, and also cover
    21 In this tutorial we will learn about ``for`` loops in python, and also
    22 the basics of indenting code in python.
    22 learn how to write blocks of code in Python.
    23 
    23 
    24 .. #[Nishanth]: Instead of saying basics of indenting code,
    24 .. #[Nishanth]: Instead of saying basics of indenting code,
    25                 say How to define code blocks in Python
    25                 say How to define code blocks in Python
    26 
    26 
    27 {{{ switch to next slide, about whitespaces }}}
    27 {{{ switch to next slide, about whitespaces }}}
    28 
    28 
    29 In Python whitespace is significant, and the blocks are visually
    29 In Python whitespace is significant, and the blocks are visually
    30 separated rather than using braces or any other mechanisms for
    30 separated.
    31 defining blocks. And by this method Python forces the programmers to
       
    32 stick on to one way of writing or beautifying the code rather than
       
    33 debating over where to place the braces. This way it produces uniform
       
    34 code than obscure or unreadable code.
       
    35 
    31 
    36 .. #[nishanth]: Simply tell how blocks are defined in python.
    32 .. #[nishanth]: Simply tell how blocks are defined in python.
    37                 The details like braces are not used and its
    33                 The details like braces are not used and its
    38                 advantages like neat code can be told after completely
    34                 advantages like neat code can be told after completely
    39                 explaining the indentation
    35                 explaining the indentation
    40 
    36 
    41 .. #[Amit]: Do you want to do that here. May be its better to talk about 
    37 .. #[Amit]: Do you want to do that here. May be its better to talk about 
    42    this after some initiation into the idea of blocks. 
    38    this after some initiation into the idea of blocks. 
    43 
    39 
    44 A block may be defined by a suitable indentation level which can be
    40 The best practice is to indent the code using four spaces.
    45 either be a tab or few spaces. And the best practice is to indent the
       
    46 code using four spaces.
       
    47 
    41 
    48 .. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces
    42 .. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces
    49                 for indentation. Do that while typing so that they can
    43                 for indentation. Do that while typing so that they can
    50                 actually see what is being typed.
    44                 actually see what is being typed.
    51 
    45 
    52 Now let us move straight into ``for`` loop.
    46 Now let us move straight into ``for`` loop.
    53 
    47 
    54 {{{ switch to next slide, problem statement of exercise 1 }}}
    48 {{{ switch to next slide, problem statement of exercise 1 }}}
    55 
    49 
       
    50 
    56 Write a for loop which iterates through a list of numbers and find the
    51 Write a for loop which iterates through a list of numbers and find the
    57 square root of each number. Also make a new list with the square roots
    52 square root of each number.
    58 and print it at the end.
    53 ::
       
    54 
       
    55     numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
    59 
    56 
    60 .. #[nishanth]: making new list with square roots induces extra complication
    57 .. #[nishanth]: making new list with square roots induces extra complication
    61                 like appending which has no use case here
    58                 like appending which has no use case here
    62 ::
       
    63 
       
    64     numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
       
    65 
    59 
    66 .. #[Nishanth]: The problem focuses more on square root and creation
    60 .. #[Nishanth]: The problem focuses more on square root and creation
    67                 of list. The problem must be simple and focusing on 
    61                 of list. The problem must be simple and focusing on 
    68                 nothing more but the indentation and for loop.
    62                 nothing more but the indentation and for loop.
    69                 May be change the problem to print squares than to
    63                 May be change the problem to print squares than to
    77 
    71 
    78 {{{ open the text editor and paste the following code there }}}
    72 {{{ open the text editor and paste the following code there }}}
    79 ::
    73 ::
    80 
    74 
    81     numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
    75     numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
    82     square_roots = []
       
    83     for each in numbers:
    76     for each in numbers:
    84         sq_root = sqrt(each)
    77         print "Square root of", each, "is", sqrt(each)
    85         print "Square root of", each, "is", sq_root
    78     print "This is not in for loop!"
    86         square_roots.append(sq_root)
       
    87     print 
       
    88     print square_roots
       
    89 
    79 
    90 ..  numbers = [1, 12, 3, 4, 21, 17]
    80 ..  numbers = [1, 12, 3, 4, 21, 17]
    91     for each in numbers:
    81     for each in numbers:
    92         print each, each * each
    82         print each, each * each
    93 
    83 
   108 
    98 
   109 .. #[Nishanth]: you don't have to use the -i option here
    99 .. #[Nishanth]: you don't have to use the -i option here
   110 
   100 
   111 {{{ run the script }}}
   101 {{{ run the script }}}
   112 
   102 
   113 So that was easy! We didn't have to find the length of the string nor
   103 So that was easy! All what we did was iterate over the list element by
   114 address of each element of the list one by one. All what we did was
   104 element and then use the element for calculation. Note that here we
   115 iterate over the list element by element and then use the element for
   105 used two variables. One the variable ``numbers``, which is a list,
   116 calculation. Note that here we used three variables. One the variable
   106 another one ``each``, which is the element of list under consideration
   117 ``numbers``, which is a list, another one ``each``, which is the
   107 in each cycle of the ``for`` loop. The variable names can be chosen by
   118 element of list under consideration in each cycle of the ``for`` loop,
   108 you.
   119 and then a variable ``sq_root`` for storing the square root in each
       
   120 cycle of the ``for`` loop. The variable names can be chosen by you.
       
   121 
   109 
   122 .. #[Nishanth]: The details like we didn't have to find the length
   110 .. #[Nishanth]: The details like we didn't have to find the length
   123                 are relevant for people who have programmed in C or
   111                 are relevant for people who have programmed in C or
   124                 other languages earlier. But for a newbie it is more
   112                 other languages earlier. But for a newbie it is more
   125                 of confusing extra info. That part may be skipped.
   113                 of confusing extra info. That part may be skipped.
   129                 variable sq_root can also be avoided. let it be more
   117                 variable sq_root can also be avoided. let it be more
   130                 about "each", "numbers" and "for". no other new names.
   118                 about "each", "numbers" and "for". no other new names.
   131 
   119 
   132 {{{ show the script which was created }}}
   120 {{{ show the script which was created }}}
   133 
   121 
   134 Note that three lines after ``for`` statement, are indented using four
   122 Note that the lines after ``for`` statement, is indented using four
   135 spaces.
   123 spaces.
   136 
   124 
   137 {{{ highlight the three lines after for statement }}}
   125 {{{ highlight the line after for statement }}}
   138 
   126 
   139 It means that those three lines are part of the for loop. And it is
   127 It means that line is part of the for loop. And it is a block of code,
   140 called a block of statements. And the seventh line or the immediate
   128 although it is only a single statement in the block. And the fourth
   141 line after the third line in the ``for`` loop is not indented, 
   129 line or the immediate line after the ``for`` block is not indented,
   142 
   130 
   143 {{{ highlight the seventh line - the line just after for loop }}}
   131 {{{ highlight the fourth line - the line just after for loop }}}
   144 
   132 
   145 it means that it is not part of the ``for`` loop and the lines after
   133 it means that it is not part of the ``for`` loop and the lines after
   146 that doesn't fall in the scope of the ``for`` loop. Thus each block is
   134 that doesn't fall in the scope of the ``for`` loop. Thus each block is
   147 separated by the indentation level. Thus marking the importance of
   135 separated by the indentation level. Thus marking the importance of
   148 white-spaces in Python.
   136 white-spaces in Python.
   155 ::
   143 ::
   156     
   144     
   157     7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916
   145     7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916
   158 
   146 
   159 {{{ switch to next slide, problem statement of second problem in
   147 {{{ switch to next slide, problem statement of second problem in
   160 solved exercie}}}
   148 solved exercise}}}
   161 
   149 
   162 Now let us try a simple one, to print the square root of numbers in
   150 Now let us try a simple one, to print the square root of numbers in
   163 the list. And this time let us do it right in the IPython
   151 the list. And this time let us do it right in the IPython
   164 interpreter. 
   152 interpreter. 
   165 
   153 
   172     for each in numbers:
   160     for each in numbers:
   173 
   161 
   174 and now you will notice that, as soon as you press the return key
   162 and now you will notice that, as soon as you press the return key
   175 after for statement, the prompt changes to four dots and the cursor is
   163 after for statement, the prompt changes to four dots and the cursor is
   176 not right after the four dots but there are four spaces from the
   164 not right after the four dots but there are four spaces from the
   177 dots. The four dots tell you that you are inside a block. Now type the
   165 dots. Please note that IPython automatically indents the block. The
   178 rest of the ``for`` loop,
   166 four dots tell you that you are inside a block. Now type the rest of
       
   167 the ``for`` loop,
   179 
   168 
   180 .. #[Nishanth]: Tell that IPython does auto indentation.
   169 .. #[Nishanth]: Tell that IPython does auto indentation.
   181 
   170 
   182 ::
   171 ::
   183 
   172 
   184         sq_root = sqrt(each)
   173         print "Square root of", each, "is", sqrt(each)
   185         print "Square root of", each, "is", sq_root
       
   186 
   174 
   187 Now we have finished the statements in the block, and still the
   175 Now we have finished the statements in the block, and still the
   188 interpreter is showing four dots, which means you are still inside the
   176 interpreter is showing four dots, which means you are still inside the
   189 block. To exit from the block press return key or the enter key twice
   177 block. To exit from the block press return key or the enter key twice
   190 without entering anything else. It printed the square root of each
   178 without entering anything else. It printed the square root of each
   191 number in the list, and that is executed in a ``for`` loop.
   179 number in the list, and that is executed in a ``for`` loop.
   192 
   180 
   193 Now, let us generate the multiplication table of 10 from one to
   181 Now, let us find the cube of all the numbers from one to ten. But this
   194 ten. But this time let us try it in the vanilla version of Python
   182 time let us try it in the vanilla version of Python interpreter.
   195 interpreter.
       
   196 
   183 
   197 Start the vanilla version of Python interpreter by issuing the command
   184 Start the vanilla version of Python interpreter by issuing the command
   198 ``python`` in your terminal.
   185 ``python`` in your terminal.
   199 
   186 
   200 {{{ open the python interpreter in the terminal using the command
   187 {{{ open the python interpreter in the terminal using the command
   205     
   192     
   206     for i in range(1,11):
   193     for i in range(1,11):
   207 
   194 
   208 and press enter once, and we will see that this time it shows four
   195 and press enter once, and we will see that this time it shows four
   209 dots, but the cursor is close to the dots, so we have to indent the
   196 dots, but the cursor is close to the dots, so we have to indent the
   210 block. So enter four spaces there and then type the following
   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
   211 ::
   200 ::
   212     
   201     
   213     
   202         print i, "cube is", i**3
   214         print "10 x",i,"=",i*10
       
   215 
   203 
   216 Now when we hit enter, we still see the four dots, to get out of the
   204 Now when we hit enter, we still see the four dots, to get out of the
   217 block, hit enter once again
   205 block, hit enter once again
   218 
   206 
   219 .. #[Nishanth]: Here also the overhead on print can be reduced.
   207 .. #[Nishanth]: Here also the overhead on print can be reduced.
   229 Okay! so the main thing here we learned is how to use Python
   217 Okay! so the main thing here we learned is how to use Python
   230 interpreter and IPython interpreter to specify blocks. But while we
   218 interpreter and IPython interpreter to specify blocks. But while we
   231 were generating the multiplication table we used something new,
   219 were generating the multiplication table we used something new,
   232 ``range()`` function. ``range()`` is an inbuilt function in Python
   220 ``range()`` function. ``range()`` is an inbuilt function in Python
   233 which can be used to generate a ``list`` of integers from a starting
   221 which can be used to generate a ``list`` of integers from a starting
   234 number to an ending number. Note that the ending number that you specify
   222 number to an ending number. Note that the ending number that you
   235 will not be included in the ``list``.
   223 specify will not be included in the ``list``.
   236 
   224 
   237 .. #[Nishanth]: Show some examples of range without the step argument
   225 .. #[Nishanth]: Show some examples of range without the step argument
   238                 May be give an exercise with negative numbers as arguments
   226                 May be give an exercise with negative numbers as arguments
   239 
   227 
   240 Now, let us print all the odd numbers from 1 to 50. Let us do it in
   228 Now, let us print all the odd numbers from 1 to 50. Let us do it in
   241 our IPython interpreter for ease of use.
   229 our IPython interpreter for ease of use.
   242 
   230 
   243 {{{ switch focus to ipython interpreter }}}
       
   244 
       
   245 {{{ switch to next slide, problem statement of the next problem in
   231 {{{ switch to next slide, problem statement of the next problem in
   246 solved exercises }}}
   232 solved exercises }}}
   247 
   233 
   248 Print the list of odd numbers from 1 to 50. It will be better if
   234 {{{ switch focus to ipython interpreter }}}
   249 you can try it out yourself.
   235 
   250 
   236 The problem can be solved by just using the ``range()`` function.
   251 It is a very trivial problem and can be solved as,
   237 
       
   238 It can be solved as,
   252 ::
   239 ::
   253 
   240 
   254     print range(1,51,2)
   241     print range(1,51,2)
   255 
   242 
   256 This time we passed three parameters to ``range()`` function unlike
   243 This time we passed three parameters to ``range()`` function unlike