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
|
\documentclass[12pt]{article}
\title{Python: Data Structures}
\author{FOSSEE}
\usepackage{listings}
\lstset{language=Python,
basicstyle=\ttfamily,
commentstyle=\itshape\bfseries,
showstringspaces=false
}
\newcommand{\typ}[1]{\lstinline{#1}}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}
\usepackage{url}
\begin{document}
\date{}
\vspace{-1in}
\begin{center}
\LARGE{Python: Functions and Objects}\\
\large{FOSSEE}
\end{center}
\section{Function}
They allows us to enclose a set of statements and call them again and again instead of repeating the group of statements every-time.
\subsection{Function definition}
\begin{lstlisting}
In []: def signum(r):
...: if r < 0:
...: return -1
...: elif r > 0:
...: return 1
...: else:
...: return 0
...:
...:
\end{lstlisting}
%\typ{def} is a keyword, which is used to define a function with given name.
\subsection{Usage}
\begin{lstlisting}
In []: signum(4)
Out[]: 1
In []: signum(0)
Out[]: 0
In []: signum(-4)
Out[]: -1
In []: signum() # ERROR signum() takes exactly 1 argument(0 given)
\end{lstlisting}
\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, tuples), they cant be modified in the function, but objects like \typ{list} and dictionary can be manipulated.
\subsection{Default Arguments}
This feature allow the functions to take the arguments optionally. For example:
\begin{lstlisting}
In []: greet = 'hello world'
In []: greet.split()
Out[]: ['hello', 'world']
\end{lstlisting}
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.
\begin{lstlisting}
In []: line = 'Rossum, Guido, 54, 46, 55'
In []: line.split(',') #split with ','
Out[]: ['Rossum', ' Guido', ' 54',
' 46', ' 55']
\end{lstlisting}
Function to work with default argument can be defined as:
\begin{lstlisting}
def welcome(greet, name='world!'):
print greet, name
\end{lstlisting}
above function can be used as:
\begin{lstlisting}
In []: welcome("Hello") #using default argument
Hello World!
In []: welcome("Hi", "Guido") #taking name via argument
Hi Guido
\end{lstlisting}
\subsection{Keyword Arguments}
This feature provides the facility of passing arguments by specifying the name of the parameter as defined in the function definition. You don't have to remember the order of the parameters in function definition. For example:
\begin{lstlisting}
In []: plot(y, sin(y), 'g', linewidth=2)
In []: plot(y, cos(y), linewidth=1, color='g')
\end{lstlisting}
Both call to \typ{plot} function will work and parameters are set accordingly.\\
One can define a function such that keyword arguments can be used in following way:
\begin{lstlisting}
def wish(name='World', greetings='Hello'):
print greetings, name
\end{lstlisting}
This function can be called as:
\begin{lstlisting}
In [13]: wish() #default arguments will work
Hello World
In [14]: wish(greetings='hey', name='madhu')
hey madhu
In [15]: wish(name='vattam', greetings = 'bye bye')
bye bye vattam
\end{lstlisting}
% sorry Vattam just a joke :P
\section{Self contained python script}
Functions like \typ{plot}, \typ{linspace} etc are not inbuilt functions. One have to import them to use them.
\begin{lstlisting}
from scipy import linspace, pi, sin
from pylab import plot, legend, annotate
from pylab import xlim, ylim
x = linspace(-5*pi, 5*pi, 500)
plot(x, x, 'b')
plot(x, -x, 'b')
plot(x, sin(x), 'g', linewidth=2)
plot(x, x*sin(x), 'r', linewidth=3)
legend(['x', '-x', 'sin(x)', 'xsin(x)'])
annotate('origin', xy = (0, 0))
xlim(-5*pi, 5*pi)
ylim(-5*pi, 5*pi)
\end{lstlisting}
These import statements are necessary to make program self contained. After importing, we can run script via:\\
\typ{$ python sine_plot.py} \\ %$
We no longer need:
\begin{lstlisting}
$ ipython -pylab
In []: %run -i sine_plot.py
\end{lstlisting} %$
\section{objects}
In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects.
\begin{lstlisting}
In []: a = str() # initializing a string object.
In []: b = "Hello World"
In []: b.split() # calling function on object 'b'
Out[]: ['Hello', 'World']
\end{lstlisting}
``.'' is a operator used to call functions defined for given object.
\section{Links and References}
\begin{itemize}
\item Some of inbuilt functions available with Python are listed at\\ \url{http://docs.python.org/library/functions.html}
\item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/}
\end{itemize}
\end{document}
|