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{Function} |
28 \section{Function} |
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. |
29 They allows us to enclose a set of statements and call them again and again instead of repeating the group of statements every-time. |
30 \subsection{Function definition} |
30 \subsection{Function definition} |
31 \begin{lstlisting} |
31 \begin{lstlisting} |
32 def signum( r ): |
32 In []: def signum(r): |
33 if r < 0: |
33 ...: if r < 0: |
34 return -1 |
34 ...: return -1 |
35 elif r > 0: |
35 ...: elif r > 0: |
36 return 1 |
36 ...: return 1 |
37 else: |
37 ...: else: |
38 return 0 |
38 ...: return 0 |
|
39 ...: |
|
40 ...: |
39 \end{lstlisting} |
41 \end{lstlisting} |
40 %\typ{def} is a keyword, which is used to define a function with given name. |
42 %\typ{def} is a keyword, which is used to define a function with given name. |
41 \subsection{Usage} |
43 \subsection{Usage} |
42 \begin{lstlisting} |
44 \begin{lstlisting} |
43 In []: signum(4) |
45 In []: signum(4) |
46 Out[]: 0 |
48 Out[]: 0 |
47 In []: signum(-4) |
49 In []: signum(-4) |
48 Out[]: -1 |
50 Out[]: -1 |
49 In []: signum() # ERROR signum() takes exactly 1 argument(0 given) |
51 In []: signum() # ERROR signum() takes exactly 1 argument(0 given) |
50 \end{lstlisting} |
52 \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. |
53 \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 passes 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} |
54 \subsection{Default Arguments} |
53 This feature allow the functions to take the arguments optionally. For example: |
55 This feature allow the functions to take the arguments optionally. For example: |
54 \begin{lstlisting} |
56 \begin{lstlisting} |
55 In []: greet = 'hello world' |
57 In []: greet = 'hello world' |
56 |
58 |
57 In []: greet.split() |
59 In []: greet.split() |
58 Out[]: ['hello', 'world'] |
60 Out[]: ['hello', 'world'] |
59 \end{lstlisting} |
61 \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. |
62 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} |
63 \begin{lstlisting} |
62 In []: line = 'Rossum, Guido, 54, 46, 55' |
64 In []: line = 'Rossum, Guido, 54, 46, 55' |
63 |
65 |
64 In []: line.split(',') #split with ',' |
66 In []: line.split(',') #split with ',' |
65 Out[]: ['Rossum', ' Guido', ' 54', |
67 Out[]: ['Rossum', ' Guido', ' 54', |
96 In [14]: wish(greetings='hey', name='madhu') |
98 In [14]: wish(greetings='hey', name='madhu') |
97 hey madhu |
99 hey madhu |
98 In [15]: wish(name='vattam', greetings = 'get lost') |
100 In [15]: wish(name='vattam', greetings = 'get lost') |
99 get lost vattam |
101 get lost vattam |
100 \end{lstlisting} |
102 \end{lstlisting} |
|
103 % sorry Vattam just a joke :P |
101 \section{Self contained python script} |
104 \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. |
105 Functions like \typ{plot}, \typ{linspace} etc are not inbuilt functions. One have to import them to use them. |
103 \begin{lstlisting} |
106 \begin{lstlisting} |
104 from scipy import linspace, pi, sin |
107 from scipy import linspace, pi, sin |
105 from pylab import plot, legend, annotate |
108 from pylab import plot, legend, annotate |
113 legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
116 legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
114 annotate('origin', xy = (0, 0)) |
117 annotate('origin', xy = (0, 0)) |
115 xlim(-5*pi, 5*pi) |
118 xlim(-5*pi, 5*pi) |
116 ylim(-5*pi, 5*pi) |
119 ylim(-5*pi, 5*pi) |
117 \end{lstlisting} |
120 \end{lstlisting} |
118 Above mentioned code will work with following setup: |
121 These import statements are necessary to make program self contained. After importing, we can run script via:\\ |
|
122 \typ{$ python sine_plot.py} \\ %$ |
|
123 We no longer need: |
119 \begin{lstlisting} |
124 \begin{lstlisting} |
120 $ ipython -pylab |
125 $ ipython -pylab |
121 In []: %run -i sine_plot.py |
126 In []: %run -i sine_plot.py |
122 \end{lstlisting} %$ |
127 \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} |
128 \section{objects} |
127 In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects. |
129 In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects. |
128 \begin{lstlisting} |
130 \begin{lstlisting} |
129 In []: a = str() |
131 In []: a = str() # initializing a string object. |
130 In []: b = "Hello World" |
132 In []: b = "Hello World" |
131 In []: b.split() |
133 In []: b.split() # calling funciton on object 'b' |
132 Out[]: ['Hello', 'World'] |
134 Out[]: ['Hello', 'World'] |
133 \end{lstlisting} |
135 \end{lstlisting} |
134 ``.'' is a operator used to call functions defined for given object. |
136 ``.'' is a operator used to call functions defined for given object. |
135 \section{Links and References} |
137 \section{Links and References} |
136 \begin{itemize} |
138 \begin{itemize} |