day2/cheatsheet3.tex
changeset 330 46533051b9d3
parent 329 0a6ab1d81491
child 346 e9961fb16c58
equal deleted inserted replaced
329:0a6ab1d81491 330:46533051b9d3
    48 Out[]: 0
    48 Out[]: 0
    49 In []: signum(-4)
    49 In []: signum(-4)
    50 Out[]: -1
    50 Out[]: -1
    51 In []: signum() # ERROR signum() takes exactly 1 argument(0 given)
    51 In []: signum() # ERROR signum() takes exactly 1 argument(0 given)
    52 \end{lstlisting}
    52 \end{lstlisting}
    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.
    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, tuples), they cant be modified in the function, but objects like \typ{list} and dictionary can be manipulated.
    54 \subsection{Default Arguments}
    54 \subsection{Default Arguments}
    55 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:
    56 \begin{lstlisting}
    56 \begin{lstlisting}
    57 In []: greet = 'hello world'
    57 In []: greet = 'hello world'
    58 
    58 
    78 Hello World!
    78 Hello World!
    79 In []: welcome("Hi", "Guido") #taking name via argument
    79 In []: welcome("Hi", "Guido") #taking name via argument
    80 Hi Guido
    80 Hi Guido
    81 \end{lstlisting}
    81 \end{lstlisting}
    82 \subsection{Keyword Arguments}
    82 \subsection{Keyword Arguments}
    83 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:
    83 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:
    84 \begin{lstlisting}
    84 \begin{lstlisting}
    85 In []: plot(y, sin(y), 'g', linewidth=2)
    85 In []: plot(y, sin(y), 'g', linewidth=2)
    86 In []: plot(y, cos(y), linewidth=1, color='g')
    86 In []: plot(y, cos(y), linewidth=1, color='g')
    87 \end{lstlisting}
    87 \end{lstlisting}
    88 Both call to \typ{plot} function will work and paramenters are set accordingly.\\
    88 Both call to \typ{plot} function will work and parameters are set accordingly.\\
    89 One can define a function such that keyword arguments can be used in following way:
    89 One can define a function such that keyword arguments can be used in following way:
    90 \begin{lstlisting}
    90 \begin{lstlisting}
    91 def wish(name='World', greetings='Hello'):
    91 def wish(name='World', greetings='Hello'):
    92   print greetings, name
    92   print greetings, name
    93 \end{lstlisting}
    93 \end{lstlisting}
    95 \begin{lstlisting}
    95 \begin{lstlisting}
    96 In [13]: wish() #default arguments will work
    96 In [13]: wish() #default arguments will work
    97 Hello World
    97 Hello World
    98 In [14]: wish(greetings='hey', name='madhu')
    98 In [14]: wish(greetings='hey', name='madhu')
    99 hey madhu
    99 hey madhu
   100 In [15]: wish(name='vattam', greetings = 'get lost')
   100 In [15]: wish(name='vattam', greetings = 'bye bye')
   101 get lost vattam
   101 bye bye vattam
   102 \end{lstlisting}
   102 \end{lstlisting}
   103 % sorry Vattam just a joke :P
   103 % sorry Vattam just a joke :P
   104 \section{Self contained python script}
   104 \section{Self contained python script}
   105 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.
   106   \begin{lstlisting}
   106   \begin{lstlisting}
   128 \section{objects}
   128 \section{objects}
   129 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. 
   130 \begin{lstlisting}
   130 \begin{lstlisting}
   131 In []: a = str() # initializing a string object.
   131 In []: a = str() # initializing a string object.
   132 In []: b = "Hello World"
   132 In []: b = "Hello World"
   133 In []: b.split() # calling funciton on object 'b'
   133 In []: b.split() # calling function on object 'b'
   134 Out[]: ['Hello', 'World']
   134 Out[]: ['Hello', 'World']
   135 \end{lstlisting}
   135 \end{lstlisting}
   136 ``.'' is a operator used to call functions defined for given object.
   136 ``.'' is a operator used to call functions defined for given object.
   137 \section{Links and References}
   137 \section{Links and References}
   138 \begin{itemize}
   138 \begin{itemize}
   139 \item Some of inbult functions available with Python are listed at\\ \url{http://docs.python.org/library/functions.html}
   139 \item Some of inbuilt functions available with Python are listed at\\ \url{http://docs.python.org/library/functions.html}
   140 \item Reference manual to describe the standard libraries  that are distributed with Python is available at \url{http://docs.python.org/library/} 
   140 \item Reference manual to describe the standard libraries  that are distributed with Python is available at \url{http://docs.python.org/library/} 
   141 \end{itemize}
   141 \end{itemize}
   142 \end{document}
   142 \end{document}