day2/cheatsheet4.tex
changeset 334 2214b5dba4d4
parent 329 0a6ab1d81491
equal deleted inserted replaced
333:25b18b51be41 334:2214b5dba4d4
       
     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 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}, \typ{linspace} are \textbf{Modules}. They are Python script, which have various functions and objects, which can be imported and 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 
       
    43 def lcm(a, b):
       
    44   return (a * b) / gcd(a, b)
       
    45     
       
    46 print lcm(14, 56)
       
    47 \end{lstlisting}
       
    48 Here since both gcd.py and lcm.py are in same directory, import statement imports \typ{gcd} function from gcd.py.\\
       
    49 When you try to run lcm.py it prints three results, two from gcd.py and third from lcm.py.
       
    50 \begin{lstlisting}
       
    51 $ python lcm.py
       
    52 5
       
    53 4
       
    54 56
       
    55 \end{lstlisting} %$
       
    56 \newpage
       
    57 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__'} \
       
    58 \begin{lstlisting}
       
    59 def gcd(a, b):
       
    60   if a % b == 0: 
       
    61     return b
       
    62   return gcd(b, a%b)
       
    63 if __name__ == '__main__':
       
    64   print gcd(15, 65)
       
    65   print gcd(16, 76)
       
    66 \end{lstlisting}
       
    67 \typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence
       
    68 \begin{lstlisting}
       
    69 $ python gcd.py
       
    70 5
       
    71 4
       
    72 $ python lcm.py 
       
    73 56
       
    74 \end{lstlisting}
       
    75 \end{document}