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