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