SEESenv/web/html/ch5func.html
author amit@thunder
Mon, 22 Feb 2010 22:38:51 +0530
changeset 24 10074d1357ff
parent 16 2e3d2cadbcb6
child 25 ed38dd9bdb50
permissions -rw-r--r--
Some changes after the horrible problem of writing the same ch9 on all the html pages
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>
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
     2
 <head>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
     3
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
     4
  <title>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
     5
   Chapter. func
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
     6
  </title>
15
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
     7
  <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
     8
  <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
     9
  <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
    10
  <script type="text/javascript" src="/review/support/jquery-min.js">
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
    11
  </script>
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
    12
  <script type="text/javascript" src="/review/support/form.js">
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
    13
  </script>
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
    14
  <script type="text/javascript" src="/review/support/hsbook.js">
f2383fee6584 Testing
amit@thunder
parents: 6
diff changeset
    15
  </script>
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    16
 </head>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    17
 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
16
2e3d2cadbcb6 Testing
amit@thunder
parents: 15
diff changeset
    18
  <div class="chapter" id="ch5func">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    19
   <div class="titlepage">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    20
   </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    21
   <div class="toc">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    22
    <p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    23
     <b>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    24
      Table of Contents
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    25
     </b>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    26
    </p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    27
    <dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    28
     <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    29
      <span class="article">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    30
       <a href="#id2813580">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    31
        Functional Approach
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    32
       </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    33
      </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    34
     </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    35
     <dd>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    36
      <dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    37
       <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    38
        <span class="section">
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
         <a href="#id2864618">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    40
          1. Function scope
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    41
         </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    42
        </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    43
       </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    44
       <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    45
        <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    46
         <a href="#id2864676">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    47
          2. Default Arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    48
         </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    49
        </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    50
       </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    51
       <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    52
        <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    53
         <a href="#id2864716">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    54
          3. Keyword Arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    55
         </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    56
        </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    57
       </dt>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    58
       <dt>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    59
        <span class="section">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    60
         <a href="#id2864832">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    61
          4. Parameter Packing and Unpacking
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    62
         </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    63
        </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    64
       </dt>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    65
       <dt>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    66
        <span class="section">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    67
         <a href="#id2864932">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    68
          5. Nested Functions and Scopes
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    69
         </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    70
        </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    71
       </dt>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    72
       <dt>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    73
        <span class="section">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    74
         <a href="#id2864984">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    75
          6. map, reduce and filter functions
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    76
         </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    77
        </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    78
       </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    79
       <dd>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    80
        <dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    81
         <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    82
          <span class="section">
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
           <a href="#id2865174">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    84
            6.1. List Comprehensions
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    85
           </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    86
          </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    87
         </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    88
        </dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    89
       </dd>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    90
      </dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    91
     </dd>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    92
    </dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    93
   </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    94
   <div class="article" title="Functional Approach">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    95
    <div class="titlepage">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    96
     <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
    97
      <div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    98
       <h2 class="title">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    99
        <a name="id2813580">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   100
        </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   101
        Functional Approach
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   102
       </h2>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   103
      </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   104
     </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   105
     <hr />
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   106
    </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   107
    <div class="toc">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   108
     <p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   109
      <b>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   110
       Table of Contents
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   111
      </b>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   112
     </p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   113
     <dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   114
      <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   115
       <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   116
        <a href="#id2864618">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   117
         1. Function scope
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   118
        </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   119
       </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   120
      </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   121
      <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   122
       <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   123
        <a href="#id2864676">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   124
         2. Default Arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   125
        </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   126
       </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   127
      </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   128
      <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   129
       <span class="section">
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
        <a href="#id2864716">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   131
         3. Keyword Arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   132
        </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   133
       </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   134
      </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   135
      <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   136
       <span class="section">
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
        <a href="#id2864832">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   138
         4. Parameter Packing and Unpacking
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   139
        </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   140
       </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   141
      </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   142
      <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   143
       <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   144
        <a href="#id2864932">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   145
         5. Nested Functions and Scopes
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   146
        </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   147
       </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   148
      </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   149
      <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   150
       <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   151
        <a href="#id2864984">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   152
         6. map, reduce and filter functions
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   153
        </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   154
       </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   155
      </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   156
      <dd>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   157
       <dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   158
        <dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   159
         <span class="section">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   160
          <a href="#id2865174">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   161
           6.1. List Comprehensions
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   162
          </a>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   163
         </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   164
        </dt>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   165
       </dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   166
      </dd>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   167
     </dl>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   168
    </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   169
    <p id="ch5func_1">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   170
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   171
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   172
       Functions
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   173
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   174
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   175
     allow us to enclose a set of statements and call the function again
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   176
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
   177
