summaryrefslogtreecommitdiff
path: root/advanced_features_of_functions/script.rst
blob: a3676d352666b72f33b61ea7b7cee4f8d03b946f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
.. Objectives
.. ----------

.. At the end of this tutorial, you will be able to 

.. 1. Assign default values to arguments, when defining functions
.. 2. Define and call functions with keyword arguments. 
.. 3. Also, you will get a glimpse of the plethora of functions
.. available, in Python standard library and the scientific computing
.. libraries. 


.. Prerequisites
.. -------------

..   1. getting started with ipython
..   #. getting started with functions
     
.. Author              : Puneeth 
   Internal Reviewer   : Anoop Jacob Thomas<anoop@fossee.in>
   External Reviewer   :
   Language Reviewer   : Bhanukiran
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]

Script
------


.. L1

{{{ Show the  first slide containing title, name of the production
team along with the logo of MHRD }}}

.. R1

Hello friends and Welcome to the tutorial on 'advanced features of functions'. 

.. L2

{{{ Show the objective slide }}}

.. R2

At the end of this tutorial, you will be able to, 

 1. Assign default values to arguments, when defining functions.
 #. Define and call functions with keyword arguments. 
 #. Learn some of the built-in functions available in Python standard 
    library and the scientific computing libraries.

.. L3

{{{ Switch to the pre-requisite slide }}}

.. R3

Before beginning this tutorial,we would suggest you to complete the 
tutorial on "Getting started with functions".

.. R4

Let us Start the ipython interpreter 

.. L4

{{{ Open the terminal }}}
::

    ipython -pylab.

.. R5

Let's use the ``round`` function as an example to understand what a
default value of an argument means. Let's type the following
expressions in the terminal. 

.. L5
::

    round(2.484)

    round(2.484, 2)

.. R6

Both the first expression and the second are calls to the ``round``
function, but the first calls it with only one argument and the second
calls it with two arguments. By observing the output, we can guess
that the first one is equivalent to call with the second argument
being 0. 0 .

.. L6

.. L7

{{{ show a slide with examples of functions showing default values }}}

.. R7

  s.strip() # strips on spaces. 
  s.strip('@') # strips the string of '@' symbols.

Thus it can be said that here, blank space is the default argument.

  plot(x, y) # plots with x vs. y using default line style. 
  plot(x, y, 'o') # plots x vs. y with circle markers. 

Hence, here when third argument is not provided, it shows default line style.

  linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi
  linspace(0, 2*pi) # returns 50 points between 0 and 2pi

Hence, the default for the third argument is 50.

.. R8

Let's now define a simple function that uses default arguments. We
define a simple function that prints a welcome message to a person,
given a greeting and his/her name.

.. L8

