day2/cheatsheet4.tex
changeset 327 c78cad28c2f7
child 329 0a6ab1d81491
equal deleted inserted replaced
326:f57f42861770 327:c78cad28c2f7
       
     1 \documentclass[12pt]{article}
       
     2 
       
     3 
       
     4 \title{Python: Data Structures}
       
     5 \author{FOSSEE}
       
     6 \usepackage{listings}
       
     7 \lstset{language=Python,
       
     8     basicstyle=\ttfamily,
       
     9 commentstyle=\itshape\bfseries, 
       
    10 showstringspaces=false
       
    11 }
       
    12 \newcommand{\typ}[1]{\lstinline{#1}}
       
    13 \usepackage[english]{babel}
       
    14 \usepackage[latin1]{inputenc}
       
    15 \usepackage{times}
       
    16 \usepackage[T1]{fontenc}
       
    17 \usepackage{ae,aecompl}
       
    18 \usepackage{mathpazo,courier,euler}
       
    19 \usepackage[scaled=.95]{helvet}
       
    20 
       
    21 \begin{document}
       
    22 \date{}
       
    23 \vspace{-1in}
       
    24 \begin{center}
       
    25 \LARGE{Python: Python Development}\\
       
    26 \large{FOSSEE}
       
    27 \end{center}
       
    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. 
       
    30 \begin{lstlisting}
       
    31 def gcd(a, b):
       
    32   if a % b == 0: 
       
    33     return b
       
    34   return gcd(b, a%b)
       
    35 
       
    36 print gcd(15, 65)
       
    37 print gcd(16, 76)
       
    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:
       
    40 \begin{lstlisting}
       
    41 from gcd import gcd    
       
    42 def lcm(a, b):
       
    43   return (a * b) / gcd(a, b)
       
    44     
       
    45 print lcm(14, 56)
       
    46 \end{lstlisting}
       
    47 Here since both gcd.py and lcm.py are in same directory, import statement imports \typ{gcd} function from gcd.py.\\
       
    48 When you try to run lcm.py it prints three results, two from gcd.py and third from lcm.py.
       
    49 \begin{lstlisting}
       
    50 $ python lcm.py
       
    51 5
       
    52 4
       
    53 56
       
    54 \end{lstlisting} %$
       
    55 \newpage
       
    56 We have print statements to make sure \typ{gcd} and \typ{lcm} are working properly. So to suppress output of \typ{gcd} module when imported in lcm.py we use \typ{'__main__'} \
       
    57 \begin{lstlisting}
       
    58 def gcd(a, b):
       
    59   if a % b == 0: 
       
    60     return b
       
    61   return gcd(b, a%b)
       
    62 if __name__ == '__main__':
       
    63   print gcd(15, 65)
       
    64   print gcd(16, 76)
       
    65 \end{lstlisting}
       
    66 This \typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence
       
    67 \begin{lstlisting}
       
    68 $ python gcd.py
       
    69 5
       
    70 4
       
    71 $ python lcm.py 
       
    72 56
       
    73 \end{lstlisting}
       
    74 \end{document}