advanced_features_of_functions/script.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. At the end of this tutorial, you will be able to 
       
     5 
       
     6 .. 1. Assign default values to arguments, when defining functions
       
     7 .. 2. Define and call functions with keyword arguments. 
       
     8 .. 3. Also, you will get a glimpse of the plethora of functions
       
     9 .. available, in Python standard library and the scientific computing
       
    10 .. libraries. 
       
    11 
       
    12 
       
    13 .. Prerequisites
       
    14 .. -------------
       
    15 
       
    16 ..   1. getting started with ipython
       
    17 ..   #. getting started with functions
       
    18      
       
    19 .. Author              : Puneeth 
       
    20    Internal Reviewer   : Anoop Jacob Thomas<anoop@fossee.in>
       
    21    External Reviewer   :
       
    22    Language Reviewer   : Bhanukiran
       
    23    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
       
    24 
       
    25 Script
       
    26 ------
       
    27 
       
    28 {{{ Show the slide containing title }}}
       
    29 
       
    30 Welcome to the tutorial on advanced feature of functions. 
       
    31 
       
    32 {{{ Show the outline slide }}}
       
    33 
       
    34 In this tutorial we shall be looking at specifying default arguments
       
    35 to functions when defining them and calling functions using keyword
       
    36 arguments. We shall also, look at some of the built-in functions
       
    37 available in the standard library of Python and the scientific
       
    38 computing libraries. 
       
    39 
       
    40 {{{ switch to terminal }}}
       
    41 
       
    42 We have an ``ipython`` terminal open, which we shall be using through
       
    43 out this session. 
       
    44 
       
    45 Let's use the ``round`` function as an example to understand what a
       
    46 default value of an argument means. Let's type the following
       
    47 expressions in the terminal. 
       
    48 
       
    49 ::
       
    50 
       
    51   round(2.484)
       
    52 
       
    53   round(2.484, 2)
       
    54 
       
    55 Both the first expression and the second are calls to the ``round``
       
    56 function, but the first calls it with only one argument and the second
       
    57 calls it with two arguments. By observing the output, we can guess
       
    58 that the first one is equivalent to call with the second argument
       
    59 being 0. 0 is the default value of the argument. 
       
    60 
       
    61 .. #[[Anoop: It will be good if we show ``round??`` and tell them the
       
    62    optional argument ndigits, or it could be given as an
       
    63    exercise(solved) asking them to find the name of the argument in
       
    64    the function round]]
       
    65 
       
    66 {{{ show a slide with examples of functions showing default values }}}
       
    67 
       
    68 .. #[[Anoop: I think the slide is not there]]
       
    69 
       
    70 ::
       
    71 
       
    72   s.strip() # strips on spaces. 
       
    73   s.strip('@') # strips the string of '@' symbols.
       
    74 
       
    75   plot(x, y) # plots with x vs. y using default line style. 
       
    76   plot(x, y, 'o') # plots x vs. y with circle markers. 
       
    77 
       
    78   linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi
       
    79   linspace(0, 2*pi) # returns 50 points between 0 and 2pi
       
    80 
       
    81 .. #[punch: all above content goes on to a slide]
       
    82 
       
    83 {{{ switch back to ipython }}}
       
    84 
       
    85 Let's now define a simple function that uses default arguments. We
       
    86 define a simple function that prints a welcome message to a person,
       
    87 given a greeting and his/her name.
       
    88 
       
    89 ::
       
    90 
       
    91   def welcome(greet, name="World"):
       
    92       print greet, name
       
    93 
       
    94 Let us first call the function with two arguments, one for ``greet``
       
    95 and other for ``name``.
       
    96 
       
    97 ::
       
    98 
       
    99   welcome("Hi", "Guido")          
       
   100 
       
   101 We get the expected welcome message, "Hi Guido". 
       
   102 
       
   103 Now let us call the function with just one argument "Hello". 
       
   104 ::
       
   105 
       
   106   welcome("Hello")
       
   107 
       
   108 "Hello" is treated as the ``greet`` and we get "Hello World" as
       
   109 the output. "World" is the default value for the argument ``name``. 
       
   110 
       
   111 Following is an (are) exercise(s) that you must do. 
       
   112 
       
   113 {{{ switch to next slide, containing problem statement of 
       
   114     question 1 }}}
       
   115 
       
   116 %%1%% Redefine the function ``welcome``, by interchanging it's
       
   117 arguments. Place the ``name`` argument with it's default value of
       
   118 "World" before the ``greet`` argument.
       
   119 
       
   120 Please, pause the video here. Do the exercise and then continue. 
       
   121 
       
   122 {{{ switch to next slide, containing the solution to problem 1 }}}
       
   123 
       
   124 ::
       
   125 
       
   126   def welcome(name="World", greet):
       
   127       print greet, name
       
   128 
       
   129 We get an error that reads ``SyntaxError: non-default argument follows
       
   130 default argument``. When defining a function all the argument with
       
   131 default values should come at the end. 
       
   132 
       
   133 .. #[[Anoop: In the slide, "when defining a function all the default
       
   134    arguments must be defined at the end" has to be emphasized"]]
       
   135 
       
   136 Following is an exercise that you must do. 
       
   137 
       
   138 {{{ switch to next slide, containing the problem statement of 
       
   139     question 2 }}}
       
   140 
       
   141 %%2%% See the definition of linspace using ``?`` and observe how all
       
   142 the arguments with default values are towards the end.
       
   143 
       
   144 Please, pause the video here. Do the exercise and then continue. 
       
   145 
       
   146 {{{ switch to next slide, containing solution to problem 2 }}}
       
   147 
       
   148 ::
       
   149 
       
   150   linspace?
       
   151 
       
   152 Following is an exercise that you must do. 
       
   153 
       
   154 {{{ switch to next slide, problem statement }}}
       
   155 
       
   156 %%3%% Redefine the function ``welcome`` with a default value of
       
   157 "Hello" to the ``greet`` argument. Then, call the function without any
       
   158 arguments. 
       
   159 
       
   160 Please, pause the video here. Do the exercise and then continue. 
       
   161 
       
   162 {{{ switch to next slide, solution }}}
       
   163 
       
   164 ::
       
   165 
       
   166   def welcome(greet="Hello", name="World"):
       
   167       print greet, name
       
   168  
       
   169 
       
   170   welcome()
       
   171  
       
   172 
       
   173 Let us now learn what keyword arguments or named arguments are. We
       
   174 shall refer to them as keyword arguments, henceforth. 
       
   175 
       
   176 {{{ show a slide with examples using keyword arguments. }}}
       
   177 
       
   178 .. #[[Anoop: slide is missing]]
       
   179 
       
   180 ::
       
   181 
       
   182   legend(['sin(2y)'], loc = 'center')
       
   183 
       
   184   plot(y, sin(y), 'g', linewidth = 2)
       
   185 
       
   186   annotate('local max', xy = (1.5, 1))
       
   187 
       
   188   pie(science.values(), labels = science.keys())
       
   189 
       
   190 When you are calling functions in Python, you don't need to remember
       
   191 the order in which to pass the arguments. Instead, you can use the
       
   192 name of the argument to pass it a value. This slide shows a few
       
   193 function calls that use keyword arguments. ``loc``, ``linewidth``,
       
   194 ``xy`` and ``labels`` are being called with keyword arguments. 
       
   195 
       
   196 {{{ switch to ipython terminal }}}
       
   197 
       
   198 Let us try and understand this better using the ``welcome`` function
       
   199 that we have been using all along. Let us call it in different ways
       
   200 and observe the output to see how keyword arguments work. 
       
   201 
       
   202 ::
       
   203 
       
   204   welcome()
       
   205 
       
   206   welcome("Hello", "James")
       
   207 
       
   208   welcome("Hi", name="Guido")
       
   209 
       
   210 When no keyword is specified, the arguments are allotted based on
       
   211 their position. So, "Hi" is the value of the argument ``greet`` and
       
   212 name is passed the value "Guido". 
       
   213 ::
       
   214 
       
   215   welcome(name="Guido", greet="Hey! ")
       
   216 
       
   217 When keyword arguments are used, the arguments can be called in any
       
   218 order. 
       
   219 
       
   220 ::
       
   221 
       
   222   welcome(name="Guido", "Hey")
       
   223 
       
   224 This call returns an error that reads, ``non keyword arg after keyword
       
   225 arg``. Python expects all the keyword to be present towards the end. 
       
   226 
       
   227 That brings us to the end of what we wanted to learn about ``keyword``
       
   228 arguments. 
       
   229 
       
   230 {{{ switch to a slide showing variety of functions with uses }}}
       
   231 
       
   232 .. #[[Anoop: slide missing]]
       
   233 
       
   234 Before defining a function of your own, make sure that you check the
       
   235 standard library, for a similar function. Python is popularly called a
       
   236 "Batteries included" language, for the huge library that comes along
       
   237 with it. 
       
   238 
       
   239 ::
       
   240 
       
   241   Math functions - abs, sin, ....
       
   242 
       
   243 .. #[punch: Need to decide, exactly what to put here. Reviewer comments
       
   244 ..  welcome.] 
       
   245   
       
   246 {{{ switch to slide showing classes of functions in pylab, scipy }}}
       
   247 
       
   248 .. #[[Anoop: slide missing]]
       
   249 
       
   250 Apart from the standard library there are other libraries like ``pylab``,
       
   251 ``scipy``, etc which have a huge collection of functions for scientific
       
   252 purposes. 
       
   253 ::
       
   254 
       
   255   pylab
       
   256     plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
       
   257 
       
   258   scipy (modules)
       
   259     fftpack, stats, linalg, ndimage, signal, optimize, integrate
       
   260 
       
   261 {{{ Show summary slide }}}
       
   262 
       
   263 .. #[[Anoop: add range of functions available in python standard
       
   264    library]]
       
   265 
       
   266 That brings us to the end of this tutorial. In this tutorial we have
       
   267 learnt how to use functions with default values and keyword
       
   268 arguments. We also looked at the range of functions available in the
       
   269 Python standard library and the Scientific Computing related
       
   270 packages. 
       
   271 
       
   272 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   273 
       
   274 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   275 
       
   276 Hope you have enjoyed and found it useful.
       
   277 Thank you!