advanced-features-functions.rst
changeset 217 b595f90016c5
parent 216 7206fe0c03c5
child 218 620a644c0581
equal deleted inserted replaced
216:7206fe0c03c5 217:b595f90016c5
     1 ========
       
     2  Script
       
     3 ========
       
     4 
       
     5 {{{ show the welcome slide }}}
       
     6 
       
     7 Welcome to the tutorial on advanced feature of functions. 
       
     8 
       
     9 {{{ show the outline slide }}}
       
    10 
       
    11 In this tutorial we shall be looking at specifying default arguments
       
    12 to functions when defining them and calling functions using keyword
       
    13 arguments. We shall also, look at some of the built-in functions
       
    14 available in the standard library of Python.
       
    15 
       
    16 {{{ switch to terminal }}}
       
    17 
       
    18 We have an ``ipython`` terminal open, which we shall be using through
       
    19 out this session. 
       
    20 
       
    21 Let's use the ``round`` function as an example to understand what a
       
    22 default value of an argument means. Let's type the following
       
    23 expressions in the terminal. 
       
    24 
       
    25 ::
       
    26 
       
    27   round(2.484)
       
    28 
       
    29   round(2.484, 2)
       
    30 
       
    31 Both the first expression and the second are calls to the ``round``
       
    32 function, but the first calls it with only one argument and the second
       
    33 calls it with two arguments. By observing the output, we can guess
       
    34 that the first one is equivalent to call with the second argument
       
    35 being 0. 0 is the default value of the argument. 
       
    36 
       
    37 {{{ show a slide with examples of functions showing default values }}}
       
    38 ::
       
    39 
       
    40   s.strip() # strips on spaces. 
       
    41   s.strip('@') # strips the string of '@' symbols.
       
    42 
       
    43   plot(x, y) # plots with x vs. y using default line style. 
       
    44   plot(x, y, 'o') # plots x vs. y with circle markers. 
       
    45 
       
    46   linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi
       
    47   linspace(0, 2*pi) # returns 50 points between 0 and 2pi
       
    48 
       
    49 #[punch: all above content goes on to a slide]
       
    50 
       
    51 {{{ switch back to ipython }}}
       
    52 
       
    53 Let's now define a simple function that uses default arguments. We
       
    54 define a simple function that prints a welcome message to a person,
       
    55 given a greeting and his/her name.
       
    56 
       
    57 ::
       
    58 
       
    59   def welcome(greet, name="World"):
       
    60       print greet, name
       
    61 
       
    62 Let us first call the function with two arguments, one for ``greet``
       
    63 and other for ``name``.
       
    64 
       
    65 ::
       
    66 
       
    67   welcome("Hi", "Guido")          
       
    68 
       
    69 We get the expected welcome message, "Hi Guido". 
       
    70 
       
    71 Now let us call the function with just one argument "Hello". 
       
    72 ::
       
    73 
       
    74   welcome("Hello")
       
    75 
       
    76 "Hello" is treated as the ``greet`` and we get "Hello World" as
       
    77 the output. "World" is the default value for the argument ``name``. 
       
    78 
       
    79 E%% %% Pause the video here and redefine the function ``welcome``, by
       
    80 interchanging it's arguments. Place the ``name`` argument with it's
       
    81 default value of "Hello" before the ``greet`` argument. Then, resume
       
    82 the video. 
       
    83 
       
    84 ::
       
    85 
       
    86   def welcome(name="World", greet):
       
    87       print greet, name
       
    88 
       
    89 We get an error that reads ``SyntaxError: non-default argument follows
       
    90 default argument``. When defining a function all the argument with
       
    91 default values should come at the end. 
       
    92 
       
    93 E%% %% Pause the video here and type ``linspace?`` to see the
       
    94 definition of the command and notice how all the arguments with
       
    95 default values are towards the end.
       
    96 
       
    97 ::
       
    98 
       
    99   linspace?
       
   100 
       
   101 E%% %% Pause the video here and redefine the function ``welcome`` with
       
   102 a default value of "Hello" to the ``greet`` argument. Then, call the
       
   103 function without any arguments. Then, resume the video. 
       
   104 
       
   105 ::
       
   106 
       
   107   def welcome(greet="Hello", name="World"):
       
   108       print greet, name
       
   109  
       
   110 
       
   111   welcome()
       
   112 
       
   113 
       
   114 Let us now learn what keyword arguments are. 
       
   115 
       
   116 {{{ show a slide with examples using keyword arguments. }}}
       
   117 ::
       
   118 
       
   119   legend(['sin(2y)'], loc = 'center')
       
   120 
       
   121   plot(y, sin(y), 'g', linewidth = 2)
       
   122 
       
   123   annotate('local max', xy = (1.5, 1))
       
   124 
       
   125   pie(science.values(), labels = science.keys())
       
   126 
       
   127 When you are calling functions in Python, you don't need to remember
       
   128 the order in which to pass the arguments. Instead, you can use the
       
   129 name of the argument to pass it a value. This slide shows a few
       
   130 function calls that use keyword arguments. ``loc``, ``linewidth``,
       
   131 ``xy`` and ``labels`` are being called with keyword arguments. 
       
   132 
       
   133 {{{ switch to ipython terminal }}}
       
   134 
       
   135 Let us try and understand this better using the ``welcome`` function
       
   136 that we have been using all along. Let us call it in different ways
       
   137 and observe the output to see how keyword arguments work. 
       
   138 
       
   139 ::
       
   140 
       
   141   welcome()
       
   142 
       
   143   welcome("Hello", "James")
       
   144 
       
   145   welcome("Hi", name="Guido")
       
   146 
       
   147 When no keyword is specified, the arguments are allotted based on
       
   148 their position. So, "Hi" is the value of the argument ``greet`` and
       
   149 name is passed the value "Guido". 
       
   150 ::
       
   151 
       
   152   welcome(name="Guido", greet="Hey! ")
       
   153 
       
   154 When keyword arguments are used, the arguments can be called in any
       
   155 order. 
       
   156 
       
   157 ::
       
   158 
       
   159   welcome(name="Guido", "Hey")
       
   160 
       
   161 This call returns an error that reads, ``non keyword arg after keyword
       
   162 arg``. Python expects all the keyword to be present towards the end. 
       
   163 
       
   164 That brings us to the end of what we wanted to learn about ``keyword``
       
   165 arguments. 
       
   166 
       
   167 {{{ switch to a slide showing variety of functions with uses }}}
       
   168 
       
   169 Before defining a function of your own, make sure that you check the
       
   170 standard library, for a similar function. Python is popularly called a
       
   171 "Batteries included" language, for the huge library that comes along
       
   172 with it. 
       
   173 
       
   174 ::
       
   175 
       
   176   Math functions - abs, sin, ....
       
   177 
       
   178 #[punch: Need to decide, exactly what to put here. Reviewer comments
       
   179  welcome.] 
       
   180   
       
   181 
       
   182 {{{ switch to slide showing classes of functions in pylab, scipy }}}
       
   183 
       
   184 Apart from the standard library there are other libraries like ``pylab``,
       
   185 ``scipy``, etc which have a huge collection of functions for scientific
       
   186 purposes. 
       
   187 ::
       
   188 
       
   189   pylab
       
   190     plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
       
   191 
       
   192   scipy (modules)
       
   193     fftpack, stats, linalg, ndimage, signal, optimize, integrate
       
   194 
       
   195 {{{ switch slide to summary slide }}}
       
   196 
       
   197 That brings us to the end of this tutorial. In this tutorial we have
       
   198 learnt how to use functions with default values and keyword
       
   199 arguments. We also looked at the range of functions available in the
       
   200 Python standard library and the Scientific Computing related
       
   201 packages. 
       
   202 
       
   203 Thank You!