advanced-features-functions/script.rst
author anand
Thu, 11 Nov 2010 00:03:57 +0530
changeset 472 fcdec2d28c9a
parent 410 226b6e789da5
child 457 68813d8d80fb
permissions -rw-r--r--
Checklist OK for `The other kinds of plots`
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
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 367
diff changeset
   172
Let us now learn what keyword arguments or named arguments are. We
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 367
diff changeset
   173
shall refer to them as keyword arguments, henceforth. 
217
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
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
   190
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
   191
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
   192
function calls that use keyword arguments. ``loc``, ``linewidth``,
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
``xy`` and ``labels`` are being called with keyword arguments. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
{{{ switch to ipython terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
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
   198
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
   199
and observe the output to see how keyword arguments work. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
  welcome()
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
  welcome("Hello", "James")
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   206
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
  welcome("Hi", name="Guido")
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
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
   210
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
   211
name is passed the value "Guido". 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   214
  welcome(name="Guido", greet="Hey! ")
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 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
   217
order. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   218
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", "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
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
   224
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
   225
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   226
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
   227
arguments. 
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
{{{ switch to a slide showing variety of functions with uses }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   231
.. #[[Anoop: slide missing]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   232
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
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
   234
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
   235
"Batteries included" language, for the huge library that comes along
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
with it. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   238
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   239
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
  Math functions - abs, sin, ....
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   241
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   242
.. #[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
   243
..  welcome.] 
217
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
{{{ switch to slide showing classes of functions in pylab, scipy }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   246
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   247
.. #[[Anoop: slide missing]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   248
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   249
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
   250
``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
   251
purposes. 
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
  pylab
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   255
    plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   256
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
  scipy (modules)
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   258
    fftpack, stats, linalg, ndimage, signal, optimize, integrate
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   259
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   260
{{{ Show summary slide }}}
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   261
367
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   262
.. #[[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
   263
   library]]
2ca9b21ed8f9 reviewed advanced features of functions script.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 272
diff changeset
   264
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   265
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
   266
learnt how to use functions with default values and keyword
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
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
   268
Python standard library and the Scientific Computing related
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   269
packages. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   270
240
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   271
{{{ 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
   272
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   273
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
   274
f4c36aa38a84 Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   275
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
   276
Thank you!