getting-started-with-for/script.rst
changeset 306 f105cfcc2498
parent 261 c7f0069d698a
child 319 e8c02b3c51ac
equal deleted inserted replaced
305:de16a94027f9 306:f105cfcc2498
    41 
    41 
    42 .. #[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
    43                 for indentation. Do that while typing so that they can
    43                 for indentation. Do that while typing so that they can
    44                 actually see what is being typed.
    44                 actually see what is being typed.
    45 
    45 
       
    46 As you can see in the slide, ``Block B`` is an inner block and it is
       
    47 indented using 4 spaces, and after ``Block B`` the next statement in
       
    48 ``Block A`` starts from the same indentation level of other ``Block
       
    49 A`` statements.
       
    50 
    46 Now let us move straight into ``for`` loop.
    51 Now let us move straight into ``for`` loop.
    47 
    52 
    48 {{{ switch to next slide, problem statement of exercise 1 }}}
    53 {{{ switch to next slide, problem statement of exercise 1 }}}
    49 
    54 
    50 
    55 
    82         print each, each * each
    87         print each, each * each
    83 
    88 
    84 .. #[nishanth]: I don't see a use case to append the sq_root to
    89 .. #[nishanth]: I don't see a use case to append the sq_root to
    85                 square_roots. It is only complicating stuff.
    90                 square_roots. It is only complicating stuff.
    86                 Simply iterate and print.
    91                 Simply iterate and print.
       
    92 
       
    93 {{{ switch to next slide, save and run script }}}
    87 
    94 
    88 {{{ save the script }}}
    95 {{{ save the script }}}
    89 
    96 
    90 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
    91 assume that you have started your IPython interpreter using ``-pylab``
    98 assume that you have started your IPython interpreter using ``-pylab``
   149 
   156 
   150 Now let us try a simple one, to print the square root of numbers in
   157 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
   158 the list. And this time let us do it right in the IPython
   152 interpreter. 
   159 interpreter. 
   153 
   160 
       
   161 {{{ switch to next slide, Indentation in ``ipython`` }}}
       
   162 
   154 {{{ switch focus to the IPython interpreter }}}
   163 {{{ switch focus to the IPython interpreter }}}
   155 
   164 
   156 So let us start with making a list. Type the following
   165 So let us start with making a list. Type the following
   157 ::
   166 ::
   158 
   167 
   164 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
   165 dots. Please note that IPython automatically indents the block. The
   174 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
   175 four dots tell you that you are inside a block. Now type the rest of
   167 the ``for`` loop,
   176 the ``for`` loop,
   168 
   177 
       
   178 {{{ switch to next slide, Indentation in ``ipython`` (cont'd) }}}
       
   179 
   169 .. #[Nishanth]: Tell that IPython does auto indentation.
   180 .. #[Nishanth]: Tell that IPython does auto indentation.
   170 
   181 
   171 ::
   182 ::
   172 
   183 
   173         print "Square root of", each, "is", sqrt(each)
   184         print "Square root of", each,
       
   185 	print "is", sqrt(each)
   174 
   186 
   175 Now we have finished the statements in the block, and still the
   187 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
   188 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
   189 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
   190 without entering anything else. It printed the square root of each
   179 number in the list, and that is executed in a ``for`` loop.
   191 number in the list, and that is executed in a ``for`` loop.
   180 
   192 
       
   193 {{{ switch to next slide, Indentation in ``python`` interpreter }}}
       
   194 
   181 Now, let us find the cube of all the numbers from one to ten. But this
   195 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.
   196 time let us try it in the vanilla version of Python interpreter.
   183 
   197 
   184 Start the vanilla version of Python interpreter by issuing the command
   198 Start the vanilla version of Python interpreter by issuing the command
   185 ``python`` in your terminal.
   199 ``python`` in your terminal.
   186 
   200 
   187 {{{ open the python interpreter in the terminal using the command
   201 {{{ open the python interpreter in the terminal using the command
   188 python to start the vanilla Python interpreter }}}
   202 python to start the vanilla Python interpreter }}}
       
   203 
       
   204 {{{ switch to next slide, Indentation in ``python`` interpreter
       
   205 (cont'd) }}}
   189 
   206 
   190 Start with,
   207 Start with,
   191 ::
   208 ::
   192     
   209     
   193     for i in range(1,11):
   210     for i in range(1,11):
   211 
   228 
   212 .. #[nishanth]: Not sure if you must use range here. You can 
   229 .. #[nishanth]: Not sure if you must use range here. You can 
   213                 define a list of numbers and iterate on it.
   230                 define a list of numbers and iterate on it.
   214                 Then say this list can also be generated using
   231                 Then say this list can also be generated using
   215                 the range function and hence introduce range.
   232                 the range function and hence introduce range.
       
   233 
       
   234 {{{ switch to the next slide, ``range()`` function }}}
   216 
   235 
   217 Okay! so the main thing here we learned is how to use Python
   236 Okay! so the main thing here we learned is how to use Python
   218 interpreter and IPython interpreter to specify blocks. But while we
   237 interpreter and IPython interpreter to specify blocks. But while we
   219 were generating the multiplication table we used something new,
   238 were generating the multiplication table we used something new,
   220 ``range()`` function. ``range()`` is an inbuilt function in Python
   239 ``range()`` function. ``range()`` is an inbuilt function in Python
   223 specify will not be included in the ``list``.
   242 specify will not be included in the ``list``.
   224 
   243 
   225 .. #[Nishanth]: Show some examples of range without the step argument
   244 .. #[Nishanth]: Show some examples of range without the step argument
   226                 May be give an exercise with negative numbers as arguments
   245                 May be give an exercise with negative numbers as arguments
   227 
   246 
   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
   247 {{{ switch to next slide, problem statement of the next problem in
   232 solved exercises }}}
   248 solved exercises }}}
       
   249 
       
   250 Now, let us print all the odd numbers from 1 to 50. Pause here and try
       
   251 to solve the problem yourself.
       
   252 
       
   253 Let us do it in our IPython interpreter for ease of use.
   233 
   254 
   234 {{{ switch focus to ipython interpreter }}}
   255 {{{ switch focus to ipython interpreter }}}
   235 
   256 
   236 The problem can be solved by just using the ``range()`` function.
   257 The problem can be solved by just using the ``range()`` function.
   237 
   258 
   246 starting number of the sequence and the second parameter is the end of
   267 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
   268 the range. Note that the sequence doesn't include the ending
   248 number. The third parameter is for stepping through the sequence. Here
   269 number. The third parameter is for stepping through the sequence. Here
   249 we gave two which means we are skipping every alternate element.
   270 we gave two which means we are skipping every alternate element.
   250 
   271 
   251 {{{ switch to next slide, recap slide }}}
   272 {{{ switch to next slide, summary slide }}}
   252 
   273 
   253 Thus we come to the end of this tutorial. We learned about blocks in
   274 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
   275 Python, indentation, blocks in IPython, for loop, iterating over a
   255 list and then the ``range()`` function.
   276 list and then the ``range()`` function.
   256 
   277