allow us to isolate a piece of code from all the other code and provides the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   178
convenience of not polluting the global variables.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   179
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   180
    <p id="ch5func_2">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   181
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   182
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   183
       Function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   184
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   185
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   186
     in python is defined with the keyword
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   187
     <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   188
      <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   189
       def
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   190
      </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   191
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   192
     followed by the name
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   193
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
   194
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
   195
definition line is followed by the body of the function intended by one block.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   196
The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   197
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   198
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   199
       Function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   200
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   201
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   202
     must return a value:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   203
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   204
    <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   205
     def factorial(n):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   206
  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
   207
  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
   208
    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
   209
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   210
  return fact
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   211
    </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   212
    <p id="ch5func_3">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   213
     The code snippet above defines a function with the name factorial, takes the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   214
number for which the factorial must be computed, computes the factorial and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   215
returns the value.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   216
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   217
    <p id="ch5func_4">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   218
     A
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   219
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   220
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   221
       Function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   222
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   223
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   224
     once defined can be used or called anywhere else in the program. We
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   225
call a fucntion with its name followed by a pair of parenthesis which encloses
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   226
the arguments to the function.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   227
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   228
    <p id="ch5func_5">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   229
     The value that function returns can be assigned to a variable. Let's call the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   230
above function and store the factorial in a variable:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   231
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   232
    <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   233
     fact5 = factorial(5)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   234
    </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   235
    <p id="ch5func_6">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   236
     The value of fact5 will now be 120, which is the factorial of 5. Note that we
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   237
passed 5 as the argument to the function.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   238
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   239
    <p id="ch5func_7">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   240
     It may be necessary to document what the function does, for each of the function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   241
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
   242
this Python allows the first line of the function body to be a string. This
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   243
string is called as
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   244
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   245
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   246
       Documentation String
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   247
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   248
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   249
     or
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   250
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   251
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   252
       docstring
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   253
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   254
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   255
     .
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   256
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   257
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   258
       docstrings
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   259
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   260
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   261
     prove
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   262
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
   263
docstrings from Python functions and generate the documentation automatically
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   264
from it.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   265
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   266
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   267
       docstrings
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   268
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   269
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   270
     for functions can be written as follows:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   271
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   272
    <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   273
     def factorial(n):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   274
  '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
   275
  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
   276
  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
   277
    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
   278
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   279
  return fact
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   280
    </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   281
    <p id="ch5func_8">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   282
     An important point to note at this point is that, a function can return any
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   283
Python value or a Python object, which also includes a
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   284
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   285
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   286
       Tuple
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   287
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   288
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   289
     . A
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   290
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   291
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   292
       Tuple
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   293
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   294
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   295
     is
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   296
just a collection of values and those values themselves can be of any other
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   297
valid Python datatypes, including
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   298
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   299
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   300
       Lists
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   301
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   302
     </span>
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
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   305
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   306
       Tuples
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   307
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   308
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   309
     ,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   310
     <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   311
      <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   312
       Dictionaries
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   313
      </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   314
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   315
     among other
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   316
things. So effectively, if a function can return a tuple, it can return any
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   317
number of values through a tuple
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   318
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   319
    <p id="ch5func_9">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   320
     Let us write a small function to swap two values:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   321
    </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   322
    <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   323
     def swap(a, b):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   324
  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
   325
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   326
c, d = swap(a, b)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   327
    </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   328
    <div class="section" title="1.Function scope">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   329
     <div class="titlepage">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   330
      <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   331
       <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   332
        <h2 class="title" style="clear: both">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   333
         <a name="id2864618">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   334
         </a>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   335
         1.Function scope
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   336
        </h2>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   337
       </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   338
      </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   339
     </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   340
     <p id="ch5func_a">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   341
      The variables used inside the function are confined to the function's scope
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   342
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
   343
function. Also the arguments passed to the function are passed by-value if
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   344
it is of basic Python data type:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   345
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   346
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   347
      def cant_change(n):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   348
  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
   349
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   350
n = 5
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   351
cant_change(n)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   352
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   353
     <p id="ch5func_b">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   354
      Upon running this code, what do you think would have happened to value of n
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   355
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
   356
