diff options
author | Madhusudan.C.S | 2009-12-29 19:25:11 +0530 |
---|---|---|
committer | Madhusudan.C.S | 2009-12-29 19:25:11 +0530 |
commit | f38b9113f14dfe1421cddfaf938759408bb43eea (patch) | |
tree | b56c4d36777394e8472dc8df0c4cb113f5ab6f6e /day2/cheatsheet4.tex | |
parent | 4921db12e2b0ba6d59cea1619afd791c9e20033f (diff) | |
parent | 20f85fd6a02c29533c92c631aac2ebaba69fd82e (diff) | |
download | workshops-more-scipy-f38b9113f14dfe1421cddfaf938759408bb43eea.tar.gz workshops-more-scipy-f38b9113f14dfe1421cddfaf938759408bb43eea.tar.bz2 workshops-more-scipy-f38b9113f14dfe1421cddfaf938759408bb43eea.zip |
Merged the branches.
Diffstat (limited to 'day2/cheatsheet4.tex')
-rw-r--r-- | day2/cheatsheet4.tex | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/day2/cheatsheet4.tex b/day2/cheatsheet4.tex new file mode 100644 index 0000000..ae7ff90 --- /dev/null +++ b/day2/cheatsheet4.tex @@ -0,0 +1,75 @@ +\documentclass[12pt]{article} + + +\title{Python: Data Structures} +\author{FOSSEE} +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, +commentstyle=\itshape\bfseries, +showstringspaces=false +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\begin{document} +\date{} +\vspace{-1in} +\begin{center} +\LARGE{Python Development}\\ +\large{FOSSEE} +\end{center} +\section{Module} +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. +\begin{lstlisting} +def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) + +print gcd(15, 65) +print gcd(16, 76) +\end{lstlisting} +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: +\begin{lstlisting} +from gcd import gcd + +def lcm(a, b): + return (a * b) / gcd(a, b) + +print lcm(14, 56) +\end{lstlisting} +Here since both gcd.py and lcm.py are in same directory, import statement imports \typ{gcd} function from gcd.py.\\ +When you try to run lcm.py it prints three results, two from gcd.py and third from lcm.py. +\begin{lstlisting} +$ python lcm.py +5 +4 +56 +\end{lstlisting} %$ +\newpage +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__'} \ +\begin{lstlisting} +def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) +if __name__ == '__main__': + print gcd(15, 65) + print gcd(16, 76) +\end{lstlisting} +\typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence +\begin{lstlisting} +$ python gcd.py +5 +4 +$ python lcm.py +56 +\end{lstlisting} +\end{document} |