author | Puneeth Chaganti <punchagan@fossee.in> |
Thu, 07 Oct 2010 12:28:12 +0530 | |
changeset 242 | a33e942379d7 |
parent 241 | 2b2c94ee2c3e |
child 272 | 08638b1e211e |
permissions | -rw-r--r-- |
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
1 |
.. Objectives |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
2 |
.. ---------- |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
3 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
4 |
.. At the end of this tutorial, you will be able to |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
5 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
6 |
.. 1. Assign default values to arguments, when defining functions |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
7 |
.. 2. Define and call functions with keyword arguments. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
8 |
.. 3. Also, you will get a glimpse of the plethora of functions |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
9 |
.. available, in Python standard library and the scientific computing |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
10 |
.. libraries. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
11 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
12 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
13 |
.. Prerequisites |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
14 |
.. ------------- |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
15 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
16 |
.. 1. getting started with ipython |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
17 |
.. #. getting started with functions |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
18 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
19 |
.. Author : Puneeth |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
20 |
Internal Reviewer : |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
21 |
External Reviewer : |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
22 |
Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
23 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
24 |
Script |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
25 |
------ |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
26 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
27 |
{{{ Show the slide containing title }}} |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
28 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
29 |
Welcome to the tutorial on advanced feature of functions. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
30 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
31 |
{{{ Show the outline slide }}} |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
32 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
33 |
In this tutorial we shall be looking at specifying default arguments |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
34 |
to functions when defining them and calling functions using keyword |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
35 |
arguments. We shall also, look at some of the built-in functions |
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
36 |
available in the standard library of Python and the scientific |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
37 |
computing libraries. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
38 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
39 |
{{{ switch to terminal }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
40 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
41 |
We have an ``ipython`` terminal open, which we shall be using through |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
42 |
out this session. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
43 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
44 |
Let's use the ``round`` function as an example to understand what a |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
45 |
default value of an argument means. Let's type the following |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
46 |
expressions in the terminal. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
47 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
48 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
49 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
50 |
round(2.484) |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
51 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
52 |
round(2.484, 2) |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
53 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
54 |
Both the first expression and the second are calls to the ``round`` |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
55 |
function, but the first calls it with only one argument and the second |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
56 |
calls it with two arguments. By observing the output, we can guess |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
57 |
that the first one is equivalent to call with the second argument |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
58 |
being 0. 0 is the default value of the argument. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
59 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
60 |
{{{ show a slide with examples of functions showing default values }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
61 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
62 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
63 |
s.strip() # strips on spaces. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
64 |
s.strip('@') # strips the string of '@' symbols. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
65 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
66 |
plot(x, y) # plots with x vs. y using default line style. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
67 |
plot(x, y, 'o') # plots x vs. y with circle markers. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
68 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
69 |
linspace(0, 2*pi, 100) # returns 100 points between 0 and 2pi |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
70 |
linspace(0, 2*pi) # returns 50 points between 0 and 2pi |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
71 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
72 |
.. #[punch: all above content goes on to a slide] |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
73 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
74 |
{{{ switch back to ipython }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
75 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
76 |
Let's now define a simple function that uses default arguments. We |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
77 |
define a simple function that prints a welcome message to a person, |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
78 |
given a greeting and his/her name. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
79 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
80 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
81 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
82 |
def welcome(greet, name="World"): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
83 |
print greet, name |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
84 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
85 |
Let us first call the function with two arguments, one for ``greet`` |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
86 |
and other for ``name``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
87 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
88 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
89 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
90 |
welcome("Hi", "Guido") |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
91 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
92 |
We get the expected welcome message, "Hi Guido". |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
93 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
94 |
Now let us call the function with just one argument "Hello". |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
95 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
96 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
97 |
welcome("Hello") |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
98 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
99 |
"Hello" is treated as the ``greet`` and we get "Hello World" as |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
100 |
the output. "World" is the default value for the argument ``name``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
101 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
102 |
Following is an (are) exercise(s) that you must do. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
103 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
104 |
%%1%% Redefine the function ``welcome``, by interchanging it's |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
105 |
arguments. Place the ``name`` argument with it's default value of |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
106 |
"Hello" before the ``greet`` argument. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
107 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
108 |
Please, pause the video here. Do the exercise and then continue. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
109 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
110 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
111 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
112 |
def welcome(name="World", greet): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
113 |
print greet, name |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
114 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
115 |
We get an error that reads ``SyntaxError: non-default argument follows |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
116 |
default argument``. When defining a function all the argument with |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
117 |
default values should come at the end. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
118 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
119 |
Following is an exercise that you must do. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
120 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
121 |
%%2%% See the definition of linspace using ``?`` and observe how all |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
122 |
the arguments with default values are towards the end. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
123 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
124 |
Please, pause the video here. Do the exercise and then continue. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
125 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
126 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
127 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
128 |
linspace? |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
129 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
130 |
Following is an exercise that you must do. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
131 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
132 |
%%3%% Redefine the function ``welcome`` with a default value of |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
133 |
"Hello" to the ``greet`` argument. Then, call the function without any |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
134 |
arguments. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
135 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
136 |
Please, pause the video here. Do the exercise and then continue. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
137 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
138 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
139 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
140 |
def welcome(greet="Hello", name="World"): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
141 |
print greet, name |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
142 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
143 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
144 |
welcome() |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
145 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
146 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
147 |
Let us now learn what keyword arguments are. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
148 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
149 |
{{{ show a slide with examples using keyword arguments. }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
150 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
151 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
152 |
legend(['sin(2y)'], loc = 'center') |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
153 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
154 |
plot(y, sin(y), 'g', linewidth = 2) |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
155 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
156 |
annotate('local max', xy = (1.5, 1)) |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
157 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
158 |
pie(science.values(), labels = science.keys()) |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
159 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
160 |
When you are calling functions in Python, you don't need to remember |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
161 |
the order in which to pass the arguments. Instead, you can use the |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
162 |
name of the argument to pass it a value. This slide shows a few |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
163 |
function calls that use keyword arguments. ``loc``, ``linewidth``, |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
164 |
``xy`` and ``labels`` are being called with keyword arguments. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
165 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
166 |
{{{ switch to ipython terminal }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
167 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
168 |
Let us try and understand this better using the ``welcome`` function |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
169 |
that we have been using all along. Let us call it in different ways |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
170 |
and observe the output to see how keyword arguments work. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
171 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
172 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
173 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
174 |
welcome() |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
175 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
176 |
welcome("Hello", "James") |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
177 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
178 |
welcome("Hi", name="Guido") |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
179 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
180 |
When no keyword is specified, the arguments are allotted based on |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
181 |
their position. So, "Hi" is the value of the argument ``greet`` and |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
182 |
name is passed the value "Guido". |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
183 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
184 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
185 |
welcome(name="Guido", greet="Hey! ") |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
186 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
187 |
When keyword arguments are used, the arguments can be called in any |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
188 |
order. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
189 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
190 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
191 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
192 |
welcome(name="Guido", "Hey") |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
193 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
194 |
This call returns an error that reads, ``non keyword arg after keyword |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
195 |
arg``. Python expects all the keyword to be present towards the end. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
196 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
197 |
That brings us to the end of what we wanted to learn about ``keyword`` |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
198 |
arguments. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
199 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
200 |
{{{ switch to a slide showing variety of functions with uses }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
201 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
202 |
Before defining a function of your own, make sure that you check the |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
203 |
standard library, for a similar function. Python is popularly called a |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
204 |
"Batteries included" language, for the huge library that comes along |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
205 |
with it. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
206 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
207 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
208 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
209 |
Math functions - abs, sin, .... |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
210 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
211 |
.. #[punch: Need to decide, exactly what to put here. Reviewer comments |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
212 |
.. welcome.] |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
213 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
214 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
215 |
{{{ switch to slide showing classes of functions in pylab, scipy }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
216 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
217 |
Apart from the standard library there are other libraries like ``pylab``, |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
218 |
``scipy``, etc which have a huge collection of functions for scientific |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
219 |
purposes. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
220 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
221 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
222 |
pylab |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
223 |
plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
224 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
225 |
scipy (modules) |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
226 |
fftpack, stats, linalg, ndimage, signal, optimize, integrate |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
227 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
228 |
{{{ Show summary slide }}} |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
229 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
230 |
That brings us to the end of this tutorial. In this tutorial we have |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
231 |
learnt how to use functions with default values and keyword |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
232 |
arguments. We also looked at the range of functions available in the |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
233 |
Python standard library and the Scientific Computing related |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
234 |
packages. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
235 |
|
240
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
236 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
237 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
238 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
239 |
|
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
240 |
Hope you have enjoyed and found it useful. |
f4c36aa38a84
Cleaned up the script of advanced features of functions LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
241 |
Thank you! |