that snippet on the interpreter you already know that the value of n is not
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   357
changed. This is true of any immutable types of Python like
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   358
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   359
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   360
        Numbers
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   361
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   362
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   363
      ,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   364
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   365
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   366
        Strings
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   367
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   368
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   369
      and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   370
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   371
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   372
        Tuples
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   373
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   374
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   375
      . But when you pass mutable objects like
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   376
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   377
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   378
        Lists
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   379
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   380
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   381
      and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   382
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   383
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   384
        Dictionaries
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   385
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   386
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   387
      the values are manipulated even outside the function:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   388
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   389
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   390
      &gt;&gt;&gt; def can_change(n):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   391
...   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
   392
...
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   393
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   394
&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
   395
&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
   396
&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
   397
['Mr.', 'James', 'Gosling']
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   398
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   399
     <p id="ch5func_c">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   400
      If nothing is returned by the function explicitly, Python takes care to return
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   401
None when the funnction is called.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   402
     </p>
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   403
    </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   404
    <div class="section" title="2.Default Arguments">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   405
     <div class="titlepage">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   406
      <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   407
       <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   408
        <h2 class="title" style="clear: both">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   409
         <a name="id2864676">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   410
         </a>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   411
         2.Default Arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   412
        </h2>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   413
       </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   414
      </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   415
     </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   416
     <p id="ch5func_d">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   417
      There may be situations where we need to allow the functions to take the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   418
arguments optionally. Python allows us to define function this way by providing
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   419
a facility called
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   420
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   421
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   422
        Default Arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   423
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   424
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   425
      . For example, we need to write a function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   426
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
   427
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
   428
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
   429
