getting_started_with_for.rst
changeset 161 b7c47307e510
child 188 d0b76bc48aef
equal deleted inserted replaced
158:b459053192f1 161:b7c47307e510
       
     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 see ``for`` loops in python, and also cover
       
    22 the basics of indenting code in python.
       
    23 
       
    24 {{{ switch to next slide, about whitespaces }}}
       
    25 
       
    26 In Python whitespace is significant, and the blocks are visually
       
    27 separated rather than using braces or any other mechanisms for
       
    28 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
       
    30 debating over where to place the braces. This way it produces uniform
       
    31 code than obscure or unreadable code.
       
    32 
       
    33 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
       
    35 code using four spaces.
       
    36 
       
    37 Now let us move straight into ``for`` loop.
       
    38 
       
    39 {{{ switch to next slide,  problem statement of exercise 1 }}}
       
    40 
       
    41 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
       
    43 and print it at the end.
       
    44 ::
       
    45 
       
    46     numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
       
    47 
       
    48 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
       
    50 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
       
    52 following.
       
    53 
       
    54 {{{ open the text editor and paste the following code there }}}
       
    55 ::
       
    56 
       
    57     numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
       
    58     square_roots = []
       
    59     for each in numbers:
       
    60         sq_root = sqrt(each)
       
    61         print "Square root of", each, "is", sq_root
       
    62         square_roots.append(sq_root)
       
    63     print 
       
    64     print square_roots
       
    65 
       
    66 {{{ save the script }}}
       
    67 
       
    68 Now save the script, and run it from your IPython interpreter. I
       
    69 assume that you have started your IPython interpreter using ``-pylab``
       
    70 option.
       
    71 
       
    72 Run the script as,
       
    73 ::
       
    74 
       
    75     %run -i list_roots.py
       
    76 
       
    77 {{{ run the script }}}
       
    78 
       
    79 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
       
    81 iterate over the list element by element and then use the element for
       
    82 calculation. Note that here we used three variables. One the variable
       
    83 ``numbers``, which is a list, another one ``each``, which is the
       
    84 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
       
    86 cycle of the ``for`` loop. The variable names can be chosen by you.
       
    87 
       
    88 {{{ show the script which was created }}}
       
    89 
       
    90 Note that three lines after ``for`` statement, are indented using four
       
    91 spaces.
       
    92 
       
    93 {{{ highlight the threee lines after for statement }}}
       
    94 
       
    95 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
       
    97 line after the third line in the ``for`` loop is not indented, 
       
    98 
       
    99 {{{ highlight the seventh line - the line just after for loop }}}
       
   100 
       
   101 it means that it is not part of the ``for`` loop and the lines after
       
   102 that doesn't fall in the scope of the ``for`` loop. Thus each block is
       
   103 separated by the indentation level. Thus marking the importance of
       
   104 white-spaces in Python.
       
   105 
       
   106 {{{ switch to the slide which shows the problem statement of the first
       
   107 problem to be tried out }}}
       
   108 
       
   109 Now a question for you to try, from the given numbers make a list of
       
   110 perfect squares and a list of those which are not. The numbers are,
       
   111 ::
       
   112     
       
   113     7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916
       
   114 
       
   115 {{{ switch to next slide, problem statement of second problem in
       
   116 solved exercie}}}
       
   117 
       
   118 Now let us try a simple one, to print the square root of numbers in
       
   119 the list. And this time let us do it right in the IPython
       
   120 interpreter. 
       
   121 
       
   122 {{{ switch focus to the IPython interpreter }}}
       
   123 
       
   124 So let us start with making a list. Type the following
       
   125 ::
       
   126 
       
   127     numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916]
       
   128     for each in numbers:
       
   129 
       
   130 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
       
   132 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
       
   134 rest of the ``for`` loop,
       
   135 ::
       
   136 
       
   137         sq_root = sqrt(each)
       
   138         print "Square root of", each, "is", sq_root
       
   139 
       
   140 Now we have finished the statements in the block, and still the
       
   141 interpreter is showing four dots, which means you are still inside the
       
   142 block. To exit from the block press return key or the enter key twice
       
   143 without entering anything else. It printed the square root of each
       
   144 number in the list, and that is executed in a ``for`` loop.
       
   145 
       
   146 Now, let us generate the multiplication table of 10 from one to
       
   147 ten. But this time let us try it in the vanilla version of Python
       
   148 interpreter.
       
   149 
       
   150 Start the vanilla version of Python interpreter by issuing the command
       
   151 ``python`` in your terminal.
       
   152 
       
   153 {{{ open the python interpreter in the terminal using the command
       
   154 python to start the vanilla Python interpreter }}}
       
   155 
       
   156 Start with,
       
   157 ::
       
   158     
       
   159     for i in range(1,11):
       
   160 
       
   161 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
       
   163 block. So enter four spaces there and then type the following
       
   164 ::
       
   165     
       
   166     
       
   167         print "10 *",i,"=",i*10
       
   168 
       
   169 Now when we hit enter, we still see the four dots, to get out of the
       
   170 block type enter once.
       
   171 
       
   172 Okay! so the main thing here we learned is how to use Python
       
   173 interpreter and IPython interpreter to specify blocks. But while we
       
   174 were generating the multiplication table we used something new,
       
   175 ``range()`` function. ``range()`` is an inbuilt function in Python
       
   176 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
       
   178 will not be included in the ``list``.
       
   179 
       
   180 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.
       
   182 
       
   183 {{{ switch focus to ipython interpreter }}}
       
   184 
       
   185 {{{ switch to next slide, problem statement of the next problem in
       
   186 solved exercises }}}
       
   187 
       
   188 Print the list of odd numbers from 1 to 50. It will be better if
       
   189 you can try it out yourself.
       
   190 
       
   191 It is a very trivial problem and can be solved as,
       
   192 ::
       
   193 
       
   194     print range(1,51,2)
       
   195 
       
   196 This time we passed three parameters to ``range()`` function unlike
       
   197 the previous case where we passed only two parameters. The first two
       
   198 parameters are the same in both the cases. The first parameter is the
       
   199 starting number of the sequence and the second parameter is the end of
       
   200 the range. Note that the sequence doesn't include the ending
       
   201 number. The third parameter is for stepping through the sequence. Here
       
   202 we gave two which means we are skipping every alternate element.
       
   203 
       
   204 {{{ switch to next slide, recap slide }}}
       
   205 
       
   206 Thus we come to the end of this tutorial. We learned about blocks in
       
   207 Python, indentation, blocks in IPython, for loop, iterating over a
       
   208 list and then the ``range()`` function.
       
   209 
       
   210 {{{ switch to next slide, thank you slide }}}
       
   211 
       
   212 Thank you!
       
   213 
       
   214 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
       
   215     Reviewer 1:
       
   216     Reviewer 2:
       
   217     External reviewer: