day2/cheatsheet4.tex
author Shantanu <shantanu@fossee.in>
Wed, 25 Nov 2009 12:11:03 +0530
changeset 327 c78cad28c2f7
child 329 0a6ab1d81491
permissions -rw-r--r--
modifeid cheat sheets for session 3, 4 day 2.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
327
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
\documentclass[12pt]{article}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
\title{Python: Data Structures}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
\author{FOSSEE}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
\usepackage{listings}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
\lstset{language=Python,
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
    basicstyle=\ttfamily,
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
commentstyle=\itshape\bfseries, 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
showstringspaces=false
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
\newcommand{\typ}[1]{\lstinline{#1}}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
\usepackage[english]{babel}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
\usepackage[latin1]{inputenc}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
\usepackage{times}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
\usepackage[T1]{fontenc}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
\usepackage{ae,aecompl}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
\usepackage{mathpazo,courier,euler}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
\usepackage[scaled=.95]{helvet}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
\begin{document}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
\date{}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
\vspace{-1in}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
\begin{center}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
\LARGE{Python: Python Development}\\
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
\large{FOSSEE}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
\end{center}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
\section{Module}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    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. 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
def gcd(a, b):
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
  if a % b == 0: 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
    return b
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
  return gcd(b, a%b)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
print gcd(15, 65)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
print gcd(16, 76)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    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:
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
from gcd import gcd    
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
def lcm(a, b):
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
  return (a * b) / gcd(a, b)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
    
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
print lcm(14, 56)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
Here since both gcd.py and lcm.py are in same directory, import statement imports \typ{gcd} function from gcd.py.\\
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
When you try to run lcm.py it prints three results, two from gcd.py and third from lcm.py.
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
$ python lcm.py
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
5
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
4
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
56
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
\end{lstlisting} %$
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
\newpage
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    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__'} \
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
def gcd(a, b):
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
  if a % b == 0: 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
    return b
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
  return gcd(b, a%b)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
if __name__ == '__main__':
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
  print gcd(15, 65)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
  print gcd(16, 76)
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
This \typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
$ python gcd.py
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
5
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
4
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
$ python lcm.py 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
56
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
\end{document}