advanced-features-functions/script.rst
changeset 367 2ca9b21ed8f9
parent 272 08638b1e211e
child 410 226b6e789da5
child 456 be96dc6c9743
equal deleted inserted replaced
366:894591150f7b 367:2ca9b21ed8f9
    15 
    15 
    16 ..   1. getting started with ipython
    16 ..   1. getting started with ipython
    17 ..   #. getting started with functions
    17 ..   #. getting started with functions
    18      
    18      
    19 .. Author              : Puneeth 
    19 .. Author              : Puneeth 
    20    Internal Reviewer   : 
    20    Internal Reviewer   : Anoop Jacob Thomas<anoop@fossee.in>
    21    External Reviewer   :
    21    External Reviewer   :
    22    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    22    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    23 
    23 
    24 Script
    24 Script
    25 ------
    25 ------
    55 function, but the first calls it with only one argument and the second
    55 function, but the first calls it with only one argument and the second
    56 calls it with two arguments. By observing the output, we can guess
    56 calls it with two arguments. By observing the output, we can guess
    57 that the first one is equivalent to call with the second argument
    57 that the first one is equivalent to call with the second argument
    58 being 0. 0 is the default value of the argument. 
    58 being 0. 0 is the default value of the argument. 
    59 
    59 
       
    60 .. #[[Anoop: It will be good if we show ``round??`` and tell them the
       
    61    optional argument ndigits, or it could be given as an
       
    62    exercise(solved) asking them to find the name of the argument in
       
    63    the function round]]
       
    64 
    60 {{{ show a slide with examples of functions showing default values }}}
    65 {{{ show a slide with examples of functions showing default values }}}
       
    66 
       
    67 .. #[[Anoop: I think the slide is not there]]
       
    68 
    61 ::
    69 ::
    62 
    70 
    63   s.strip() # strips on spaces. 
    71   s.strip() # strips on spaces. 
    64   s.strip('@') # strips the string of '@' symbols.
    72   s.strip('@') # strips the string of '@' symbols.
    65 
    73 
    99 "Hello" is treated as the ``greet`` and we get "Hello World" as
   107 "Hello" is treated as the ``greet`` and we get "Hello World" as
   100 the output. "World" is the default value for the argument ``name``. 
   108 the output. "World" is the default value for the argument ``name``. 
   101 
   109 
   102 Following is an (are) exercise(s) that you must do. 
   110 Following is an (are) exercise(s) that you must do. 
   103 
   111 
       
   112 {{{ switch to next slide, containing problem statement of 
       
   113     question 1 }}}
       
   114 
   104 %%1%% Redefine the function ``welcome``, by interchanging it's
   115 %%1%% Redefine the function ``welcome``, by interchanging it's
   105 arguments. Place the ``name`` argument with it's default value of
   116 arguments. Place the ``name`` argument with it's default value of
   106 "World" before the ``greet`` argument.
   117 "World" before the ``greet`` argument.
   107 
   118 
   108 Please, pause the video here. Do the exercise and then continue. 
   119 Please, pause the video here. Do the exercise and then continue. 
   109 
   120 
       
   121 {{{ switch to next slide, containing the solution to problem 1 }}}
       
   122 
   110 ::
   123 ::
   111 
   124 
   112   def welcome(name="World", greet):
   125   def welcome(name="World", greet):
   113       print greet, name
   126       print greet, name
   114 
   127 
   115 We get an error that reads ``SyntaxError: non-default argument follows
   128 We get an error that reads ``SyntaxError: non-default argument follows
   116 default argument``. When defining a function all the argument with
   129 default argument``. When defining a function all the argument with
   117 default values should come at the end. 
   130 default values should come at the end. 
   118 
   131 
       
   132 .. #[[Anoop: In the slide, "when defining a function all the default
       
   133    arguments must be defined at the end" has to be emphasized"]]
       
   134 
   119 Following is an exercise that you must do. 
   135 Following is an exercise that you must do. 
       
   136 
       
   137 {{{ switch to next slide, containing the problem statement of 
       
   138     question 2 }}}
   120 
   139 
   121 %%2%% See the definition of linspace using ``?`` and observe how all
   140 %%2%% See the definition of linspace using ``?`` and observe how all
   122 the arguments with default values are towards the end.
   141 the arguments with default values are towards the end.
   123 
   142 
   124 Please, pause the video here. Do the exercise and then continue. 
   143 Please, pause the video here. Do the exercise and then continue. 
   125 
   144 
       
   145 {{{ switch to next slide, containing solution to problem 2 }}}
       
   146 
   126 ::
   147 ::
   127 
   148 
   128   linspace?
   149   linspace?
   129 
   150 
   130 Following is an exercise that you must do. 
   151 Following is an exercise that you must do. 
       
   152 
       
   153 {{{ switch to next slide, problem statement }}}
   131 
   154 
   132 %%3%% Redefine the function ``welcome`` with a default value of
   155 %%3%% Redefine the function ``welcome`` with a default value of
   133 "Hello" to the ``greet`` argument. Then, call the function without any
   156 "Hello" to the ``greet`` argument. Then, call the function without any
   134 arguments. 
   157 arguments. 
   135 
   158 
   136 Please, pause the video here. Do the exercise and then continue. 
   159 Please, pause the video here. Do the exercise and then continue. 
   137 
   160 
       
   161 {{{ switch to next slide, solution }}}
       
   162 
   138 ::
   163 ::
   139 
   164 
   140   def welcome(greet="Hello", name="World"):
   165   def welcome(greet="Hello", name="World"):
   141       print greet, name
   166       print greet, name
   142  
   167  
   145  
   170  
   146 
   171 
   147 Let us now learn what keyword arguments are. 
   172 Let us now learn what keyword arguments are. 
   148 
   173 
   149 {{{ show a slide with examples using keyword arguments. }}}
   174 {{{ show a slide with examples using keyword arguments. }}}
       
   175 
       
   176 .. #[[Anoop: slide is missing]]
       
   177 
   150 ::
   178 ::
   151 
   179 
   152   legend(['sin(2y)'], loc = 'center')
   180   legend(['sin(2y)'], loc = 'center')
   153 
   181 
   154   plot(y, sin(y), 'g', linewidth = 2)
   182   plot(y, sin(y), 'g', linewidth = 2)
   155 
   183 
   156   annotate('local max', xy = (1.5, 1))
   184   annotate('local max', xy = (1.5, 1))
   157 
   185 
   158   pie(science.values(), labels = science.keys())
   186   pie(science.values(), labels = science.keys())
       
   187 
       
   188 .. #[[Anoop: I think it will better to introduce keyword arguments as
       
   189    keyword/named arguments, as the keyword term was quite confusing
       
   190    for me, so can be for someone who already know certain
       
   191    jargon's/concepts, also it would be good to tell them that these
       
   192    are different from keywords in programming languages, explicit is
       
   193    better than implicit, and probably you could also tell them that
       
   194    from now on we will refer to it as just keyword arguments]]
   159 
   195 
   160 When you are calling functions in Python, you don't need to remember
   196 When you are calling functions in Python, you don't need to remember
   161 the order in which to pass the arguments. Instead, you can use the
   197 the order in which to pass the arguments. Instead, you can use the
   162 name of the argument to pass it a value. This slide shows a few
   198 name of the argument to pass it a value. This slide shows a few
   163 function calls that use keyword arguments. ``loc``, ``linewidth``,
   199 function calls that use keyword arguments. ``loc``, ``linewidth``,
   197 That brings us to the end of what we wanted to learn about ``keyword``
   233 That brings us to the end of what we wanted to learn about ``keyword``
   198 arguments. 
   234 arguments. 
   199 
   235 
   200 {{{ switch to a slide showing variety of functions with uses }}}
   236 {{{ switch to a slide showing variety of functions with uses }}}
   201 
   237 
       
   238 .. #[[Anoop: slide missing]]
       
   239 
   202 Before defining a function of your own, make sure that you check the
   240 Before defining a function of your own, make sure that you check the
   203 standard library, for a similar function. Python is popularly called a
   241 standard library, for a similar function. Python is popularly called a
   204 "Batteries included" language, for the huge library that comes along
   242 "Batteries included" language, for the huge library that comes along
   205 with it. 
   243 with it. 
   206 
   244 
   212 ..  welcome.] 
   250 ..  welcome.] 
   213   
   251   
   214 
   252 
   215 {{{ switch to slide showing classes of functions in pylab, scipy }}}
   253 {{{ switch to slide showing classes of functions in pylab, scipy }}}
   216 
   254 
       
   255 .. #[[Anoop: slide missing]]
       
   256 
   217 Apart from the standard library there are other libraries like ``pylab``,
   257 Apart from the standard library there are other libraries like ``pylab``,
   218 ``scipy``, etc which have a huge collection of functions for scientific
   258 ``scipy``, etc which have a huge collection of functions for scientific
   219 purposes. 
   259 purposes. 
   220 ::
   260 ::
   221 
   261 
   224 
   264 
   225   scipy (modules)
   265   scipy (modules)
   226     fftpack, stats, linalg, ndimage, signal, optimize, integrate
   266     fftpack, stats, linalg, ndimage, signal, optimize, integrate
   227 
   267 
   228 {{{ Show summary slide }}}
   268 {{{ Show summary slide }}}
       
   269 
       
   270 .. #[[Anoop: add range of functions available in python standard
       
   271    library]]
   229 
   272 
   230 That brings us to the end of this tutorial. In this tutorial we have
   273 That brings us to the end of this tutorial. In this tutorial we have
   231 learnt how to use functions with default values and keyword
   274 learnt how to use functions with default values and keyword
   232 arguments. We also looked at the range of functions available in the
   275 arguments. We also looked at the range of functions available in the
   233 Python standard library and the Scientific Computing related
   276 Python standard library and the Scientific Computing related