modifeid cheat sheets for session 3, 4 day 2.
authorShantanu <shantanu@fossee.in>
Wed, 25 Nov 2009 12:11:03 +0530 (2009-11-25)
changeset 327 c78cad28c2f7
parent 326 f57f42861770
child 328 4075482a9770
modifeid cheat sheets for session 3, 4 day 2.
day2/cheatsheet1.tex
day2/cheatsheet2.tex
day2/cheatsheet3.tex
day2/cheatsheet4.tex
--- a/day2/cheatsheet1.tex	Tue Nov 24 11:00:52 2009 +0530
+++ b/day2/cheatsheet1.tex	Wed Nov 25 12:11:03 2009 +0530
@@ -74,7 +74,8 @@
 In []: print w[-1] #last character of string
 Out[]: o
   \end{lstlisting}
-\textbf{Note:} For a string variable, individual elements can be accessed using indices.
+\textbf{Note:} For a string variable, individual elements can be accessed using indices.\\
+\textbf{Note:} All slicing and striding operations works with strings as well.
   \begin{lstlisting}
 In []: len(w) #function to calculate length of string
 Out[]: 5
--- a/day2/cheatsheet2.tex	Tue Nov 24 11:00:52 2009 +0530
+++ b/day2/cheatsheet2.tex	Wed Nov 25 12:11:03 2009 +0530
@@ -201,7 +201,57 @@
 \end{lstlisting}
 \textbf{Note:} elements cant be changed!
 \section{Dictionaries}
-statistics section? 
+Dictionaries are data structures that provide key-value mappings. They are similar to lists except that instead of the values having integer indexes, they have keys or strings as indexes.\\
+A simple dictionary can be created by:
+\begin{lstlisting}
+In []: player = {'Mat': 134,'Inn': 233,
+          'Runs': 10823, 'Avg': 52.53}
+\end{lstlisting}
+For above case, value on left of ':' is key and value on right is corresponding value. To retrieve value related to key 'Avg'
+\begin{lstlisting}
+In []: player['Avg']
+Out[]: 52.530000000000001
+\end{lstlisting}
+\subsection{Element operations}
+\begin{lstlisting}
+In []: player['Name'] = 'Rahul Dravid' #Adds new key-value pair.
+In []: player
+Out[]: 
+{'Avg': 52.530000000000001,
+ 'Inn': 233,
+ 'Mat': 134,
+ 'Name': 'Rahul Dravid',
+ 'Runs': 10823}
+In []: player.pop('Mat') # removing particular key-value pair
+Out[]: 134
+In [21]: player
+Out[21]: {'Avg': 52.530000000000001, 'Inn': 233, 
+          'Name': 'Rahul Dravid', 'Runs': 10823}
+\end{lstlisting}
+\textbf{Note:} Duplicate keys are overwritten!\\
+\begin{lstlisting}
+In []: player['Name'] = 'Dravid'
+In []: player
+Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, 
+          'Name': 'Dravid', 'Runs': 10823}
+\end{lstlisting}
+\subsection{containership}
+\begin{lstlisting}
+In []: 'Inn' in player
+Out[]: True
+In []: 'Econ' in player
+Out[]: False
+\end{lstlisting}
+\textbf{Note:} Containership is always checked on 'keys' of dictionary but not 'values'.\\
+\subsection{Methods}
+\begin{lstlisting}
+In []: player.keys() # returns list of all keys
+Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
+
+In []: player.values() # returns list of all values.
+Out[]: [10823, 233, 
+        52.530000000000001, 134]  
+\end{lstlisting}
 \section{Sets}
 are an unordered collection of unique elements.\\
 Creation:
--- a/day2/cheatsheet3.tex	Tue Nov 24 11:00:52 2009 +0530
+++ b/day2/cheatsheet3.tex	Wed Nov 25 12:11:03 2009 +0530
@@ -25,8 +25,9 @@
 \LARGE{Python: Functions and Objects}\\
 \large{FOSSEE}
 \end{center}
-\section{Functions}
-Function definition
+\section{Function}
+They allows us to enclose a set of statements and call the function again and again instead of repeating the group of statements every-time. 
+\subsection{Function definition}
   \begin{lstlisting}
 def signum( r ):    
     if r < 0:
@@ -36,16 +37,69 @@
     else:
         return 0
   \end{lstlisting}
