diff -r 4075482a9770 -r 0a6ab1d81491 day2/cheatsheet3.tex --- a/day2/cheatsheet3.tex Tue Dec 08 12:40:07 2009 +0530 +++ b/day2/cheatsheet3.tex Tue Dec 08 13:06:14 2009 +0530 @@ -26,16 +26,18 @@ \large{FOSSEE} \end{center} \section{Function} -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. +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} -def signum( r ): - if r < 0: - return -1 - elif r > 0: - return 1 - else: - return 0 +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} @@ -48,7 +50,7 @@ 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 pass immutable types(String, tupels), they cant be modified in the function, but objects like \typ{list} and dictionary can be manipulated. +\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. \subsection{Default Arguments} This feature allow the functions to take the arguments optionally. For example: \begin{lstlisting} @@ -57,7 +59,7 @@ 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. +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' @@ -98,6 +100,7 @@ In [15]: wish(name='vattam', greetings = 'get lost') get lost 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} @@ -115,20 +118,19 @@ xlim(-5*pi, 5*pi) ylim(-5*pi, 5*pi) \end{lstlisting} -Above mentioned code will work with following setup: +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} %$ -as we are already including \typ{pylab} into \typ{ipython}. But to make it work independently so that even\\ -\typ{$ python sine_plot.py} \\ %$ -works, one will have to use \typ{import} statements.\\ \section{objects} In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects. \begin{lstlisting} -In []: a = str() +In []: a = str() # initializing a string object. In []: b = "Hello World" -In []: b.split() +In []: b.split() # calling funciton on object 'b' Out[]: ['Hello', 'World'] \end{lstlisting} ``.'' is a operator used to call functions defined for given object.