# HG changeset patch # User shantanu # Date 1260257774 -19800 # Node ID 0a6ab1d81491f0e1ca626c52a0b643bcb796bb13 # Parent 4075482a97701c3af7dbc4071a13453cd847ef21 Some more changes for day 2 sessions. diff -r 4075482a9770 -r 0a6ab1d81491 day2/cheatsheet1.tex --- a/day2/cheatsheet1.tex Tue Dec 08 12:40:07 2009 +0530 +++ b/day2/cheatsheet1.tex Tue Dec 08 13:06:14 2009 +0530 @@ -134,7 +134,7 @@ In []: p = 'World' In []: s + p #concatenating two strings Out[]: 'HelloWorld' -In []: s * 4 #repeating string for given num of times +In []: s * 4 #repeat string for given number of times Out[]: 'HelloHelloHelloHello' \end{lstlisting} \subsection{Relational and Logical Operators} diff -r 4075482a9770 -r 0a6ab1d81491 day2/cheatsheet2.tex --- a/day2/cheatsheet2.tex Tue Dec 08 12:40:07 2009 +0530 +++ b/day2/cheatsheet2.tex Tue Dec 08 13:06:14 2009 +0530 @@ -40,23 +40,23 @@ statement1 statement2 \end{lstlisting} -All the statements are executed, till the condition statement evaluates to True. +All statements are executed, till the condition statement evaluates to True. \subsection{\typ{for} and \typ{range}} \typ{range(start, stop, step)}\\ returns a list containing an arithmetic progression of integers.\\ Of the arguments mentioned above, both start and step are optional.\\ -For example, if we skip third argument, i.e \typ{step}, then default value is 1. So: +For example, if we skip third argument, i.e \typ{step}, default is taken as 1. So: \begin{lstlisting} In []: range(1,10) Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9] \end{lstlisting} \textbf{Note:} stop value is not included in the list.\\ -Similarly if we don't pass \typ{start} argument, default is taken to be 0. +Similarly if we don't pass \typ{first} argument (in this case \typ{start}), default is taken to be 0. \begin{lstlisting} In []: range(10) Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] \end{lstlisting} -In case \typ{step} is mentioned, the jump between consecutive members of the list would be equal to that. +In case third argument is mentioned(\typ{step}), the jump between consecutive members of the list would be equal to that. \begin{lstlisting} In []: range(1,10,2) Out[]: [1, 3, 5, 7, 9] @@ -94,14 +94,14 @@ In []: num[-1] Out[]: 4 \end{lstlisting} -\textbf{Note: }\typ{-1} points the last element in a list. Similarly to access third last element of a list one can use: +\textbf{Note: }\typ{-1} points to last element in a list. Similarly to access third last element of a list one can use: \begin{lstlisting} In []: num[-3] Out[]: 2 \end{lstlisting} -\subsection{Adding lists and elements} +\subsection{\typ{list} operations} \begin{lstlisting} -In []: num += [9, 10, 11] # Adding two lists +In []: num += [9, 10, 11] # Concatenating two lists In []: num Out[]: [1, 2, 3, 4, 9, 10, 11] \end{lstlisting} @@ -162,7 +162,7 @@ In []: a[::2] Out[]: [1, 3, 5] \end{lstlisting} -Start from beginning(since \typ{initial} is blank), till last of list(this time last element is included, as \typ{final} is blank), with step size of 2.\\ +Start from beginning(since \typ{initial} is blank), till last(this time last element is included, as \typ{final} is blank), with step size of 2.\\ Apart from using \typ{reverse} command on list, one can also use slicing in special way to get reverse of a list. \begin{lstlisting} In []: a[-1:-4:-1] @@ -228,13 +228,13 @@ 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} +\textbf{Note:} Duplicate keys are overwritten! \subsection{containership} \begin{lstlisting} In []: 'Inn' in player @@ -242,7 +242,7 @@ In []: 'Econ' in player Out[]: False \end{lstlisting} -\textbf{Note:} Containership is always checked on 'keys' of dictionary but not 'values'.\\ +\textbf{Note:} Containership is always checked on 'keys' of dictionary, never on 'values'.\\ \subsection{Methods} \begin{lstlisting} In []: player.keys() # returns list of all keys @@ -279,4 +279,3 @@ Out[]: True \end{lstlisting} \end{document} - diff -r 4075482a9770 -r 0a6ab1d81491 day2/cheatsheet3.tex --- a/day2/cheatsheet3.tex Tue Dec 08 12:40:07 2009 +0530 +++ b/day2/cheatsheet3.tex Tue Dec 08 13:06:14 2009 +0530 @@ -26,16 +26,18 @@ \large{FOSSEE} \end{center} \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. +They allows us to enclose a set of statements and call them again and again instead of repeating the group of statements every-time. \subsection{Function definition} \begin{lstlisting} -def signum( r ): - if r < 0: - return -1 - elif r > 0: - return 1 - else: - return 0 +In []: def signum(r): + ...: if r < 0: + ...: return -1 + ...: elif r > 0: + ...: return 1 + ...: else: + ...: return 0 + ...: + ...: \end{lstlisting} %\typ{def} is a keyword, which is used to define a function with given name. \subsection{Usage} @@ -48,7 +50,7 @@ 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. +\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 passes 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} @@ -57,7 +59,7 @@ 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. +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' @@ -98,6 +100,7 @@ In [15]: wish(name='vattam', greetings = 'get lost') get lost vattam \end{lstlisting} +% sorry Vattam just a joke :P \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} @@ -115,20 +118,19 @@ xlim(-5*pi, 5*pi) ylim(-5*pi, 5*pi) \end{lstlisting} -Above mentioned code will work with following setup: +These import statements are necessary to make program self contained. After importing, we can run script via:\\ +\typ{$ python sine_plot.py} \\ %$ +We no longer need: \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 []: a = str() # initializing a string object. In []: b = "Hello World" -In []: b.split() +In []: b.split() # calling funciton on object 'b' Out[]: ['Hello', 'World'] \end{lstlisting} ``.'' is a operator used to call functions defined for given object. diff -r 4075482a9770 -r 0a6ab1d81491 day2/cheatsheet4.tex --- a/day2/cheatsheet4.tex Tue Dec 08 12:40:07 2009 +0530 +++ b/day2/cheatsheet4.tex Tue Dec 08 13:06:14 2009 +0530 @@ -22,11 +22,11 @@ \date{} \vspace{-1in} \begin{center} -\LARGE{Python: Python Development}\\ +\LARGE{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. +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: @@ -39,6 +39,7 @@ 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) @@ -63,7 +64,7 @@ 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 +\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