advanced-features-functions/script.rst
author bhanu
Mon, 08 Nov 2010 01:34:02 +0530
changeset 454 0c69c2b32183
parent 367 2ca9b21ed8f9
child 410 226b6e789da5
child 456 be96dc6c9743
permissions -rw-r--r--
Language check done for the script `strings`
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   :
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    22
   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
    23
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    24
Script
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    25
------
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
{{{ Show the slide containing title }}}
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
Welcome to the tutorial on advanced feature of functions. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    31
{{{ Show the outline slide }}}
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
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
    34
to functions when defining them and calling functions using keyword
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
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
    36
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
    37
computing libraries. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
{{{ switch to terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
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
    42
out this session. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
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
    45
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
    46
expressions in the terminal. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
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
  round(2.484)
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
  round(2.484, 2)
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
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
    55
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
    56
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
    57
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
    58
being 0. 0 is the default value of the argument. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
    60
.. #[[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
    61
   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
    62
   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
    63
   the function round]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
    64
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
{{{ 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
    66
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
    67
.. #[[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
    68
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
::
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
  s.strip() # strips on spaces. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
  s.strip('@') # strips the string of '@' symbols.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
  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
    75
  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
    76
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
  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
    78
  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
    79
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    80
.. #[punch: all above content goes on to a slide]
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
{{{ switch back to ipython }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
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
    85
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
    86
given a greeting and his/her name.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
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
  def welcome(greet, name="World"):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
      print greet, name
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
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
    94
and other for ``name``.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
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
  welcome("Hi", "Guido")          
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
We get the expected welcome message, "Hi Guido". 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
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
   103
::
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
  welcome("Hello")
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
"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
   108
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
   109
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   110
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
   111
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   112
{{{ 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
   113
    question 1 }}}
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   114
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   115
%%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
   116
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
   117
"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
   118
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   119
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
   120
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   121
{{{ 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
   122
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
::
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
  def welcome(name="World", greet):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
      print greet, name
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
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
   129
default argument``. When defining a function all the argument with
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
default values should come at the end. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   131
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   132
.. #[[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
   133
   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
   134
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   135
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
   136
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   137
{{{ 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
   138
    question 2 }}}
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   139
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   140
%%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
   141
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
   142
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   143
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
   144
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   145
{{{ 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
   146
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
::
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
  linspace?
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   151
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
   152
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   153
{{{ switch to next slide, problem statement }}}
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   154
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   155
%%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
   156
"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
   157
arguments. 
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   158
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   159
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
   160
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   161
{{{ switch to next slide, solution }}}
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   162
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
::
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
  def welcome(greet="Hello", name="World"):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
      print greet, name
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
 
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
  welcome()
272
08638b1e211e Slides for advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 241
diff changeset
   170
 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
Let us now learn what keyword arguments are. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
{{{ 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
   175
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   176
.. #[[Anoop: slide is missing]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   177
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
::
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
  legend(['sin(2y)'], loc = 'center')
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
  plot(y, sin(y), 'g', linewidth = 2)
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
  annotate('local max', xy = (1.5, 1))
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
  pie(science.values(), labels = science.keys())
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   188
.. #[[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
   189
   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
   190
   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
   191
   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
   192
   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
   193
   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
   194
   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
   195
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
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
   197
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
   198
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
   199
function calls that use keyword arguments. ``loc``, ``linewidth``,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
``xy`` and ``labels`` are being called with keyword arguments. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
{{{ switch to ipython terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   204
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
   205
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
   206
and observe the output to see how keyword arguments work. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
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
  welcome()
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   211
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   212
  welcome("Hello", "James")
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   213
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   214
  welcome("Hi", name="Guido")
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   215
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   216
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
   217
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
   218
name is passed the value "Guido". 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   219
::
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
  welcome(name="Guido", greet="Hey! ")
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   222
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
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
   224
order. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
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
  welcome(name="Guido", "Hey")
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   229
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
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
   231
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
   232
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
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
   234
arguments. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   235
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
{{{ switch to a slide showing variety of functions with uses }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   238
.. #[[Anoop: slide missing]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   239
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
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
   241
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
   242
"Batteries included" language, for the huge library that comes along
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   243
with it. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   244
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
  Math functions - abs, sin, ....
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   248
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   249
.. #[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
   250
..  welcome.] 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   251
  
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
{{{ switch to slide showing classes of functions in pylab, scipy }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   254
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   255
.. #[[Anoop: slide missing]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   256
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
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
   258
``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
   259
purposes. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   260
::
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
  pylab
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   263
    plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   264
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   265
  scipy (modules)
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   266
    fftpack, stats, linalg, ndimage, signal, optimize, integrate
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   268
{{{ Show summary slide }}}
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   269
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   270
.. #[[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
   271
   library]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   272
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   273
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
   274
learnt how to use functions with default values and keyword
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   275
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
   276
Python standard library and the Scientific Computing related
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   277
packages. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   278
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   279
{{{ 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
   280
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   281
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
   282
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   283
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
   284
Thank you!