day2/session3.tex
branchscipyin2010
changeset 451 db7b23465572
parent 431 9126059d6b37
child 452 f9417abb23a6
equal deleted inserted replaced
450:80028e4eee3d 451:db7b23465572
    76 \title[Basic Python]{Python language: Functions, modules and objects}
    76 \title[Basic Python]{Python language: Functions, modules and objects}
    77 
    77 
    78 \author[FOSSEE Team] {The FOSSEE Group}
    78 \author[FOSSEE Team] {The FOSSEE Group}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {1 May, 2010\\Day 2, Session 3}
    81 \date[] {SciPy.in 2010, Tutorials}
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    83 
    83 
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    85 %\logo{\pgfuseimage{iitmlogo}}
    85 %\logo{\pgfuseimage{iitmlogo}}
    86 
    86 
   122   \tableofcontents
   122   \tableofcontents
   123   % You might wish to add the option [pausesections]
   123   % You might wish to add the option [pausesections]
   124 \end{frame}
   124 \end{frame}
   125 
   125 
   126 \section{Functions}
   126 \section{Functions}
       
   127 
       
   128 \begin{frame}[fragile]
       
   129   \frametitle{Functions}
       
   130   \begin{itemize}
       
   131     \item \kwrd{def} - keyword to define a function
       
   132     \item Arguments are local to a function
       
   133     \item Functions can return multiple values
       
   134   \end{itemize}
       
   135 \end{frame}
       
   136 
       
   137 \begin{frame}[fragile]
       
   138   \frametitle{Functions: example}
       
   139   \begin{lstlisting}
       
   140 def signum( r ):
       
   141     """returns 0 if r is zero
       
   142     -1 if r is negative
       
   143     +1 if r is positive"""
       
   144     if r < 0:
       
   145         return -1
       
   146     elif r > 0:
       
   147         return 1
       
   148     else:
       
   149         return 0
       
   150   \end{lstlisting}
       
   151   \emphbar{Note docstrings}
       
   152 \end{frame}
       
   153 
       
   154 \begin{frame}[fragile]
       
   155   \frametitle {What does this function do?}
       
   156   \begin{lstlisting}
       
   157 def what( n ):
       
   158     if n < 0: n = -n
       
   159     while n > 0:
       
   160         if n % 2 == 1:
       
   161             return False
       
   162         n /= 10
       
   163     return True
       
   164   \end{lstlisting}
       
   165 \end{frame} 
       
   166 
       
   167 \begin{frame}[fragile]
       
   168   {What does this function do?}
       
   169 \begin{lstlisting}
       
   170 def what( n ):
       
   171     i = 1
       
   172     while i * i < n:
       
   173         i += 1
       
   174     return i * i == n, i
       
   175   \end{lstlisting}
       
   176 \end{frame}
       
   177 
       
   178 \begin{frame}[fragile]
       
   179   \frametitle {What does this function do?}
       
   180   \begin{lstlisting}
       
   181 def what( x, n ):
       
   182     if n < 0: 
       
   183        n = -n
       
   184        x = 1.0 / x
       
   185 
       
   186     z = 1.0
       
   187     while n > 0:
       
   188         if n % 2 == 1:
       
   189             z *= x
       
   190         x *= x
       
   191         n /= 2
       
   192     return z
       
   193   \end{lstlisting}
       
   194 \end{frame} 
       
   195 
   127 \subsection{Default arguments}
   196 \subsection{Default arguments}
   128 \begin{frame}[fragile]
   197 \begin{frame}[fragile]
   129   \frametitle{Functions: default arguments}
   198   \frametitle{Functions: default arguments}
   130   \begin{lstlisting}
   199   \begin{lstlisting}
   131 In []: greet = 'hello world'
   200 In []: greet = 'hello world'