function to return 10 numbers in the sequence if no option is specified we can
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   430
define the function as follows:
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   431
     </p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   432
     <pre class="programlisting">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   433
      def fib(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
   434
  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
   435
  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
   436
    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
   437
    fib_list.append(next)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   438
  return fib_list
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   439
     </pre>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   440
     <p id="ch5func_e">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   441
      When we call this function, we can optionally specify the value for the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   442
parameter n, during the call as an argument. Calling with no argument and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   443
argument with n=5 returns the following fibonacci sequences:
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   444
     </p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   445
     <pre class="programlisting">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   446
      fib()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   447
[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
   448
fib(5)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   449
[0, 1, 1, 2, 3]
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   450
     </pre>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   451
    </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   452
    <div class="section" title="3.Keyword Arguments">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   453
     <div class="titlepage">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   454
      <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   455
       <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   456
        <h2 class="title" style="clear: both">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   457
         <a name="id2864716">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   458
         </a>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   459
         3.Keyword Arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   460
        </h2>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   461
       </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   462
      </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   463
     </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   464
     <p id="ch5func_f">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   465
      When a function takes a large number of arguments, it may be difficult to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   466
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
   467
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
   468
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
   469
of passing arguments by specifying the name of the parameter as defined in
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   470
the function definition. This is known as
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   471
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   472
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   473
        Keyword Arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   474
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   475
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   476
      .
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   477
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   478
     <p id="ch5func_10">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   479
      In a function call,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   480
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   481
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   482
        Keyword arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   483
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   484
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   485
      can be used for each argument, in the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   486
following fashion:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   487
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   488
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   489
      argument_name=argument_value
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   490
Also denoted as: keyword=argument
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   491
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   492
def wish(name='World', greetings='Hello'):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   493
  print "%s, %s!" % (greetings, name)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   494
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   495
     <p id="ch5func_11">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   496
      This function can be called in one of the following ways. It is important to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   497
note that no restriction is imposed in the order in which
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   498
      <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   499
       <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   500
        Keyword arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   501
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   502
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   503
      can be specified. Also note, that we have combined
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   504
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   505
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   506
        Keyword arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   507
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   508
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   509
      with
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   510
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   511
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   512
        Default arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   513
       </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   514
      </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   515
      in this example, however it is not necessary:
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   516
     </p>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   517
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   518
      wish(name='Guido', greetings='Hey')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   519
wish(greetings='Hey', name='Guido')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   520
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   521
     <p id="ch5func_12">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   522
      Calling functions by specifying arguments in the order of parameters specified
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   523
in the function definition is called as
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   524
      <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   525
       <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   526
        Positional arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   527
       </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   528
      </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   529
      , as opposed to
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   530
      <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   531
       <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   532
        Keyword arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   533
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   534
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   535
      . It is possible to use both
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   536
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   537
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   538
        Positional arguments
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   539
       </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   540
      </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   541
      and
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   542
      <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   543
       <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   544
        Keyword arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   545
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   546
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   547
      in a single function call. But Python doesn't allow us to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   548
bungle up both of them. The arguments to the function, in the call, must always
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   549
start with
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   550
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   551
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   552
        Positional arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   553
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   554
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   555
      which is in turn followed by
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   556
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   557
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   558
        Keyword
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   559
arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   560
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   561
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   562
      :
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   563
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   564
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   565
      def my_func(x, y, z, u, v, w):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   566
  # 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
   567
  ...
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   568
  # 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
   569
  ...
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   570
  # return the value
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   571
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   572
     <p id="ch5func_13">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   573
      It is valid to call the above functions in the following ways:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   574
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   575
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   576
      my_func(10, 20, 30, u=1.0, v=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
   577
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
   578
my_func(10, 20, z=30, u=1.0, v=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
   579
my_func(x=10, y=20, z=30, u=1.0, v=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
   580
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   581
     <p id="ch5func_14">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   582
      Following lists some of the invalid calls:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   583
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   584
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   585
      my_func(10, 20, z=30, 1.0, 2.0, 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
   586
my_func(x=10, 20, z=30, 1.0, 2.0, 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
   587
my_func(x=10, y=20, z=30, u=1.0, v=2.0, 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
   588
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   589
    </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   590
    <div class="section" title="4.Parameter Packing and Unpacking">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   591
     <div class="titlepage">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   592
      <div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   593
       <div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   594
        <h2 class="title" style="clear: both">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   595
         <a name="id2864832">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   596
         </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   597
         4.Parameter Packing and Unpacking
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   598
        </h2>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   599
       </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   600
      </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   601
     </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   602
     <p id="ch5func_15">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   603
      The positional arguments passed to a function can be collected in a tuple
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   604
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
   605
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
   606
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
   607
must include a list explicit parameters, followed by tuple paramter collecting
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   608
parameter, whose name is preceded by a
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   609
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   610
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   611
        *
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   612
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   613
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   614
      , for collecting positional
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   615
parameters, in turn followed by the dictionary collecting parameter, whose name
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   616
is preceded by a
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   617
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   618
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   619
        **
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   620
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   621
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   622
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   623
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   624
      def print_report(title, *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
   625
  """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
   626
  (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
   627
  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
   628
  {
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   629
      '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
   630
      '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
   631
      '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
   632
  }
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   633
  """
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   634
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   635
  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
   636
  print "Full name: %(first)s %(middle)s %(last)s" % name
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   637
  print "Age: %d\nEmail-ID: %s" % args
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   638
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   639
     <p id="ch5func_16">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   640
      The above function can be called as. Note, the order of keyword parameters can
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   641
be interchanged:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   642
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   643
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   644
      &gt;&gt;&gt; print_report('Employee Report', 29, 'johny@example.com', 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
   645
                 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
   646
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
   647
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
   648
Age: 29
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   649
Email-ID: 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
   650
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   651
     <p id="ch5func_17">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   652
      The reverse of this can also be achieved by using a very identical syntax while
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   653
calling the function. A tuple or a dictionary can be passed as arguments in
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   654
place of a list of
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   655
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   656
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   657
        Positional arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   658
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   659
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   660
      or
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   661
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   662
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   663
        Keyword arguments
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   664
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   665
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   666
      respectively
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   667
using
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   668
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   669
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   670
        *
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   671
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   672
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   673
      or
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   674
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   675
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   676
        **
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   677
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   678
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   679
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   680
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   681
      def print_report(title, age, email, first, middle, last):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   682
  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
   683
  print "Full name: %s %s %s" % (first, middle, last)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   684
  print "Age: %d\nEmail-ID: %s" % (age, email)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   685
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   686
&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
   687
&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
   688
        '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
   689
        '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
   690
        '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
   691
        }
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   692
&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
   693
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
   694
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
   695
Age: 29
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   696
Email-ID: 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
   697
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   698
    </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   699
    <div class="section" title="5.Nested Functions and Scopes">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   700
     <div class="titlepage">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   701
      <div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   702
       <div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   703
        <h2 class="title" style="clear: both">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   704
         <a name="id2864932">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   705
         </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   706
         5.Nested Functions and Scopes
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   707
        </h2>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   708
       </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   709
      </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   710
     </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   711
     <p id="ch5func_18">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   712
      Python allows nesting one function inside another. This style of programming
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   713
turns out to be extremely flexible and powerful features when we use
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   714
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   715
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   716
        Python
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   717
decorators
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   718
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   719
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   720
      . We will not talk about decorators is beyond the scope of this
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   721
course. If you are interested in knowing more about
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   722
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   723
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   724
        decorator programming
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   725
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   726
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   727
      in
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   728
Python you are suggested to read:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   729
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   730
     <span style="color: red">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   731
      &lt;line_block&gt;
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   732
      <span style="color: red">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   733
       &lt;line&gt;
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   734
       <div class="reference">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   735
        <div class="titlepage">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   736
         <hr />
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   737
        </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   738
        http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   739
       </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   740
       &lt;/line&gt;
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   741
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   742
      <span style="color: red">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   743
       &lt;line&gt;
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   744
       <div class="reference">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   745
        <div class="titlepage">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   746
         <hr />
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   747
        </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   748
        http://personalpages.tds.net/~kent37/kk/00001.html
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   749
       </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   750
       &lt;/line&gt;
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   751
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   752
      &lt;/line_block&gt;
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   753
     </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   754
     <p id="ch5func_19">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   755
      However, the following is an example for nested functions in Python:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   756
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   757
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   758
      def outer():
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   759
  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
   760
  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
   761
    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
   762
  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
   763
  inner()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   764
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   765
&gt;&gt;&gt; outer()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   766
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   767
    </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   768
    <div class="section" title="6.map, reduce and filter functions">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   769
     <div class="titlepage">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   770
      <div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   771
       <div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   772
        <h2 class="title" style="clear: both">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   773
         <a name="id2864984">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   774
         </a>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   775
         6.map, reduce and filter functions
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   776
        </h2>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   777
       </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   778
      </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   779
     </div>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   780
     <p id="ch5func_1a">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   781
      Python provides several built-in functions for convenience. The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   782
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   783
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   784
        map()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   785
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   786
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   787
      ,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   788
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   789
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   790
        reduce()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   791
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   792
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   793
      and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   794
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   795
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   796
        filter()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   797
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   798
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   799
      functions prove to be very useful with sequences like
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   800
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   801
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   802
        Lists
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   803
       </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   804
      </span>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   805
      .
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
   806
     </p>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   807
     <p id="ch5func_1b">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   808
      The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   809
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   810
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   811
        map
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   812
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   813
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   814
      (
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   815
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   816
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   817
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   818
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   819
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   820
      ,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   821
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   822
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   823
        sequence
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   824
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   825
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   826
      ) function takes two arguments:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   827
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   828
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   829
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   830
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   831
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   832
      and a
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   833
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   834
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   835
        sequence
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   836
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   837
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   838
      argument. The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   839
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   840
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   841
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   842
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   843
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   844
      argument must be the name 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
   845
function which in turn takes a single argument, the individual element 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
   846
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   847
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   848
        sequence
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   849
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   850
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   851
      . The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   852
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   853
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   854
        map
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   855
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   856
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   857
      function calls
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   858
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   859
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   860
        function(item)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   861
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   862
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   863
      , for each item in the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   864
sequence and returns a list of values, where each value is the value returned
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   865
by each call to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   866
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   867
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   868
        function(item)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   869
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   870
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   871
      .
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   872
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   873
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   874
        map()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   875
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   876
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   877
      function allows to pass more than
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   878
one sequence. In this case, the first argument,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   879
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   880
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   881
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   882
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   883
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   884
      must take as many
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   885
arguments as the number of sequences passed. This function is called with each
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   886
corresponding element in the each of the sequences, or
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   887
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   888
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   889
        None
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   890
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   891
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   892
      if one 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
   893
sequence is exhausted:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   894
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   895
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   896
      def square(x):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   897
  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
   898
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   899
&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
   900
[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
   901
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   902
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
   903
  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
   904
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   905
&gt;&gt;&gt; map(mul, [1, 2, 3, 4], [6, 7, 8, 9])
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   906
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   907
     <p id="ch5func_1c">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   908
      The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   909
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   910
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   911
        filter
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   912
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   913
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   914
      (
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   915
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   916
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   917
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   918
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   919
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   920
      ,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   921
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   922
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   923
        sequence
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   924
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   925
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   926
      ) function takes two arguments, similar to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   927
the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   928
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   929
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   930
        map()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   931
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   932
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   933
      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
   934
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   935
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   936
        filter
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   937
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   938
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   939
      function calls
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   940
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   941
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   942
        function(item)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   943
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   944
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   945
      , for each
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   946
item in the sequence and returns all the elements in the sequence for which
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   947
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   948
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   949
        function(item)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   950
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   951
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   952
      returned True:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   953
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   954
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   955
      def even(x):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   956
  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
   957
    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
   958
  else:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   959
    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
   960
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   961
&gt;&gt;&gt; filter(even, range(1, 10))
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   962
[1, 3, 5, 7, 9]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   963
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   964
     <p id="ch5func_1d">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   965
      The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   966
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   967
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   968
        reduce
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   969
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   970
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   971
      (
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   972
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   973
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   974
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   975
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   976
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   977
      ,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   978
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   979
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   980
        sequence
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   981
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   982
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   983
      ) function takes two arguments, similar to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   984
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   985
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   986
        map
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   987
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   988
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   989
      function, however multiple sequences are not allowed. The
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   990
      <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   991
       <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   992
        reduce
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   993
       </strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   994
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   995
      function calls
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   996
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   997
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   998
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   999
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1000
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1001
      with first two consecutive elements in the sequence,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1002
obtains the result, calls
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1003
      <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1004
       <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1005
        function
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1006
       </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1007
      </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1008
      with the result and the subsequent element
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1009
in the sequence and so on until the end of the list and returns the final result:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1010
     </p>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1011
     <pre class="programlisting">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1012
      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
  1013
  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
  1014
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1015
&gt;&gt;&gt; reduce(mul, [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
  1016
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1017
     </pre>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1018
     <div class="section" title="6.1.List Comprehensions">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1019
      <div class="titlepage">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1020
       <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1021
        <div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1022
         <h3 class="title">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1023
          <a name="id2865174">
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1024
          </a>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1025
          6.1.List Comprehensions
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1026
         </h3>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1027
        </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1028
       </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1029
      </div>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1030
      <p id="ch5func_1e">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1031
       List Comprehension is a convenvience utility provided by Python. It is a
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1032
syntatic sugar to create
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1033
       <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1034
        <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1035
         Lists
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1036
        </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1037
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1038
       . Using
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1039
       <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1040
        <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1041
         List Comprehensions
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1042
        </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1043
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1044
       one can create
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1045
       <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1046
        <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1047
         Lists
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1048
        </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1049
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1050
       from other type of sequential data structures or other
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1051
       <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1052
        <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1053
         Lists
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1054
        </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1055
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1056
       itself.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1057
The syntax of
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1058
       <span class="emphasis">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1059
        <em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1060
         List Comprehensions
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1061
        </em>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1062
       </span>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1063
       consists of a square brackets to indicate
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1064
the result is a
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1065
       <span class="emphasis">
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1066
        <em>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1067
         List
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1068
        </em>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1069
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1070
       within which we include at least one
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1071
       <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1072
        <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1073
         for
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1074
        </strong>
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1075
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1076
       clause and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1077
multiple
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1078
       <span class="strong">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1079
        <strong>
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1080
         if
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1081
        </strong>
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1082
       </span>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1083
       clauses. It will be more clear with an example:
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1084
      </p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1085
      <pre class="programlisting">
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1086
       &gt;&gt;&gt; num = [1, 2, 3]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1087
&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
  1088
&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
  1089
[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
  1090
&gt;&gt;&gt; all_num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1091
&gt;&gt;&gt; even = [x for x in all_num if x%2 == 0]
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1092
      </pre>
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1093
      <p id="ch5func_1f">
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1094
       The syntax used here is very clear from the way it is written. It can be
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1095
translated into english as, "for each element x in the list all_num,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
  1096
if remainder of x divided by 2 is 0, add x to the list."
6
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1097
      </p>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1098
     </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1099
    </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1100
   </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1101
  </div>
1ce9b33fb6ff sorted the problem related to paths
amit@thunder
parents: 2
diff changeset
  1102
 </body>
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
  1103
</html>