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