SEESenv/web/html/ch5func.html
author amit@thunder
Thu, 25 Feb 2010 00:45:20 +0530
changeset 28 514098969b11
parent 27 cb14131583c6
permissions -rw-r--r--
Testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
<html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     2
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. func</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     3
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch5func">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     4
<div class="titlepage"></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     5
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     6
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     7
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     8
<dt><span class="article"><a href="#id2803272">Functional Approach</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     9
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    10
<dt><span class="section"><a href="#id2854310">1. Function scope</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    11
<dt><span class="section"><a href="#id2854368">2. Default Arguments</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    12
<dt><span class="section"><a href="#id2854408">3. Keyword Arguments</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    13
<dt><span class="section"><a href="#id2854524">4. Parameter Packing and Unpacking</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    14
<dt><span class="section"><a href="#id2854624">5. Nested Functions and Scopes</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    15
<dt><span class="section"><a href="#id2854673">6. map, reduce and filter functions</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    16
<dd><dl><dt><span class="section"><a href="#id2854863">6.1. List Comprehensions</a></span></dt></dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    17
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    18
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    19
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    20
<div class="article" title="Functional Approach">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    21
<div class="titlepage">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    22
<div><div><h2 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    23
<a name="id2803272"></a>Functional Approach</h2></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    24
<hr />
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    25
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    26
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    27
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    28
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    29
<dt><span class="section"><a href="#id2854310">1. Function scope</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    30
<dt><span class="section"><a href="#id2854368">2. Default Arguments</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    31
<dt><span class="section"><a href="#id2854408">3. Keyword Arguments</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    32
<dt><span class="section"><a href="#id2854524">4. Parameter Packing and Unpacking</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    33
<dt><span class="section"><a href="#id2854624">5. Nested Functions and Scopes</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    34
<dt><span class="section"><a href="#id2854673">6. map, reduce and filter functions</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    35
<dd><dl><dt><span class="section"><a href="#id2854863">6.1. List Comprehensions</a></span></dt></dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    36
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    37
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    38
<p id="ch5func_1"><span class="emphasis"><em>Functions</em></span> allow us to enclose a set of statements and call the function again
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    39
and again instead of repeating the group of statements everytime. Functions also
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    40
allow us to isolate a piece of code from all the other code and provides the
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    41
convenience of not polluting the global variables.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    42
<p id="ch5func_2"><span class="emphasis"><em>Function</em></span> in python is defined with the keyword <span class="strong"><strong>def</strong></span> followed by the name
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    43
of the function, in turn followed by a pair of parenthesis which encloses the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    44
list of parameters to the function. The definition line ends with a ':'. The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    45
definition line is followed by the body of the function intended by one block.
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    46
The <span class="emphasis"><em>Function</em></span> must return a value:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    47
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    48
def factorial(n):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    49
  fact = 1
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    50
  for i in range(2, n):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    51
    fact *= i
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    52
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    53
  return fact</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    54
<p id="ch5func_3">The code snippet above defines a function with the name factorial, takes the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    55
number for which the factorial must be computed, computes the factorial and
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    56
returns the value.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    57
<p id="ch5func_4">A <span class="emphasis"><em>Function</em></span> once defined can be used or called anywhere else in the program. We
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    58
call a fucntion with its name followed by a pair of parenthesis which encloses
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    59
the arguments to the function.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    60
<p id="ch5func_5">The value that function returns can be assigned to a variable. Let's call the
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    61
above function and store the factorial in a variable:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    62
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    63
fact5 = factorial(5)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    64
<p id="ch5func_6">The value of fact5 will now be 120, which is the factorial of 5. Note that we
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    65
passed 5 as the argument to the function.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    66
<p id="ch5func_7">It may be necessary to document what the function does, for each of the function
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    67
to help the person who reads our code to understand it better. In order to do
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    68
this Python allows the first line of the function body to be a string. This
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    69
string is called as <span class="emphasis"><em>Documentation String</em></span> or <span class="emphasis"><em>docstring</em></span>. <span class="emphasis"><em>docstrings</em></span> prove
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    70
to be very handy since there are number of tools which can pull out all the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    71
docstrings from Python functions and generate the documentation automatically
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    72
from it. <span class="emphasis"><em>docstrings</em></span> for functions can be written as follows:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    73
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    74
def factorial(n):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    75
  'Returns the factorial for the number n.'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    76
  fact = 1
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    77
  for i in range(2, n):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    78
    fact *= i
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    79
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    80
  return fact</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    81
