diff options
Diffstat (limited to 'day2')
-rwxr-xr-x | day2/cheatsheet1.tex | 3 | ||||
-rw-r--r-- | day2/cheatsheet2.tex | 52 | ||||
-rw-r--r-- | day2/cheatsheet3.tex | 89 | ||||
-rw-r--r-- | day2/cheatsheet4.tex | 74 |
4 files changed, 209 insertions, 9 deletions
diff --git a/day2/cheatsheet1.tex b/day2/cheatsheet1.tex index a0d100b..ca1f38a 100755 --- a/day2/cheatsheet1.tex +++ b/day2/cheatsheet1.tex @@ -74,7 +74,8 @@ Out[]: e 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 diff --git a/day2/cheatsheet2.tex b/day2/cheatsheet2.tex index 05f248d..01da564 100644 --- a/day2/cheatsheet2.tex +++ b/day2/cheatsheet2.tex @@ -201,7 +201,57 @@ In []: t[4] = 7 # ERROR: tuples are immutable \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: diff --git a/day2/cheatsheet3.tex b/day2/cheatsheet3.tex index 8f5a6f1..f29f611 100644 --- a/day2/cheatsheet3.tex +++ b/day2/cheatsheet3.tex @@ -25,8 +25,9 @@ showstringspaces=false \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 @@ def signum( r ): 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} +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} -Keyword Arguments +\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} +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} -Self contained python script +\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 @@ annotate('origin', xy = (0, 0)) 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} diff --git a/day2/cheatsheet4.tex b/day2/cheatsheet4.tex new file mode 100644 index 0000000..da4e187 --- /dev/null +++ b/day2/cheatsheet4.tex @@ -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} |