{{{ switch to terminal }}
::

    def welcome(greet, name="World"):
        print greet, name

.. R9

Let us first call the function with two arguments, one for ``greet``
and other for ``name``.

.. L9
::

    welcome("Hi", "Guido")          

.. R10

We get the expected welcome message, "Hi Guido". 

Now let us call the function with just one argument "Hello". 

.. L10
::

    welcome("Hello")

.. R11

"Hello" is treated as the ``greet`` and we get "Hello World" as
the output. "World" is the default value for the argument ``name``. 

Pause the video here, try out the following exercise and resume the video.

.. L11

.. L12

{{{ Show slide with exercise 1 }}}

.. R12

 Redefine the function ``welcome``, by interchanging it's
 arguments. Place the ``name`` argument with it's default value of
 "World" before the ``greet`` argument.

.. R13

Switch to the terminal for solution

.. L13

{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::

    def welcome(name="World", greet):
        print greet, name

.. R14

We get an error that reads ``SyntaxError: non-default argument follows
default argument``. 

.. L14

{{{ Show slide with solution 1 }}}

.. R15

When defining a function all the argument with
default values should come at the end. 

Pause the video here, try out the following exercise and resume the video.

.. L15

.. L16

{{{ Show slide with exercise 2 }}}

.. R16

 See the definition of linspace using ``?`` and make a note of all
 the arguments with default values are towards the end.

.. R17

Switch to the terminal for solution

.. L17

{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::

    linspace?

.. R18

As we go on hitting the enter key, we the the number of arguments this
command has.Please read the content on your terminal.
<pause>

Again,Pause the video here,try out the following exercise and resume the video.

.. L18

.. L19

{{{ Show slide with exercise 3 }}}

.. R19

 Redefine the function ``welcome`` with a default value of
 "Hello" to the ``greet`` argument. Then, call the function without any
 arguments. 

.. R20

Switch to the terminal for solution

.. L20

{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::

    def welcome(greet="Hello", name="World"):
        print greet, name
 
    welcome()

.. R21
 
As we can see, we get the output as ``Hello World``.

Let us now learn what keyword arguments or named arguments are. We
shall refer to them as keyword arguments, henceforth. 

.. L21

.. L22

{{{ show slide keyword arguments examples }}}

  1. legend(['sin(2y)'], loc = 'center')
  #. plot(y, sin(y), 'g', linewidth = 2)
  #. annotate('local max', xy = (1.5, 1))
  #. pie(science.values(), labels = science.keys())

.. R22

When you are calling functions in Python, you don't need to remember
the order in which to pass the arguments. Instead, you can use the
name of the argument to pass it a value. This slide shows a few
function calls that use keyword arguments. ``loc``, ``linewidth``,
``xy`` and ``labels`` are being called with keyword arguments. 

Let us try and understand this better using the ``welcome`` function
that we have been using all along. Let us call it in different ways
and observe the output to see how keyword arguments work. 

.. L22

{{{ switch to ipython terminal }}}
::

    welcome()

    welcome("Hello", "James")

    welcome("Hi", name="Guido")

.. R23

When no keyword is specified, the arguments are allotted based on
their position. So, "Hi" is the value of the argument ``greet`` and
name is passed the value "Guido".If we type, 

.. L23
::

    welcome(name="Guido", greet="Hey! ")

.. R24

When keyword arguments are used, the arguments can be called in any
order. And if we call our function as,

.. L24
::

    welcome(name="Guido", "Hey")

.. R25

This call returns an error that reads, ``non-keyword arg after keyword
arg``. Python expects all the keyword to be present towards the end. 

That brings us to the end of what we wanted to learn about ``keyword``
arguments. 

.. L25

.. L26

{{{ switch to slide built-in functions }}}

.. R26

Before defining a function of your own, make sure that you check the
standard library, for a similar function. Python is popularly called a
"Batteries included" language, for the huge library that comes along
with it. 

  Math functions - abs, sin, ....
  Plot functions - plot, bar, pie ...
  Boolean functions - and, or, not ... 

.. L27
  
{{{ switch to slide showing classes of functions in pylab, scipy }}}

.. R27

Apart from the standard library there are other libraries like ``pylab``,
``scipy``, etc which have a huge collection of functions for scientific
purposes. 

  pylab
    plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog

  scipy (modules)
    fftpack, stats, linalg, ndimage, signal, optimize, integrate

.. L28

{{{ Show summary slide }}}

.. R28

This brings us to the end of this tutorial. In this tutorial, we 
have learnt to, 

 1. Define functions with default arguments.
 #. Call functions using keyword arguments.
 #. Use the range of functions available in the Python standard library 
    and the Scientific Computing related packages. 

.. L30

{{{Show self assessment questions slide}}}

.. R30

Here are some self assessment questions for you to solve

1. All arguments of a function cannot have default values. 
   - True or False? 

2. The following is a valid function definition. True or False? 
::
   
    def seperator(count=40, char, show=False):
        if show:
            print char * count
        return char * count

3. When calling a function, 

   - the arguments should always be in the order in which they are defined.
   - the arguments can be in any order.
   - only keyword arguments can be in any order, but should be called
      at the beginning.
   - only keyword arguments can be in any order, but should be called
     at the end.

.. L31

{{{solution of self assessment questions on slide}}}

.. R31

And the answers,

1. Flase.All arguments of a Python function can have default values.

2. False. All parameters with default arguments should be
   defined at the end. 

3. When calling a function,only keyword arguments can be in any order, 
   but should be called at the end. 

.. L32

{{{ Show the Thankyou slide }}}

.. R32

Hope you have enjoyed this tutorial and found it useful.
Thank you!