<p id="ch5func_8">An important point to note at this point is that, a function can return any
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    82
Python value or a Python object, which also includes a <span class="emphasis"><em>Tuple</em></span>. A <span class="emphasis"><em>Tuple</em></span> is
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    83
just a collection of values and those values themselves can be of any other
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    84
valid Python datatypes, including <span class="emphasis"><em>Lists</em></span>, <span class="emphasis"><em>Tuples</em></span>, <span class="emphasis"><em>Dictionaries</em></span> among other
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    85
things. So effectively, if a function can return a tuple, it can return any
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    86
number of values through a tuple</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    87
<p id="ch5func_9">Let us write a small function to swap two values:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    88
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    89
def swap(a, b):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    90
  return b, a
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    91
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    92
c, d = swap(a, b)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    93
<div class="section" title="1.Function scope">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    94
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    95
<a name="id2854310"></a>1.Function scope</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    96
<p id="ch5func_a">The variables used inside the function are confined to the function's scope
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    97
and doesn't pollute the variables of the same name outside the scope of the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    98
function. Also the arguments passed to the function are passed by-value if
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    99
it is of basic Python data type:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   100
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   101
def cant_change(n):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   102
  n = 10
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   103
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   104
n = 5
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   105
cant_change(n)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   106
<p id="ch5func_b">Upon running this code, what do you think would have happened to value of n
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   107
which was assigned 5 before the function call? If you have already tried out
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   108
that snippet on the interpreter you already know that the value of n is not
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   109
changed. This is true of any immutable types of Python like <span class="emphasis"><em>Numbers</em></span>, <span class="emphasis"><em>Strings</em></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   110
and <span class="emphasis"><em>Tuples</em></span>. But when you pass mutable objects like <span class="emphasis"><em>Lists</em></span> and <span class="emphasis"><em>Dictionaries</em></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   111
the values are manipulated even outside the function:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   112
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   113
&gt;&gt;&gt; def can_change(n):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   114
...   n[1] = James
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   115
...
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   116
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   117
&gt;&gt;&gt; name = ['Mr.', 'Steve', 'Gosling']
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   118
&gt;&gt;&gt; can_change(name)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   119
&gt;&gt;&gt; name
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   120
['Mr.', 'James', 'Gosling']</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   121
<p id="ch5func_c">If nothing is returned by the function explicitly, Python takes care to return
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   122
None when the funnction is called.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   123
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   124
<div class="section" title="2.Default Arguments">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   125
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   126
<a name="id2854368"></a>2.Default Arguments</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   127
<p id="ch5func_d">There may be situations where we need to allow the functions to take the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   128
arguments optionally. Python allows us to define function this way by providing
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   129
a facility called <span class="emphasis"><em>Default Arguments</em></span>. For example, we need to write a function
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   130
that returns a list of fibonacci numbers. Since our function cannot generate an
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   131
infinite list of fibonacci numbers, we need to specify the number of elements
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   132
that the fibonacci sequence must contain. Suppose, additionally, we want to the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   133
function to return 10 numbers in the sequence if no option is specified we can
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   134
define the function as follows:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   135
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   136
def fib(n=10):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   137
  fib_list = [0, 1]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   138
  for i in range(n - 2):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   139
    next = fib_list[-2] + fib_list[-1]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   140
    fib_list.append(next)
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   141
  return fib_list</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   142
