23 \vspace{-1in} |
23 \vspace{-1in} |
24 \begin{center} |
24 \begin{center} |
25 \LARGE{Python: Functions and Objects}\\ |
25 \LARGE{Python: Functions and Objects}\\ |
26 \large{FOSSEE} |
26 \large{FOSSEE} |
27 \end{center} |
27 \end{center} |
28 \section{Functions} |
28 \section{Function} |
29 Function definition |
29 They allows us to enclose a set of statements and call the function again and again instead of repeating the group of statements every-time. |
|
30 \subsection{Function definition} |
30 \begin{lstlisting} |
31 \begin{lstlisting} |
31 def signum( r ): |
32 def signum( r ): |
32 if r < 0: |
33 if r < 0: |
33 return -1 |
34 return -1 |
34 elif r > 0: |
35 elif r > 0: |
35 return 1 |
36 return 1 |
36 else: |
37 else: |
37 return 0 |
38 return 0 |
38 \end{lstlisting} |
39 \end{lstlisting} |
39 Default Arguments |
40 %\typ{def} is a keyword, which is used to define a function with given name. |
|
41 \subsection{Usage} |
|
42 \begin{lstlisting} |
|
43 In []: signum(4) |
|
44 Out[]: 1 |
|
45 In []: signum(0) |
|
46 Out[]: 0 |
|
47 In []: signum(-4) |
|
48 Out[]: -1 |
|
49 In []: signum() # ERROR signum() takes exactly 1 argument(0 given) |
|
50 \end{lstlisting} |
|
51 \textbf{Note:} Arguments passed to a function are passed by-value \textbf{only if} they are basic Python data type(int, float). In case one pass immutable types(String, tupels), they cant be modified in the function, but objects like \typ{list} and dictionary can be manipulated. |
|
52 \subsection{Default Arguments} |
|
53 This feature allow the functions to take the arguments optionally. For example: |
|
54 \begin{lstlisting} |
|
55 In []: greet = 'hello world' |
|
56 |
|
57 In []: greet.split() |
|
58 Out[]: ['hello', 'world'] |
|
59 \end{lstlisting} |
|
60 In above case, default argument which \typ{split} function uses is a blank space. One can pass argument also, to split the string for a different delimiter. |
|
61 \begin{lstlisting} |
|
62 In []: line = 'Rossum, Guido, 54, 46, 55' |
|
63 |
|
64 In []: line.split(',') #split with ',' |
|
65 Out[]: ['Rossum', ' Guido', ' 54', |
|
66 ' 46', ' 55'] |
|
67 \end{lstlisting} |
|
68 Function to work with default argument can be defined as: |
40 \begin{lstlisting} |
69 \begin{lstlisting} |
41 def welcome(greet, name='world!'): |
70 def welcome(greet, name='world!'): |
42 print greet, name |
71 print greet, name |
43 \end{lstlisting} |
72 \end{lstlisting} |
44 Keyword Arguments |
73 above function can be used as: |
|
74 \begin{lstlisting} |
|
75 In []: welcome("Hello") #using default argument |
|
76 Hello World! |
|
77 In []: welcome("Hi", "Guido") #taking name via argument |
|
78 Hi Guido |
|
79 \end{lstlisting} |
|
80 \subsection{Keyword Arguments} |
|
81 This feature provides the facility of passing arguments by specifying the name of the parameter as defined in the function definition. You dont have to remember the order of the parameters in function definition. For example: |
45 \begin{lstlisting} |
82 \begin{lstlisting} |
46 In []: plot(y, sin(y), 'g', linewidth=2) |
83 In []: plot(y, sin(y), 'g', linewidth=2) |
|
84 In []: plot(y, cos(y), linewidth=1, color='g') |
47 \end{lstlisting} |
85 \end{lstlisting} |
48 Self contained python script |
86 Both call to \typ{plot} function will work and paramenters are set accordingly.\\ |
|
87 One can define a function such that keyword arguments can be used in following way: |
|
88 \begin{lstlisting} |
|
89 def wish(name='World', greetings='Hello'): |
|
90 print greetings, name |
|
91 \end{lstlisting} |
|
92 This function can be called as: |
|
93 \begin{lstlisting} |
|
94 In [13]: wish() #default arguments will work |
|
95 Hello World |
|
96 In [14]: wish(greetings='hey', name='madhu') |
|
97 hey madhu |
|
98 In [15]: wish(name='vattam', greetings = 'get lost') |
|
99 get lost vattam |
|
100 \end{lstlisting} |
|
101 \section{Self contained python script} |
|
102 Functions like \typ{plot}, \typ{linspace} etc are not inbuilt functions. One have to import them to use them. |
49 \begin{lstlisting} |
103 \begin{lstlisting} |
50 from scipy import linspace, pi, sin |
104 from scipy import linspace, pi, sin |
51 from pylab import plot, legend, annotate |
105 from pylab import plot, legend, annotate |
52 from pylab import xlim, ylim |
106 from pylab import xlim, ylim |
53 |
107 |
59 legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
113 legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
60 annotate('origin', xy = (0, 0)) |
114 annotate('origin', xy = (0, 0)) |
61 xlim(-5*pi, 5*pi) |
115 xlim(-5*pi, 5*pi) |
62 ylim(-5*pi, 5*pi) |
116 ylim(-5*pi, 5*pi) |
63 \end{lstlisting} |
117 \end{lstlisting} |
64 |
118 Above mentioned code will work with following setup: |
|
119 \begin{lstlisting} |
|
120 $ ipython -pylab |
|
121 In []: %run -i sine_plot.py |
|
122 \end{lstlisting} %$ |
|
123 as we are already including \typ{pylab} into \typ{ipython}. But to make it work independently so that even\\ |
|
124 \typ{$ python sine_plot.py} \\ %$ |
|
125 works, one will have to use \typ{import} statements.\\ |
|
126 \section{objects} |
|
127 In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects. |
|
128 \begin{lstlisting} |
|
129 In []: a = str() |
|
130 In []: b = "Hello World" |
|
131 In []: b.split() |
|
132 Out[]: ['Hello', 'World'] |
|
133 \end{lstlisting} |
|
134 ``.'' is a operator used to call functions defined for given object. |
|
135 \section{Links and References} |
|
136 \begin{itemize} |
|
137 \item Some of inbult functions available with Python are listed at\\ \url{http://docs.python.org/library/functions.html} |
|
138 \item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/} |
|
139 \end{itemize} |
65 \end{document} |
140 \end{document} |