day2/cheatsheet4.tex
changeset 329 0a6ab1d81491
parent 327 c78cad28c2f7
equal deleted inserted replaced
328:4075482a9770 329:0a6ab1d81491
    20 
    20 
    21 \begin{document}
    21 \begin{document}
    22 \date{}
    22 \date{}
    23 \vspace{-1in}
    23 \vspace{-1in}
    24 \begin{center}
    24 \begin{center}
    25 \LARGE{Python: Python Development}\\
    25 \LARGE{Python Development}\\
    26 \large{FOSSEE}
    26 \large{FOSSEE}
    27 \end{center}
    27 \end{center}
    28 \section{Module}
    28 \section{Module}
    29 Packages like \typ{scipy}, \typ{pylab} etc we used for functions like \typ{plot} are Modules. Modules are Python script, which have various functions and objects, which if imported can be reused. 
    29 Packages like \typ{scipy}, \typ{pylab} etc we used for functions like \typ{plot}, \typ{linspace} are \textbf{Modules}. They are Python script, which have various functions and objects, which can be imported and reused. 
    30 \begin{lstlisting}
    30 \begin{lstlisting}
    31 def gcd(a, b):
    31 def gcd(a, b):
    32   if a % b == 0: 
    32   if a % b == 0: 
    33     return b
    33     return b
    34   return gcd(b, a%b)
    34   return gcd(b, a%b)
    37 print gcd(16, 76)
    37 print gcd(16, 76)
    38 \end{lstlisting}
    38 \end{lstlisting}
    39 Save above mentioned python script with name 'gcd.py'. Now we can \typ{import} \typ{gcd} function. For example, in same directory create 'lcm.py' with following content:
    39 Save above mentioned python script with name 'gcd.py'. Now we can \typ{import} \typ{gcd} function. For example, in same directory create 'lcm.py' with following content:
    40 \begin{lstlisting}
    40 \begin{lstlisting}
    41 from gcd import gcd    
    41 from gcd import gcd    
       
    42 
    42 def lcm(a, b):
    43 def lcm(a, b):
    43   return (a * b) / gcd(a, b)
    44   return (a * b) / gcd(a, b)
    44     
    45     
    45 print lcm(14, 56)
    46 print lcm(14, 56)
    46 \end{lstlisting}
    47 \end{lstlisting}
    61   return gcd(b, a%b)
    62   return gcd(b, a%b)
    62 if __name__ == '__main__':
    63 if __name__ == '__main__':
    63   print gcd(15, 65)
    64   print gcd(15, 65)
    64   print gcd(16, 76)
    65   print gcd(16, 76)
    65 \end{lstlisting}
    66 \end{lstlisting}
    66 This \typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence
    67 \typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence
    67 \begin{lstlisting}
    68 \begin{lstlisting}
    68 $ python gcd.py
    69 $ python gcd.py
    69 5
    70 5
    70 4
    71 4
    71 $ python lcm.py 
    72 $ python lcm.py