<p id="ch5func_e">When we call this function, we can optionally specify the value for the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   143
parameter n, during the call as an argument. Calling with no argument and
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   144
argument with n=5 returns the following fibonacci sequences:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   145
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   146
fib()
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   147
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   148
fib(5)
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   149
[0, 1, 1, 2, 3]</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   150
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   151
<div class="section" title="3.Keyword Arguments">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   152
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   153
<a name="id2854408"></a>3.Keyword Arguments</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   154
<p id="ch5func_f">When a function takes a large number of arguments, it may be difficult to
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   155
remember the order of the parameters in the function definition or it may
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   156
be necessary to pass values to only certain parameters since others take
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   157
the default value. In either of these cases, Python provides the facility
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   158
of passing arguments by specifying the name of the parameter as defined in
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   159
the function definition. This is known as <span class="emphasis"><em>Keyword Arguments</em></span>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   160
<p id="ch5func_10">In a function call, <span class="emphasis"><em>Keyword arguments</em></span> can be used for each argument, in the
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   161
following fashion:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   162
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   163
argument_name=argument_value
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   164
Also denoted as: keyword=argument
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   165
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   166
def wish(name='World', greetings='Hello'):
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   167
  print "%s, %s!" % (greetings, name)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   168
<p id="ch5func_11">This function can be called in one of the following ways. It is important to
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   169
note that no restriction is imposed in the order in which <span class="emphasis"><em>Keyword arguments</em></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   170
can be specified. Also note, that we have combined <span class="emphasis"><em>Keyword arguments</em></span> with
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   171
<span class="emphasis"><em>Default arguments</em></span> in this example, however it is not necessary:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   172
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   173
wish(name='Guido', greetings='Hey')
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   174
wish(greetings='Hey', name='Guido')</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   175
<p id="ch5func_12">Calling functions by specifying arguments in the order of parameters specified
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   176
in the function definition is called as <span class="emphasis"><em>Positional arguments</em></span>, as opposed to
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   177
<span class="emphasis"><em>Keyword arguments</em></span>. It is possible to use both <span class="emphasis"><em>Positional arguments</em></span> and
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   178
<span class="emphasis"><em>Keyword arguments</em></span> in a single function call. But Python doesn't allow us to
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   179
bungle up both of them. The arguments to the function, in the call, must always
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   180
start with <span class="emphasis"><em>Positional arguments</em></span> which is in turn followed by <span class="emphasis"><em>Keyword
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   181
arguments</em></span>:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   182
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   183
def my_func(x, y, z, u, v, w):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   184
  # initialize variables.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   185
  ...
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   186
  # do some stuff
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   187
  ...
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   188
  # return the value</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   189
<p id="ch5func_13">It is valid to call the above functions in the following ways:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   190
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   191
my_func(10, 20, 30, u=1.0, v=2.0, w=3.0)
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   192
my_func(10, 20, 30, 1.0, 2.0, w=3.0)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   193
my_func(10, 20, z=30, u=1.0, v=2.0, w=3.0)
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   194
my_func(x=10, y=20, z=30, u=1.0, v=2.0, w=3.0)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   195
<p id="ch5func_14">Following lists some of the invalid calls:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   196
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   197
my_func(10, 20, z=30, 1.0, 2.0, 3.0)
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   198
my_func(x=10, 20, z=30, 1.0, 2.0, 3.0)
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   199
my_func(x=10, y=20, z=30, u=1.0, v=2.0, 3.0)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   200
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   201
<div class="section" title="4.Parameter Packing and Unpacking">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   202
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   203
<a name="id2854524"></a>4.Parameter Packing and Unpacking</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   204
<p id="ch5func_15">The positional arguments passed to a function can be collected in a tuple
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   205
parameter and keyword arguments can be collected in a dictionary. Since keyword
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   206
arguments must always be the last set of arguments passed to a function, the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   207
keyword dictionary parameter must be the last parameter. The function definition
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   208
must include a list explicit parameters, followed by tuple paramter collecting
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   209
parameter, whose name is preceded by a <span class="strong"><strong>*</strong></span>, for collecting positional
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   210
parameters, in turn followed by the dictionary collecting parameter, whose name
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   211
is preceded by a <span class="strong"><strong>**</strong></span></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   212
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   213
def print_report(title, *args, **name):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   214
  """Structure of *args*
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   215
  (age, email-id)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   216
  Structure of *name*
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   217
  {
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   218
      'first': First Name
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   219
      'middle': Middle Name
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   220
      'last': Last Name
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   221
  }
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   222
  """
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   223
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   224
  print "Title: %s" % (title)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   225
  print "Full name: %(first)s %(middle)s %(last)s" % name
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   226
  print "Age: %d nEmail-ID: %s" % args</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   227
