1 ======== |
1 .. Objectives |
2 Script |
2 .. ---------- |
3 ======== |
3 |
4 |
4 .. At the end of this tutorial, you will be able to |
5 {{{ show the welcome slide }}} |
5 |
|
6 .. 1. Assign default values to arguments, when defining functions |
|
7 .. 2. Define and call functions with keyword arguments. |
|
8 .. 3. Also, you will get a glimpse of the plethora of functions |
|
9 .. available, in Python standard library and the scientific computing |
|
10 .. libraries. |
|
11 |
|
12 |
|
13 .. Prerequisites |
|
14 .. ------------- |
|
15 |
|
16 .. 1. getting started with ipython |
|
17 .. #. getting started with functions |
|
18 .. #. getting started with functions |
|
19 |
|
20 .. Author : Puneeth |
|
21 Internal Reviewer : |
|
22 External Reviewer : |
|
23 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
24 |
|
25 Script |
|
26 ------ |
|
27 |
|
28 {{{ Show the slide containing title }}} |
6 |
29 |
7 Welcome to the tutorial on advanced feature of functions. |
30 Welcome to the tutorial on advanced feature of functions. |
8 |
31 |
9 {{{ show the outline slide }}} |
32 {{{ Show the outline slide }}} |
10 |
33 |
11 In this tutorial we shall be looking at specifying default arguments |
34 In this tutorial we shall be looking at specifying default arguments |
12 to functions when defining them and calling functions using keyword |
35 to functions when defining them and calling functions using keyword |
13 arguments. We shall also, look at some of the built-in functions |
36 arguments. We shall also, look at some of the built-in functions |
14 available in the standard library of Python. |
37 available in the standard library of Python and the scientific |
|
38 computing libraries. |
15 |
39 |
16 {{{ switch to terminal }}} |
40 {{{ switch to terminal }}} |
17 |
41 |
18 We have an ``ipython`` terminal open, which we shall be using through |
42 We have an ``ipython`` terminal open, which we shall be using through |
19 out this session. |
43 out this session. |
44 plot(x, y, 'o') # plots x vs. y with circle markers. |
68 plot(x, y, 'o') # plots x vs. y with circle markers. |
45 |
69 |
46 linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi |
70 linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi |
47 linspace(0, 2*pi) # returns 50 points between 0 and 2pi |
71 linspace(0, 2*pi) # returns 50 points between 0 and 2pi |
48 |
72 |
49 #[punch: all above content goes on to a slide] |
73 .. #[punch: all above content goes on to a slide] |
50 |
74 |
51 {{{ switch back to ipython }}} |
75 {{{ switch back to ipython }}} |
52 |
76 |
53 Let's now define a simple function that uses default arguments. We |
77 Let's now define a simple function that uses default arguments. We |
54 define a simple function that prints a welcome message to a person, |
78 define a simple function that prints a welcome message to a person, |
74 welcome("Hello") |
98 welcome("Hello") |
75 |
99 |
76 "Hello" is treated as the ``greet`` and we get "Hello World" as |
100 "Hello" is treated as the ``greet`` and we get "Hello World" as |
77 the output. "World" is the default value for the argument ``name``. |
101 the output. "World" is the default value for the argument ``name``. |
78 |
102 |
79 E%% %% Pause the video here and redefine the function ``welcome``, by |
103 Following is an (are) exercise(s) that you must do. |
80 interchanging it's arguments. Place the ``name`` argument with it's |
104 |
81 default value of "Hello" before the ``greet`` argument. Then, resume |
105 %%1%% Redefine the function ``welcome``, by interchanging it's |
82 the video. |
106 arguments. Place the ``name`` argument with it's default value of |
|
107 "Hello" before the ``greet`` argument. |
|
108 |
|
109 Please, pause the video here. Do the exercise and then continue. |
83 |
110 |
84 :: |
111 :: |
85 |
112 |
86 def welcome(name="World", greet): |
113 def welcome(name="World", greet): |
87 print greet, name |
114 print greet, name |
88 |
115 |
89 We get an error that reads ``SyntaxError: non-default argument follows |
116 We get an error that reads ``SyntaxError: non-default argument follows |
90 default argument``. When defining a function all the argument with |
117 default argument``. When defining a function all the argument with |
91 default values should come at the end. |
118 default values should come at the end. |
92 |
119 |
93 E%% %% Pause the video here and type ``linspace?`` to see the |
120 Following is an exercise that you must do. |
94 definition of the command and notice how all the arguments with |
121 |
95 default values are towards the end. |
122 %%2%% See the definition of linspace using ``?`` and observe how all |
|
123 the arguments with default values are towards the end. |
|
124 |
|
125 Please, pause the video here. Do the exercise and then continue. |
96 |
126 |
97 :: |
127 :: |
98 |
128 |
99 linspace? |
129 linspace? |
100 |
130 |
101 E%% %% Pause the video here and redefine the function ``welcome`` with |
131 Following is an exercise that you must do. |
102 a default value of "Hello" to the ``greet`` argument. Then, call the |
132 |
103 function without any arguments. Then, resume the video. |
133 %%3%% Redefine the function ``welcome`` with a default value of |
|
134 "Hello" to the ``greet`` argument. Then, call the function without any |
|
135 arguments. |
|
136 |
|
137 Please, pause the video here. Do the exercise and then continue. |
104 |
138 |
105 :: |
139 :: |
106 |
140 |
107 def welcome(greet="Hello", name="World"): |
141 def welcome(greet="Hello", name="World"): |
108 print greet, name |
142 print greet, name |
173 |
207 |
174 :: |
208 :: |
175 |
209 |
176 Math functions - abs, sin, .... |
210 Math functions - abs, sin, .... |
177 |
211 |
178 #[punch: Need to decide, exactly what to put here. Reviewer comments |
212 .. #[punch: Need to decide, exactly what to put here. Reviewer comments |
179 welcome.] |
213 .. welcome.] |
180 |
214 |
181 |
215 |
182 {{{ switch to slide showing classes of functions in pylab, scipy }}} |
216 {{{ switch to slide showing classes of functions in pylab, scipy }}} |
183 |
217 |
184 Apart from the standard library there are other libraries like ``pylab``, |
218 Apart from the standard library there are other libraries like ``pylab``, |
190 plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog |
224 plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog |
191 |
225 |
192 scipy (modules) |
226 scipy (modules) |
193 fftpack, stats, linalg, ndimage, signal, optimize, integrate |
227 fftpack, stats, linalg, ndimage, signal, optimize, integrate |
194 |
228 |
195 {{{ switch slide to summary slide }}} |
229 {{{ Show summary slide }}} |
196 |
230 |
197 That brings us to the end of this tutorial. In this tutorial we have |
231 That brings us to the end of this tutorial. In this tutorial we have |
198 learnt how to use functions with default values and keyword |
232 learnt how to use functions with default values and keyword |
199 arguments. We also looked at the range of functions available in the |
233 arguments. We also looked at the range of functions available in the |
200 Python standard library and the Scientific Computing related |
234 Python standard library and the Scientific Computing related |
201 packages. |
235 packages. |
202 |
236 |
203 Thank You! |
237 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
238 |
|
239 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
240 |
|
241 Hope you have enjoyed and found it useful. |
|
242 Thank you! |