diff options
author | shantanu | 2009-12-08 13:06:14 +0530 |
---|---|---|
committer | shantanu | 2009-12-08 13:06:14 +0530 |
commit | b3821a5ee2fd2b655873c93ad78cd15e899decac (patch) | |
tree | 1483fbad41fd8814765df84dcd49678628642f68 /day2 | |
parent | c62aa7f245f37a76d6ada696419fd492da38b263 (diff) | |
download | workshops-more-scipy-b3821a5ee2fd2b655873c93ad78cd15e899decac.tar.gz workshops-more-scipy-b3821a5ee2fd2b655873c93ad78cd15e899decac.tar.bz2 workshops-more-scipy-b3821a5ee2fd2b655873c93ad78cd15e899decac.zip |
Some more changes for day 2 sessions.
Diffstat (limited to 'day2')
-rwxr-xr-x | day2/cheatsheet1.tex | 2 | ||||
-rw-r--r-- | day2/cheatsheet2.tex | 21 | ||||
-rw-r--r-- | day2/cheatsheet3.tex | 34 | ||||
-rw-r--r-- | day2/cheatsheet4.tex | 7 |
4 files changed, 33 insertions, 31 deletions
diff --git a/day2/cheatsheet1.tex b/day2/cheatsheet1.tex index ca1f38a..22289b3 100755 --- a/day2/cheatsheet1.tex +++ b/day2/cheatsheet1.tex @@ -134,7 +134,7 @@ In []: s = 'Hello' 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 --git a/day2/cheatsheet2.tex b/day2/cheatsheet2.tex index 01da564..bb5762a 100644 --- a/day2/cheatsheet2.tex +++ b/day2/cheatsheet2.tex @@ -40,23 +40,23 @@ while condition: 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 @@ IndexError: list index out of range 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 @@ Start slice from second element(1), till the last element(-1) with step size of 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 @@ 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} +\textbf{Note:} Duplicate keys are overwritten! \subsection{containership} \begin{lstlisting} In []: 'Inn' in player @@ -242,7 +242,7 @@ Out[]: True 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 @@ In []: set([2,3]) < p # Test for subset Out[]: True \end{lstlisting} \end{document} - diff --git a/day2/cheatsheet3.tex b/day2/cheatsheet3.tex index f29f611..293b95e 100644 --- a/day2/cheatsheet3.tex +++ b/day2/cheatsheet3.tex @@ -26,16 +26,18 @@ showstringspaces=false \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 @@ 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. +\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 = '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. +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 @@ hey madhu 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 @@ 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: +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 --git a/day2/cheatsheet4.tex b/day2/cheatsheet4.tex index da4e187..ae7ff90 100644 --- a/day2/cheatsheet4.tex +++ b/day2/cheatsheet4.tex @@ -22,11 +22,11 @@ showstringspaces=false \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 @@ print gcd(16, 76) 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 @@ 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 +\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 |