<p id="ch5func_16">The above function can be called as. Note, the order of keyword parameters can
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   228
be interchanged:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   229
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   230
&gt;&gt;&gt; print_report('Employee Report', 29, 'johny@example.com', first='Johny',
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   231
                 last='Charles', middle='Douglas')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   232
Title: Employee Report
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   233
Full name: Johny Douglas Charles
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   234
Age: 29
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   235
Email-ID: johny@example.com</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   236
<p id="ch5func_17">The reverse of this can also be achieved by using a very identical syntax while
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   237
calling the function. A tuple or a dictionary can be passed as arguments in
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   238
place of a list of <span class="emphasis"><em>Positional arguments</em></span> or <span class="emphasis"><em>Keyword arguments</em></span> respectively
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   239
using <span class="strong"><strong>*</strong></span> or <span class="strong"><strong>**</strong></span></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   240
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   241
def print_report(title, age, email, first, middle, last):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   242
  print "Title: %s" % (title)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   243
  print "Full name: %s %s %s" % (first, middle, last)
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 24
diff changeset
   244
  print "Age: %d nEmail-ID: %s" % (age, email)
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   245
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   246
&gt;&gt;&gt; args = (29, 'johny@example.com')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   247
&gt;&gt;&gt; name = {
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   248
        'first': 'Johny',
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   249
        'middle': 'Charles',
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   250
        'last': 'Douglas'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   251
        }
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   252
&gt;&gt;&gt; print_report('Employee Report', *args, **name)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   253
Title: Employee Report
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   254
Full name: Johny Charles Douglas
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   255
Age: 29
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   256
Email-ID: johny@example.com</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   257
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   258
<div class="section" title="5.Nested Functions and Scopes">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   259
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   260
<a name="id2854624"></a>5.Nested Functions and Scopes</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   261
<p id="ch5func_18">Python allows nesting one function inside another. This style of programming
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   262
turns out to be extremely flexible and powerful features when we use <span class="emphasis"><em>Python
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   263
decorators</em></span>. We will not talk about decorators is beyond the scope of this
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   264
course. If you are interested in knowing more about <span class="emphasis"><em>decorator programming</em></span> in
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   265
Python you are suggested to read:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   266
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   267
<div class="titlepage"><hr /></div>http://avinashv.net/2008/04/python-decorators-syntactic-sugar/</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   268
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   269
<div class="titlepage"><hr /></div>http://personalpages.tds.net/~kent37/kk/00001.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   270
<p id="ch5func_19">However, the following is an example for nested functions in Python:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   271
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   272
def outer():
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   273
  print "Outer..."
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   274
  def inner():
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   275
    print "Inner..."
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   276
  print "Outer..."
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   277
  inner()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   278
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   279
&gt;&gt;&gt; outer()</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   280
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   281
<div class="section" title="6.map, reduce and filter functions">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   282
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   283
<a name="id2854673"></a>6.map, reduce and filter functions</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   284
<p id="ch5func_1a">Python provides several built-in functions for convenience. The <span class="strong"><strong>map()</strong></span>,
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   285
<span class="strong"><strong>reduce()</strong></span> and <span class="strong"><strong>filter()</strong></span> functions prove to be very useful with sequences like
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   286
<span class="emphasis"><em>Lists</em></span>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   287
<p id="ch5func_1b">The <span class="strong"><strong>map</strong></span> (<span class="emphasis"><em>function</em></span>, <span class="emphasis"><em>sequence</em></span>) function takes two arguments: <span class="emphasis"><em>function</em></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   288
and a <span class="emphasis"><em>sequence</em></span> argument. The <span class="emphasis"><em>function</em></span> argument must be the name of the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   289
function which in turn takes a single argument, the individual element of the
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   290
<span class="emphasis"><em>sequence</em></span>. The <span class="strong"><strong>map</strong></span> function calls <span class="emphasis"><em>function(item)</em></span>, for each item in the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   291
sequence and returns a list of values, where each value is the value returned
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   292
by each call to <span class="emphasis"><em>function(item)</em></span>. <span class="strong"><strong>map()</strong></span> function allows to pass more than
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   293
one sequence. In this case, the first argument, <span class="emphasis"><em>function</em></span> must take as many
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   294
arguments as the number of sequences passed. This function is called with each
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   295
corresponding element in the each of the sequences, or <span class="strong"><strong>None</strong></span> if one of the
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   296
sequence is exhausted:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   297
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   298
def square(x):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   299
  return x*x
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   300
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   301
&gt;&gt;&gt; map(square, [1, 2, 3, 4])
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   302
[1, 4, 9, 16]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   303
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   304
def mul(x, y):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   305
  return x*y
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   306
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   307
&gt;&gt;&gt; map(mul, [1, 2, 3, 4], [6, 7, 8, 9])</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   308
<p id="ch5func_1c">The <span class="strong"><strong>filter</strong></span> (<span class="emphasis"><em>function</em></span>, <span class="emphasis"><em>sequence</em></span>) function takes two arguments, similar to
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   309
the <span class="strong"><strong>map()</strong></span> function. The <span class="strong"><strong>filter</strong></span> function calls <span class="emphasis"><em>function(item)</em></span>, for each
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   310
item in the sequence and returns all the elements in the sequence for which
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   311
<span class="emphasis"><em>function(item)</em></span> returned True:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   312
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   313
def even(x):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   314
  if x % 2:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   315
    return True
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   316
  else:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   317
    return False
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   318
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   319
&gt;&gt;&gt; filter(even, range(1, 10))
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   320
[1, 3, 5, 7, 9]</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   321
<p id="ch5func_1d">The <span class="strong"><strong>reduce</strong></span> (<span class="emphasis"><em>function</em></span>, <span class="emphasis"><em>sequence</em></span>) function takes two arguments, similar to
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   322
<span class="strong"><strong>map</strong></span> function, however multiple sequences are not allowed. The <span class="strong"><strong>reduce</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   323
function calls <span class="emphasis"><em>function</em></span> with first two consecutive elements in the sequence,
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   324
obtains the result, calls <span class="emphasis"><em>function</em></span> with the result and the subsequent element
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   325
in the sequence and so on until the end of the list and returns the final result:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   326
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   327
def mul(x, y):
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   328
  return x*y
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   329
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   330
&gt;&gt;&gt; reduce(mul, [1, 2, 3, 4])
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   331
24</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   332
<div class="section" title="6.1.List Comprehensions">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   333
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   334
<a name="id2854863"></a>6.1.List Comprehensions</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   335
<p id="ch5func_1e">List Comprehension is a convenvience utility provided by Python. It is a
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   336
syntatic sugar to create <span class="emphasis"><em>Lists</em></span>. Using <span class="emphasis"><em>List Comprehensions</em></span> one can create
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   337
<span class="emphasis"><em>Lists</em></span> from other type of sequential data structures or other <span class="emphasis"><em>Lists</em></span> itself.
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   338
The syntax of <span class="emphasis"><em>List Comprehensions</em></span> consists of a square brackets to indicate
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   339
the result is a <span class="emphasis"><em>List</em></span> within which we include at least one <span class="strong"><strong>for</strong></span> clause and
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   340
multiple <span class="strong"><strong>if</strong></span> clauses. It will be more clear with an example:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   341
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   342
&gt;&gt;&gt; num = [1, 2, 3]
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   343
&gt;&gt;&gt; sq = [x*x for x in num]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   344
&gt;&gt;&gt; sq
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   345
[1, 4, 9]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   346
&gt;&gt;&gt; all_num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   347
&gt;&gt;&gt; even = [x for x in all_num if x%2 == 0]</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   348
<p id="ch5func_1f">The syntax used here is very clear from the way it is written. It can be
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   349
translated into english as, "for each element x in the list all_num,
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   350
if remainder of x divided by 2 is 0, add x to the list."</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   351
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   352
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   353
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   354
</div></body>
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   355
</html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   356