diff options
Diffstat (limited to 'basic_python/slides')
-rw-r--r-- | basic_python/slides/func.tex | 294 | ||||
-rw-r--r-- | basic_python/slides/intro.tex | 477 | ||||
-rw-r--r-- | basic_python/slides/io_files_parsing.tex | 239 | ||||
-rw-r--r-- | basic_python/slides/strings_loops_lists.tex | 457 | ||||
-rw-r--r-- | basic_python/slides/tmp.tex | 1 | ||||
-rw-r--r-- | basic_python/slides/tuples_dicts_sets.tex | 258 |
6 files changed, 0 insertions, 1726 deletions
diff --git a/basic_python/slides/func.tex b/basic_python/slides/func.tex deleted file mode 100644 index b66be06..0000000 --- a/basic_python/slides/func.tex +++ /dev/null @@ -1,294 +0,0 @@ -\section{Functions} - -\begin{frame}[fragile] - \frametitle{Abstracting} - \begin{itemize} - \item Reduce duplication of code - \item Fewer lines of code and hence lesser scope for bugs - \item Re-usability of code, that's already been written - \item Use functions written by others, without exactly knowing how - they do, what they are doing - \item \alert{Enter Functions!} - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Defining functions} - \begin{itemize} - \item Consider the function \texttt{f(x) = x\textasciicircum{}2} - \item Let's write a Python function, equivalent to this - \end{itemize} - \begin{lstlisting} - In[]: def f(x): - ....: return x*x - ....: - - In[]: f(1) - In[]: f(2) - \end{lstlisting} - \begin{itemize} - \item \texttt{def} is a keyword - \item \texttt{f} is the name of the function - \item \texttt{x} the parameter of the function - \item \texttt{return} is a keyword; specifies what should be - returned - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Defining functions \ldots} - \begin{lstlisting} - In[]: def greet(): - ....: print "Hello World!" - ....: - - In[]: greet() - \end{lstlisting} - \begin{itemize} - \item \texttt{greet} is a function that takes no arguments - \item Also, it is not returning anything explicitly - \item But implicitly, Python returns \texttt{None} - \end{itemize} - \begin{lstlisting} - In[]: def avg(a, b): - ....: return (a + b)/2 - ....: - - In[]: avg(12, 10) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Doc-strings} - \begin{itemize} - \item It's highly recommended that all functions have documentation - \item We write a doc-string along with the function definition - \end{itemize} - \begin{lstlisting} - In[]: def avg(a, b): - """ avg takes two numbers as input - and returns their average""" - - ....: return (a + b)/2 - ....: - - In[]: avg? - In[]: greet? - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Returning multiple values} - \begin{itemize} - \item Return area and perimeter of circle, given radius - \item Function needs to return two values - \end{itemize} - \begin{lstlisting} - In[]: def circle(r): - """returns area and perimeter of a - circle given, the radius r""" - - ....: pi = 3.14 - ....: area = pi * r * r - ....: perimeter = 2 * pi * r - ....: return area, perimeter - ....: - - In[]: circle(4) - In[]: a, p = circle(6) - In[]: print a - In[]: print p - \end{lstlisting} - \begin{itemize} - \item Any number of values can be returned - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{What? -- 1} - \begin{lstlisting} - In[]: def what( n ): - ....: if n < 0: n = -n - ....: while n > 0: - ....: if n % 2 == 1: - ....: return False - ....: n /= 10 - ....: return True - ....: - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{What? -- 2} - \begin{lstlisting} - In[]: def what( n ): - ....: i = 1 - ....: while i * i < n: - ....: i += 1 - ....: return i * i == n, i - ....: - \end{lstlisting} -\end{frame} - -\subsection*{Default \& Keyword Arguments} - -\begin{frame}[fragile] - \frametitle{Default arguments} - \begin{lstlisting} - In[]: round(2.484) - In[]: round(2.484, 2) - - In[]: s.split() # split on spaces - In[]: s.split(';') # split on ';' - - In[]: range(10) # returns numbers from 0 to 9 - In[]: range(1, 10) # returns numbers from 1 to 9 - In[]: range(1, 10, 2) # returns odd numbers from 1 to 9 - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Default arguments \ldots} - \begin{lstlisting} - In[]: def welcome(greet, name="World"): - ....: print greet, name - ....: - - In[]: welcome("Hi", "Guido") - In[]: welcome("Hello") - \end{lstlisting} - \begin{itemize} - \item Arguments with default values, should be placed at the end - \item The following definition is \alert{WRONG} - \end{itemize} - \begin{lstlisting} - In[]: def welcome(name="World", greet): - ....: print greet, name - ....: - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Keyword Arguments} - \begin{lstlisting} - In[]: def welcome(greet, name="World"): - ....: print greet, name - ....: - - In[]: welcome("Hello", "James") - - In[]: welcome("Hi", name="Guido") - - In[]: welcome(name="Guido", greet="Hey") - - In[]: welcome(name="Guido", "Hey") - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Built-in functions} - \begin{itemize} - \item Variety of built-in functions are available - \item \texttt{abs, any, all, len, max, min} - \item \texttt{pow, range, sum, type} - \item Refer here: - \url{http://docs.python.org/library/functions.html} - \end{itemize} -\end{frame} - -\subsection*{Variable Scope} - -\begin{frame}[fragile] - \frametitle{Arguments are local} - \begin{lstlisting} - In[]: def change(q): - ....: q = 10 - ....: print q - ....: - - In[]: change(1) - In[]: print q - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Variables inside function are local} - \begin{lstlisting} - In[]: n = 5 - In[]: def change(): - ....: n = 10 - ....: print n - ....: - In[]: change() - In[]: print n - \end{lstlisting} - \begin{itemize} - \item Use the \texttt{global} statement to assign to global variables - \end{itemize} - \begin{lstlisting} - In[]: def change(): - ....: global n - ....: n = 10 - ....: print n - ....: - In[]: change() - In[]: print n - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{global} - \begin{itemize} - \item Use the \texttt{global} statement to assign to global variables - \end{itemize} - \begin{lstlisting} - In[]: def change(): - ....: global n - ....: n = 10 - ....: print n - ....: - In[]: change() - In[]: print n - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Mutable variables} - \begin{itemize} - \item Behavior is different when assigning to a list element/slice - \item Python looks up for the name, from innermost scope outwards, - until the name is found - \end{itemize} - \begin{lstlisting} - In[]: name = ['Mr.', 'Steve', 'Gosling'] - In[]: def change_name(): - ....: name[0] = 'Dr.' - ....: - In[]: change_name() - In[]: print name - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Passing Arguments \ldots} - \begin{lstlisting} - In[]: n = 5 - In[]: def change(n): - ....: n = 10 - ....: print "n = %s inside change " %n - ....: - In[]: change(n) - In[]: print n - \end{lstlisting} - - \begin{lstlisting} - In[]: name = ['Mr.', 'Steve', 'Gosling'] - In[]: def change_name(n): - ....: n[0] = 'Dr.' - ....: print "n = %s inside change_name" %n - ....: - In[]: change_name(name) - In[]: print name - \end{lstlisting} -\end{frame} diff --git a/basic_python/slides/intro.tex b/basic_python/slides/intro.tex deleted file mode 100644 index 52be064..0000000 --- a/basic_python/slides/intro.tex +++ /dev/null @@ -1,477 +0,0 @@ -\section{The Language} -\begin{frame}[fragile] - \frametitle{Python!} - \begin{itemize} - \item Programming Language - \item Powerful, High-level, Interpreted, Multi-Platform - \item Elegant and highly readable syntax - \item Efficient high-level data structures - \end{itemize} - \begin{itemize} - \item Easy to learn - \item Allows to concentrate on the problem instead of the language - \item Increased Productivity - \end{itemize} - \begin{itemize} - \item Guido van Rossum -- BDFL - \item Conceived in December 1989 - \item Named after ``Monty Python's Flying Circus'', a 70s comedy - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Why Python?} - \begin{itemize} - \item Extremely readable; Forces programmers to write readable code. - \item Interactive; Offers a very fast edit-test-debug cycle. - \item Doesn't get in your way; High-level data structures let you - focus on the problem - \item Handles memory management - \item Batteries included; Huge standard library for wide range of - tasks. - \item Object-oriented. - \item C, C++ and FORTRAN interfacing allows use of legacy code - \item Your time is more valuable than machine time! - \end{itemize} -\end{frame} - -\section{The Interpreter} -\begin{frame}[fragile] - \frametitle{Python interpreter} - \begin{itemize} - \item Let's get our hands dirty! - \item Start Python from your shell - \end{itemize} - \lstset{language=sh} - \begin{lstlisting} - $ python - \end{lstlisting} %$ - \begin{lstlisting} -Python 2.7.1 (r271:86832, Feb 21 2011, 01:28:26) -[GCC 4.5.2 20110127 (prerelease)] on linux2 -Type "help", "copyright", "credits" or "license" for more information. ->>> - \end{lstlisting} - \begin{itemize} - \item First line shows Python version (2.7.1) - \item \verb+>>>+ the interpreter's prompt - \item The interpreter is ready and waiting for your command! - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Hello World!} - \begin{itemize} - \item Type\texttt{print `Hello World'} and hitting enter - \end{itemize} - \begin{lstlisting} - >>> print 'Hello, World!' - Hello, World! - \end{lstlisting} - \begin{itemize} - \item The interpreter prints out the words \emph{Hello World} - \end{itemize} - \begin{itemize} - \item Hit \texttt{Ctrl-D} to exit the interpreter - \item We shall look at IPython, an enhanced interpreter - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Versions} - Before moving on \ldots - \begin{itemize} - \item Currently has two stable branches or versions, 2.x and 3.x - \item 3.x is not backward compatible - \item 3.x is deemed to be the future of Python - \item But, we shall stick to 2.x for this course - \item The ecosystem around Python 2.x hasn't yet moved to 3.x - \end{itemize} -\end{frame} - -\subsection*{IPython} - -\begin{frame}[fragile] - \frametitle{Invoking IPython} - \begin{itemize} - \item An enhanced Python interpreter - \item Tab-completion, Easier access to help, Better history - \end{itemize} - \lstset{language=sh} - \begin{lstlisting} - $ ipython - \end{lstlisting} %$ - \alert{If \texttt{ipython is not installed}, you need to install it!} - \begin{itemize} - \item The prompt is \texttt{In [1]:} instead of \verb+>>>+ - \item \texttt{In} stands for input, 1 indicates the command number - \item Try \texttt{Hello World} - \end{itemize} - \begin{lstlisting} - In []: print 'Hello, World!' - Out[]: Hello, World! - \end{lstlisting} - {\tiny the numbers have been omitted to avoid confusion} - \begin{itemize} - \item Hit \texttt{Ctrl-D} to exit \texttt{ipython}; Say \texttt{y} - when prompted. - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Getting comfortable} - \begin{itemize} - \item Let's try some simple math to get comfortable - \end{itemize} - \begin{lstlisting} - In []: 1 + 2 - In []: 5 - 3 - In []: 7 - 4 - In []: 6 * 5 - \end{lstlisting} - \begin{itemize} - \item We get back the expected output - \item Output is displayed with an \texttt{Out[]} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{History \& Arrow Keys} - \begin{itemize} - \item Change the \texttt{print 1+2} - \item Use <UP-Arrow> to go back to \texttt{1+2} command - \item Use <LEFT-Arrow> to get to start of line; type \texttt{print } - \item Hit <RETURN> - \end{itemize} - \begin{lstlisting} - In []: print 1 + 2 - \end{lstlisting} - \begin{itemize} - \item Now, change the previous command to \texttt{print 10*2} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Tab-Completion} - \begin{itemize} - \item We want to use \texttt{round} function - \item Type \texttt{ro}, and hit <TAB> - \end{itemize} - \begin{lstlisting} - In []: ro<TAB> - \end{lstlisting} - \begin{itemize} - \item Type \texttt{r}, and hit <TAB> - \item All possibilities are listed out, when ambiguous - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{?} for Help} - \begin{itemize} - \item To get help for \texttt{abs} function - \end{itemize} - \begin{lstlisting} - In []: abs? - In []: abs(19) - In []: abs(-10.5) - \end{lstlisting} - \begin{itemize} - \item Look at documentation for \texttt{round} - \item Optional arguments are denoted with square brackets - \texttt{[]} - \end{itemize} - \begin{lstlisting} - In []: round(2.484) - In []: round(2.484, 1) - In []: round(2.484, 2) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{?} for Help} - \begin{itemize} - \item To get help for \texttt{abs} function - \end{itemize} - \begin{lstlisting} - In []: abs? - In []: abs(19) - In []: abs(-10.5) - \end{lstlisting} - \begin{itemize} - \item Look at documentation for \texttt{round} - \item Optional arguments are denoted with square brackets - \texttt{[]} - \end{itemize} - \begin{lstlisting} - In []: round(2.484) - In []: round(2.484, 1) - In []: round(2.484, 2) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Interrupting} - \begin{lstlisting} - In []: round(2.484 - ...: - \end{lstlisting} - \begin{itemize} - \item The \ldots prompt is the continuation prompt - \item It comes up, since we haven't completed previous command - \item Either complete by typing the missing \texttt{)} - \item OR hit \texttt{Ctrl-C} to interrupt the command - \end{itemize} - \begin{lstlisting} - In []: round(2.484 - ...: ^C - \end{lstlisting} -\end{frame} - -\section{Basic Datatypes and Operators} - -\begin{frame}[fragile] - \frametitle{Basic Datatypes} - \begin{itemize} - \item Numbers - \begin{itemize} - \item int - \item float - \item complex - \end{itemize} - \item Boolean - \item Sequence - \begin{itemize} - \item Strings - \item Lists - \item Tuples - \end{itemize} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{int}} - \begin{lstlisting} - In []: a = 13 - In []: a - \end{lstlisting} - \begin{itemize} - \item \texttt{a} is a variable of the \texttt{int} type - \item Use the \texttt{type} command to verify - \end{itemize} - \begin{lstlisting} - In []: type(a) - \end{lstlisting} - \begin{itemize} - \item Integers can be arbitrarily long - \end{itemize} - \begin{lstlisting} - In []: b = 9999999999999999999999999999 - In []: b - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{float}} - \begin{lstlisting} - In []: p = 3.141592 - In []: p - \end{lstlisting} - \begin{itemize} - \item Decimal numbers are represented using the \texttt{float} type - \item Notice the loss of precision - \item Floats have a fixed precision - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{complex}} - \begin{lstlisting} - In []: c = 3+4j - \end{lstlisting} - \begin{itemize} - \item A complex number with real part 3, imaginary part 4 - \end{itemize} - \begin{lstlisting} - In []: c.real - In []: c.imag - In []: abs(c) - \end{lstlisting} - \begin{itemize} - \item It's a combination of two floats - \item \texttt{abs} gives the absolute value - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Operations on numbers} - \begin{lstlisting} - In []: 23 + 74 - In []: 23 - 56 - In []: 45 * 76 - - In []: 8 / 3 - In []: 8.0 / 3 - In []: float(8) / 3 - \end{lstlisting} - \begin{itemize} - \item The first division is an integer division - \item To avoid integer division, at least one number should be float - \item \texttt{float} function is changing int to float - \end{itemize} - \begin{lstlisting} - In []: 87 % 6 - In []: 7 ** 8 - \end{lstlisting} - \begin{itemize} - \item \texttt{\%} is used for modulo operation - \item \texttt{**} is used for exponentiation - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Variables \& assignment} - \begin{itemize} - \item All the operations could be done on variables - \end{itemize} - \begin{lstlisting} - In []: a = 23 - In []: b = 74 - In []: a * b - - In []: c = 8 - In []: d = 8.0 - In []: f = c / 3 - \end{lstlisting} - \begin{itemize} - \item Last two commands show assignment - \end{itemize} - \begin{lstlisting} - In []: c = c / 3 - \end{lstlisting} - An operation like the one above, may equivalently be written as - \begin{lstlisting} - In []: c /= 3 - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Booleans \& Operations} - \begin{itemize} - \item All the operations could be done on variables - \end{itemize} - \begin{lstlisting} - In []: t = True - In []: t - In []: f = not t - In []: f - In []: f or t - In []: f and t - \end{lstlisting} - \begin{itemize} - \item Multiple operation in a single command - \item We use parenthesis for explicitly stating what we mean - \item No discussion of operator precedence - \end{itemize} - \begin{lstlisting} - In []: (f and t) or t - In []: f and (t or t) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Sequences} - \begin{itemize} - \item Hold a bunch of elements in a sequence - \item Elements are accessed based on position in the sequence - \item The sequence data-types - \begin{itemize} - \item str - \item list - \item tuple - \end{itemize} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Strings, Lists \& Tuples} - \begin{itemize} - \item Anything withing quotes is a string - \end{itemize} - \begin{lstlisting} - In []: greet_str = "hello" - \end{lstlisting} - \begin{itemize} - \item Items enclosed in \texttt{[ ]} and separated by \texttt{,}s - constitute a list - \end{itemize} - \begin{lstlisting} - In []: num_list = [1, 2, 3, 4, 5, 6, 7, 8] - \end{lstlisting} - \begin{itemize} - \item Items of a tuple are enclosed by \texttt{( )} instead of - \texttt{[ ]} - \end{itemize} - \begin{lstlisting} - In []: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Operations on Sequences} - \begin{itemize} - \item Accessing elements - \end{itemize} - \begin{lstlisting} - In []: num_list[2] - In []: num_tuple[2] - In []: greet_str[2] - \end{lstlisting} - \begin{itemize} - \item Add two sequences of same type - \end{itemize} - \begin{lstlisting} - In []: num_list + [3, 4, 5, 6] - In []: greet_str + " world!" - \end{lstlisting} - \begin{itemize} - \item Get the length of a sequence - \end{itemize} - \begin{lstlisting} - In []: len(num_list) - In []: len(greet_str) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Operations on Sequences \ldots} - \begin{itemize} - \item Check for container-ship of elements - \end{itemize} - \begin{lstlisting} - In []: 3 in num_list - In []: 'h' in greet_str - In []: 'w' in greet_str - In []: 2 in num_tuple - \end{lstlisting} - \begin{itemize} - \item Finding maximum and minimum - \end{itemize} - \begin{lstlisting} - In []: max(num_list) - In []: min(greet_str) - \end{lstlisting} - \begin{itemize} - \item Slice a sequence - \end{itemize} - \begin{lstlisting} - In []: num_list[1:5] - \end{lstlisting} - \begin{itemize} - \item Stride over a sequence - \end{itemize} - \begin{lstlisting} - In []: num_list[1:8:2] - \end{lstlisting} -\end{frame} - diff --git a/basic_python/slides/io_files_parsing.tex b/basic_python/slides/io_files_parsing.tex deleted file mode 100644 index cb46cbe..0000000 --- a/basic_python/slides/io_files_parsing.tex +++ /dev/null @@ -1,239 +0,0 @@ -\section{I/O} - -\begin{frame}[fragile] - \frametitle{Printing} - \begin{lstlisting} - In[]: a = "This is a string" - In[]: a - In[]: print a - \end{lstlisting} - \begin{itemize} - \item Both \texttt{a}, and \texttt{print a} are showing the value - \item What is the difference? - \item Typing \texttt{a} shows the value; \texttt{print a} prints it - \item Typing \texttt{a} shows the value only in interpreter - \item In a script, it has no effect. - \end{itemize} - \begin{lstlisting} - In[]: b = "A line \n New line" - In[]: b - In[]: print b - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{String formatting} - \begin{lstlisting} - In[]: x = 1.5 - In[]: y = 2 - In[]: z = "zed" - In[]: print "x is %2.1f y is %d z is %s" %(x, y, z) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{print x} \& \texttt{print x,}} - \begin{itemize} - \item Open an editor - \item Type the following code - \item Save as \texttt{print\_example.py} - \end{itemize} - \begin{lstlisting} - In[]: print "Hello" - In[]: print "World" - - In[]: print "Hello", - In[]: print "World" - \end{lstlisting} - \begin{itemize} - \item Run the script using \texttt{\% run print\_example.py} - \item \texttt{print x} adds a newline whereas \texttt{print x,} adds - a space - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{raw\_input}} - \begin{lstlisting} - In[]: ip = raw_input() - \end{lstlisting} - \begin{itemize} - \item The cursor is blinking; waiting for input - \item Type \texttt{an input} and hit <ENTER> - \end{itemize} - \begin{lstlisting} - In[]: print ip - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{raw\_input} \ldots} - \begin{lstlisting} - In[]: c = raw_input() - In[]: 5.6 - In[]: c - In[]: type(c) - \end{lstlisting} - \begin{itemize} - \item \alert{\texttt{raw\_input} always takes a string} - \end{itemize} - \begin{lstlisting} - In[]: name = raw_input("Please enter your name: ") - George - \end{lstlisting} - \begin{itemize} - \item \texttt{raw\_input} can display a prompt string for the user - \end{itemize} -\end{frame} - -\section{Files} - -\begin{frame}[fragile] - \frametitle{Opening files} - \begin{lstlisting} - pwd # present working directory - cd /home/fossee # go to location of the file - \end{lstlisting} - {\tiny The file is in our present working directory} - \begin{lstlisting} - In[]: f = open('pendulum.txt') - In[]: f - \end{lstlisting} - \begin{itemize} - \item \texttt{f} is a file object - \item Shows the mode in which the file is open (read mode) - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Reading the whole file} - \begin{lstlisting} - In[]: pend = f.read() - In[]: print pend - \end{lstlisting} - \begin{itemize} - \item We have read the whole file into the variable \texttt{pend} - \end{itemize} - \begin{lstlisting} - In[]: type(pend) - In[]: pend_list = pend.splitlines() - In[]: pend_list - \end{lstlisting} - \begin{itemize} - \item \texttt{pend} is a string variable - \item We can split it at the newline characters into a list of - strings - \item Close the file, when done; Also, if you want to read again - \end{itemize} - \begin{lstlisting} - In[]: f.close() - In[]: f - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Reading line-by-line} - \begin{lstlisting} - In[]: for line in open('pendulum.txt'): - ....: print line - \end{lstlisting} - \begin{itemize} - \item The file object is an ``iterable'' - \item We iterate over it and print each line - \item Instead of printing, collect lines in a list - \end{itemize} - \begin{lstlisting} - In[]: line_list = [ ] - In[]: for line in open('pendulum.txt'): - ....: line_list.append(line) - \end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{File parsing -- Problem} - \begin{lstlisting} - A;010002;ANAND R;058;037;42;35;40;212;P;; - \end{lstlisting} - \begin{itemize} - \item File with records like the one above is given - \item Each record has fields separated by ; - \item region code; roll number; name; - \item marks --- $1^{st}$ L; $2^{nd}$ L; math; science; social; total - \item pass/fail indicated by P/F; W if withheld and else empty - \end{itemize} - - \begin{itemize} - \item We wish to calculate mean of math marks in region B - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Tokenization} - \begin{lstlisting} - In[]: line = "parse this string" - In[]: line.split() - \end{lstlisting} - \begin{itemize} - \item Original string is split on white-space (if no argument) - \item Returns a list of strings - \item It can be given an argument to split on that argrument - \end{itemize} - \begin{lstlisting} - In[]: record = "A;015163;JOSEPH RAJ S;083;042;47;AA;72;244;;;" - In[]: record.split(';') - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Tokenization \ldots} - \begin{itemize} - \item Since we split on commas, fields may have extra spaces at ends - \item We can strip out the spaces at the ends - \end{itemize} - \begin{lstlisting} - In[]: word = " B " - In[]: word.strip() - \end{lstlisting} - \begin{itemize} - \item \texttt{strip} is returning a new string - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{str} to \texttt{float}} - \begin{itemize} - \item After tokenizing, the marks we have are strings - \item We need numbers to perform math operations - \end{itemize} - \begin{lstlisting} - In[]: mark_str = "1.25" - In[]: mark = int(mark_str) - In[]: type(mark_str) - In[]: type(mark) - \end{lstlisting} - \begin{itemize} - \item \texttt{strip} is returning a new string - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{File parsing -- Solution} - \begin{lstlisting} - In[]: math_B = [] # empty list to store marks - In[]: for line in open("sslc1.txt"): - ....: fields = line.split(";") - - ....: reg_code = fields[0] - ....: reg_code_clean = reg_code.strip() - - ....: math_mark_str = fields[5] - ....: math_mark = float(math_mark_str) - - ....: if reg_code == "B": - ....: math_B.append(math_mark) - - In[]: math_B_mean = sum(math_B) / len(math_B) - In[]: math_B_mean - \end{lstlisting} -\end{frame} diff --git a/basic_python/slides/strings_loops_lists.tex b/basic_python/slides/strings_loops_lists.tex deleted file mode 100644 index 26d7b54..0000000 --- a/basic_python/slides/strings_loops_lists.tex +++ /dev/null @@ -1,457 +0,0 @@ -\section{Strings} - -\begin{frame}[fragile] - \frametitle{What are Strings?} - \begin{itemize} - \item Anything quoted is a string - \item Single quotes, double quotes or triple single/double quotes - \item Any length --- single character, null string, \ldots - \end{itemize} - \begin{lstlisting} - In[]: 'This is a string' - In[]: "This is a string too' - In[]: '''This is a string as well''' - In[]: """This is also a string""" - In[]: '' # empty string - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Why so many?} - \begin{itemize} - \item Reduce the need for escaping - \end{itemize} - \begin{lstlisting} - In[]: "Python's strings are powerful!" - In[]: 'He said, "I love Python!"' - \end{lstlisting} - \begin{itemize} - \item Triple quoted strings can be multi-line - \item Used for doc-strings - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Assignment \& Operations} - \begin{lstlisting} - In[]: a = 'Hello' - In[]: b = 'World' - In[]: c = a + ', ' + b + '!' - \end{lstlisting} - \begin{itemize} - \item Strings can be multiplied with numbers - \end{itemize} - \begin{lstlisting} - In[]: a = 'Hello' - In[]: a * 5 - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Accessing Elements} - \begin{lstlisting} - In[]: print a[0], a[4], a[-1], a[-4] - \end{lstlisting} - \begin{itemize} - \item Can we change the elements? - \end{itemize} - \begin{lstlisting} - In[]: a[0] = 'H' - \end{lstlisting} - \begin{itemize} - \item Strings are immutable! - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Problem - Day of the Week?} - \begin{itemize} - \item Strings have methods to manipulate them - \end{itemize} - \begin{block}{Problem} - Given a list, \texttt{week}, containing names of the days of the - week and a string \texttt{s}, check if the string is a day of the - week. We should be able to check for any of the forms like, - \emph{sat, saturday, Sat, Saturday, SAT, SATURDAY} - \end{block} - \begin{itemize} - \item Get the first 3 characters of the string - \item Convert it all to lower case - \item Check for existence in the list, \texttt{week} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Slicing} - \begin{lstlisting} - In[]: q = "Hello World" - In[]: q[0:3] - In[]: q[:3] - In[]: q[3:] - In[]: q[:] - In[]: q[-1:1] - In[]: q[1:-1] - \end{lstlisting} - \begin{itemize} - \item One or both of the limits, is optional - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Striding} - \begin{lstlisting} - In[]: q[0:5:1] - In[]: q[0:5:2] - In[]: q[0:5:3] - In[]: q[0::2] - In[]: q[2::2] - In[]: q[::2] - In[]: q[5:0:-1] - In[]: q[::-1] - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{String Methods} - \begin{lstlisting} - In[]: s.lower() - In[]: s.upper() - s.<TAB> - \end{lstlisting} - \begin{itemize} - \item \alert{Strings are immutable!} - \item A new string is being returned - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Solution - Day of the Week?} - \begin{lstlisting} - In[]: s.lower()[:3] in week - \end{lstlisting} - OR - \begin{lstlisting} - In[]: s[:3].lower() in week - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{join} a list of strings} - \begin{itemize} - \item Given a list of strings - \item We wish to join them into a single string - \item Possibly, each string separated by a common token - \end{itemize} - \begin{lstlisting} - In[]: email_list = ["info@fossee.in", - "enquiries@fossee.in", - "help@fossee.in"] - \end{lstlisting} - \begin{lstlisting} - In[]: '; '.join(email_list) - In[]: ', '.join(email_list) - \end{lstlisting} -\end{frame} - -\section{Conditionals} - -\begin{frame}[fragile] - \frametitle{\texttt{if-else} block} - \begin{lstlisting} - In[]: a = 5 - In[]: if a % 2 == 0: - ....: print "Even" - ....: else: - ....: print "Odd" - \end{lstlisting} - \begin{itemize} - \item A code block -- \texttt{:} and indentation - \item Exactly one block gets executed in the \texttt{if-else} - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{\texttt{if-elif-else}} - \begin{lstlisting} - In[]: if a > 0: - ....: print "positive" - ....: elif a < 0: - ....: print "negative" - ....: else: - ....: print "zero" - \end{lstlisting} - \begin{itemize} - \item Only one block gets executed, depending on \texttt{a} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{else} is optional} - \begin{lstlisting} - In[]: if user == 'admin': - ....: admin_Operations() - ....: elif user == 'moderator': - ....: moderator_operations() - ....: elif user == 'client': - ....: customer_operations() - \end{lstlisting} - \begin{itemize} - \item Note that there is no \texttt{else} block - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Ternary operator} - \begin{itemize} - \item \texttt{score\_str} is either \texttt{'AA'} or a string of one - of the numbers in the range 0 to 100. - \item We wish to convert the string to a number using \texttt{int} - \item Convert it to 0, when it is \texttt{'AA'} - \item \texttt{if-else} construct or the ternary operator - \end{itemize} - \begin{lstlisting} - In[]: if score_str != 'AA': - ....: score = int(score_str) - ....: else: - ....: score = 0 - \end{lstlisting} - \begin{lstlisting} - In[]: ss = score_str - In[]: score = int(ss) if ss != 'AA' else 0 - \end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{\texttt{pass}} - \begin{itemize} - \item \texttt{pass} is a syntactic filler - \item When a certain block has no statements, a \texttt{pass} is - thrown in - \item Mostly, when you want to get back to that part, later. - \end{itemize} -\end{frame} - -\section{Loops} - -\begin{frame}[fragile] - \frametitle{\texttt{while}} - \begin{itemize} - \item Print squares of all odd numbers less than 10 using - \texttt{while} - \end{itemize} - \begin{lstlisting} - In[]: i = 1 - - In[]: while i<10: - ....: print i*i - ....: i += 2 - \end{lstlisting} - \begin{itemize} - \item The loops runs as long as the condition is \texttt{True} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{for}} - \begin{itemize} - \item Print squares of all odd numbers less than 10 using - \texttt{for} - \end{itemize} - \begin{lstlisting} - In[]: for n in [1, 2, 3]: - ....: print n - \end{lstlisting} - \begin{itemize} - \item \texttt{for} iterates over each element of a sequence - \end{itemize} - \begin{lstlisting} - In[]: for n in [1, 3, 5, 7, 9]: - ....: print n*n - - In[]: for n in range(1, 10, 2): - ....: print n*n - \end{lstlisting} - \begin{itemize} - \item \alert{range([start,] stop[, step])} - \item Returns a list; Stop value is not included. - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{\texttt{break}} - \begin{itemize} - \item breaks out of the innermost loop. - \item Squares of odd numbers below 10 using \texttt{while} \& - \texttt{break} - \end{itemize} - \begin{lstlisting} - In[]: i = 1 - - In[]: while True: - ....: print i*i - ....: i += 2 - ....: if i>10: - ....: break - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\texttt{continue}} - \begin{itemize} - \item Skips execution of rest of the loop on current iteration - \item Jumps to the end of this iteration - \item Squares of all odd numbers below 10, not multiples of 3 - \end{itemize} - \begin{lstlisting} - In[]: for n in range(1, 10, 2): - ....: if n%3 == 0: - ....: continue - ....: print n*n - \end{lstlisting} -\end{frame} - - -\section{Lists} - -\begin{frame}[fragile] - \frametitle{Creating Lists} - \begin{lstlisting} - In[]: empty = [] - - In[]: p = ['spam', 'eggs', 100, 1.234] - In[]: q = [[4, 2, 3, 4], 'and', 1, 2, 3, 4] - \end{lstlisting} - \begin{itemize} - \item Lists can be empty, with no elements in them - \item Lists can be heterogeneous -- every element of different kind - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Accessing Elements} - \begin{lstlisting} - In[]: print p[0], p[1], p[3] - - In[]: print p[-1], p[-2], p[-4] - In[]: print p[10] - \end{lstlisting} - \begin{itemize} - \item Indexing starts from 0 - \item Indexes can be negative - \item Indexes should be in the valid range - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Accessing Elements \& length} - \begin{lstlisting} - In[]: print p[0], p[1], p[3] - - In[]: print p[-1], p[-2], p[-4] - In[]: print len(p) - In[]: print p[10] - \end{lstlisting} - \begin{itemize} - \item Indexing starts from 0 - \item Indexes can be negative - \item Indexes should be within the \texttt{range(0, len(p))} - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Adding \& Removing Elements} - \begin{itemize} - \item The append method adds elements to the end of the list - \end{itemize} - \begin{lstlisting} - In[]: p.append('onemore') - In[]: p - In[]: p.append([1, 6]) - In[]: p - \end{lstlisting} - \begin{itemize} - \item Elements can be removed based on their index OR - \item based on the value of the element - \end{itemize} - \begin{lstlisting} - In[]: del p[1] - In[]: p.remove(100) - \end{lstlisting} - \begin{itemize} - \item \alert{When removing by value, first element is removed} - \end{itemize} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Concatenating lists} - \begin{lstlisting} - In[]: a = [1, 2, 3, 4] - In[]: b = [4, 5, 6, 7] - In[]: a + b - In[]: print a+b, a, b - \end{lstlisting} - \begin{itemize} - \item A new list is returned; None of the original lists change - \end{itemize} - \begin{lstlisting} - In[]: c = a + b - In[]: c - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Slicing \& Striding} - \begin{lstlisting} - In[]: primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] - In[]: primes[4:8] - In[]: primes[:4] - - In[]: num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - In[]: num[1:10:2] - In[]: num[:10] - In[]: num[10:] - In[]: num[::2] - In[]: num[::-1] - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Sorting} - \begin{lstlisting} - In[]: a = [5, 1, 6, 7, 7, 10] - In[]: a.sort() - In[]: a - \end{lstlisting} - \begin{itemize} - \item \texttt{sort} method sorts the list in-place - \item Use \texttt{sorted} if you require a new list - \end{itemize} - \begin{lstlisting} - In[]: a = [5, 1, 6, 7, 7, 10] - In[]: sorted(a) - In[]: a - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Reversing} - \begin{lstlisting} - In[]: a = [5, 1, 6, 7, 7, 10] - In[]: a.reverse() - In[]: a - \end{lstlisting} - \begin{itemize} - \item \texttt{reverse} method reverses the list in-place - \item Use \texttt{[::-1]} if you require a new list - \end{itemize} - \begin{lstlisting} - In[]: a = [5, 1, 6, 7, 7, 10] - In[]: a[::-1] - In[]: a - \end{lstlisting} -\end{frame} diff --git a/basic_python/slides/tmp.tex b/basic_python/slides/tmp.tex deleted file mode 100644 index 8b13789..0000000 --- a/basic_python/slides/tmp.tex +++ /dev/null @@ -1 +0,0 @@ - diff --git a/basic_python/slides/tuples_dicts_sets.tex b/basic_python/slides/tuples_dicts_sets.tex deleted file mode 100644 index b09fd99..0000000 --- a/basic_python/slides/tuples_dicts_sets.tex +++ /dev/null @@ -1,258 +0,0 @@ -\section{Tuples} - -\begin{frame}[fragile] - \frametitle{Tuples -- Initialization} - \begin{lstlisting} - In[]: t = (1, 2.5, "hello", -4, "world", 1.24, 5) - In[]: t - \end{lstlisting} - \begin{itemize} - \item It is not always necessary to use parenthesis - \end{itemize} - \begin{lstlisting} - In[]: a = 1, 2, 3 - In[]: b = 1, - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Indexing} - \begin{lstlisting} - In[]: t[3] - In[]: t[1:5:2] - In[]: t[2] = "Hello" - \end{lstlisting} - \begin{itemize} - \item \alert{Tuples are immutable!} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Swapping values} - \begin{lstlisting} - In[]: a = 5 - In[]: b = 7 - - In[]: temp = a - In[]: a = b - In[]: b = temp - \end{lstlisting} - \begin{itemize} - \item Here's the Pythonic way of doing it - \end{itemize} - \begin{lstlisting} - In[]: a, b = b, a - \end{lstlisting} - \begin{itemize} - \item The variables can be of different data-types - \end{itemize} - \begin{lstlisting} - In[]: a = 2.5 - In[]: b = "hello" - In[]: a, b = b, a - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Tuple packing \& unpacking} - \begin{lstlisting} - In[]: 5, - - In[]: 5, "hello", 2.5 - \end{lstlisting} - \begin{itemize} - \item Tuple packing and unpacking, when swapping - \end{itemize} - \begin{lstlisting} - In[]: a, b = b, a - \end{lstlisting} -\end{frame} - -\section{Dictionaries} - -\begin{frame}[fragile] - \frametitle{Creating Dictionaries} - \begin{lstlisting} - In[]: mt_dict = {} - - In[]: extensions = {'jpg' : 'JPEG Image', - 'py' : 'Python script', - 'html' : 'Html document', - 'pdf' : 'Portable Document Format'} - - In[]: extensions - \end{lstlisting} - \begin{itemize} - \item Key-Value pairs - \item \alert{ No ordering of keys! } - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Accessing Elements} - \begin{lstlisting} - In[]: print extensions['jpg'] - \end{lstlisting} - \begin{itemize} - \item Values can be accessed using keys - \end{itemize} - \begin{lstlisting} - In[]: print extensions['zip'] - \end{lstlisting} - \begin{itemize} - \item Values of non-existent keys cannot, obviously, be accessed - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Adding \& Removing Elements} - \begin{itemize} - \item Adding a new key-value pair - \end{itemize} - \begin{lstlisting} - In[]: extensions['cpp'] = 'C++ code' - In[]: extensions - \end{lstlisting} - \begin{itemize} - \item Deleting a key-value pair - \end{itemize} - \begin{lstlisting} - In[]: del extension['pdf'] - In[]: extensions - \end{lstlisting} - \begin{itemize} - \item Assigning to existing key, modifies the value - \end{itemize} - \begin{lstlisting} - In[]: extensions['cpp'] = 'C++ source code' - In[]: extensions - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Containership} - \begin{lstlisting} - In[]: 'py' in extensions - In[]: 'odt' in extensions - \end{lstlisting} - \begin{itemize} - \item Allow checking for container-ship of keys; NOT values - \item Use the \texttt{in} keyword to check for container-ship - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Lists of Keys and Values} - \begin{lstlisting} - In[]: extensions.keys() - In[]: extensions.values() - \end{lstlisting} - \begin{itemize} - \item Note that the order of the keys and values match - \item That can be relied upon and used - \end{itemize} - \begin{lstlisting} - In[]: for each in extensions.keys(): - ....: print each, "-->", extensions[each] - ....: - \end{lstlisting} -\end{frame} - -\section{Sets} - -\begin{frame}[fragile] - \frametitle{Creating Sets} - \begin{lstlisting} - In[]: a_list = [1, 2, 1, 4, 5, 6, 2] - In[]: a = set(a_list) - In[]: a - \end{lstlisting} - \begin{itemize} - \item Conceptually identical to the sets in mathematics - \item Duplicate elements not allowed - \item No ordering of elements exists - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Operations on Sets} - \begin{lstlisting} - In[]: f10 = set([1, 2, 3, 5, 8]) - In[]: p10 = set([2, 3, 5, 7]) - \end{lstlisting} - \begin{itemize} - \item Mathematical operations performed on sets, can be performed - \end{itemize} - \begin{itemize} - \item Union - \begin{lstlisting} - In[]: f10 | p10 - \end{lstlisting} - \item Intersection - \begin{lstlisting} - In[]: f10 & p10 - \end{lstlisting} - \item Difference - \begin{lstlisting} - In[]: f10 - p10 - \end{lstlisting} - \item Symmetric Difference - \begin{lstlisting} - In[]: f10 ^ p10 - \end{lstlisting} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Sub-sets} - \begin{itemize} - \item Proper Subset - \begin{lstlisting} - In[]: b = set([1, 2]) - In[]: b < f10 - \end{lstlisting} - \item Subsets - \begin{lstlisting} - In[]: f10 <= f10 - \end{lstlisting} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Elements of sets} - \begin{itemize} - \item Containership - \begin{lstlisting} - In[]: 1 in f10 - In[]: 4 in f10 - \end{lstlisting} - \item Iterating over elements - \begin{lstlisting} - In[]: for i in f10: - ....: print i, - ....: - \end{lstlisting} - \item Subsets - \begin{lstlisting} - In[]: f10 <= f10 - \end{lstlisting} - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Sets -- Example} - \begin{block}{} - Given a list of marks, \texttt{[20, 23, 22, 23, 20, 21, 23]} list - all the duplicates - \end{block} - \begin{lstlisting} - In[]: marks = [20, 23, 22, 23, 20, 21, 23] - In[]: marks_set = set(marks) - In[]: for mark in marks_set: - ....: marks.remove(mark) - - # left with only duplicates - In[]: duplicates = set(marks) - \end{lstlisting} -\end{frame} - |