-Default Arguments 
+%\typ{def} is a keyword, which is used to define a function with given name.
+\subsection{Usage}
+\begin{lstlisting}
+In []: signum(4)
+Out[]: 1
+In []: signum(0)
+Out[]: 0
+In []: signum(-4)
+Out[]: -1
+In []: signum() # ERROR signum() takes exactly 1 argument(0 given)
+\end{lstlisting}
+\textbf{Note:} Arguments passed to a function are passed by-value \textbf{only if} they are basic Python data type(int, float). In case one pass immutable types(String, tupels), they cant be modified in the function, but objects like \typ{list} and dictionary can be manipulated.
+\subsection{Default Arguments}
+This feature allow the functions to take the arguments optionally. For example:
+\begin{lstlisting}
+In []: greet = 'hello world'
+
+In []: greet.split()
+Out[]: ['hello', 'world']
+\end{lstlisting}
+In above case, default argument which \typ{split} function uses is a blank space. One can pass argument also, to split the string for a different delimiter.
+\begin{lstlisting}
+In []: line = 'Rossum, Guido, 54, 46, 55'
+
+In []: line.split(',') #split with ','
+Out[]: ['Rossum', ' Guido', ' 54',
+                        ' 46', ' 55']
+\end{lstlisting}
+Function to work with default argument can be defined as:
 \begin{lstlisting}
 def welcome(greet, name='world!'):
-    print greet, name
+  print greet, name
 \end{lstlisting}
-Keyword Arguments
+above function can be used as:
+\begin{lstlisting}
+In []: welcome("Hello") #using default argument
+Hello World!
+In []: welcome("Hi", "Guido") #taking name via argument
+Hi Guido
+\end{lstlisting}
+\subsection{Keyword Arguments}
+This feature provides the facility of passing arguments by specifying the name of the parameter as defined in the function definition. You dont have to remember the order of the parameters in function definition. For example:
 \begin{lstlisting}
 In []: plot(y, sin(y), 'g', linewidth=2)
+In []: plot(y, cos(y), linewidth=1, color='g')
 \end{lstlisting}
-Self contained python script
+Both call to \typ{plot} function will work and paramenters are set accordingly.\\
+One can define a function such that keyword arguments can be used in following way:
+\begin{lstlisting}
+def wish(name='World', greetings='Hello'):
+  print greetings, name
+\end{lstlisting}
+This function can be called as:
+\begin{lstlisting}
+In [13]: wish() #default arguments will work
+Hello World
+In [14]: wish(greetings='hey', name='madhu')
+hey madhu
+In [15]: wish(name='vattam', greetings = 'get lost')
+get lost vattam
+\end{lstlisting}
+\section{Self contained python script}
+Functions like \typ{plot}, \typ{linspace} etc are not inbuilt functions. One have to import them to use them.
   \begin{lstlisting}
 from scipy import linspace, pi, sin
 from pylab import plot, legend, annotate
@@ -61,5 +115,26 @@
 xlim(-5*pi, 5*pi)
 ylim(-5*pi, 5*pi)
   \end{lstlisting}
-
+Above mentioned code will work with following setup:
+\begin{lstlisting}
+$ ipython -pylab
+In []: %run -i sine_plot.py
+\end{lstlisting} %$
+as we are already including \typ{pylab} into \typ{ipython}. But to make it work independently so that even\\
+\typ{$ python sine_plot.py} \\ %$ 
+works, one will have to use \typ{import} statements.\\
+\section{objects}
+In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects. 
+\begin{lstlisting}
+In []: a = str() 
+In []: b = "Hello World"
+In []: b.split()
+Out[]: ['Hello', 'World']
+\end{lstlisting}
+``.'' is a operator used to call functions defined for given object.
+\section{Links and References}
+\begin{itemize}
+\item Some of inbult functions available with Python are listed at\\ \url{http://docs.python.org/library/functions.html}
+\item Reference manual to describe the standard libraries  that are distributed with Python is available at \url{http://docs.python.org/library/} 
+\end{itemize}
 \end{document}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day2/cheatsheet4.tex	Wed Nov 25 12:11:03 2009 +0530
@@ -0,0 +1,74 @@
+\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: Python Development}\\
+\large{FOSSEE}
+\end{center}
+\section{Module}
+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. 
+\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}
+This \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}