diff options
Diffstat (limited to 'basic_python/slides/strings_loops_lists.tex')
-rw-r--r-- | basic_python/slides/strings_loops_lists.tex | 244 |
1 files changed, 122 insertions, 122 deletions
diff --git a/basic_python/slides/strings_loops_lists.tex b/basic_python/slides/strings_loops_lists.tex index 3d6d8e8..26d7b54 100644 --- a/basic_python/slides/strings_loops_lists.tex +++ b/basic_python/slides/strings_loops_lists.tex @@ -8,11 +8,11 @@ \item Any length --- single character, null string, \ldots \end{itemize} \begin{lstlisting} - 'This is a string' - "This is a string too' - '''This is a string as well''' - """This is also a string""" - '' # empty string + 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} @@ -22,8 +22,8 @@ \item Reduce the need for escaping \end{itemize} \begin{lstlisting} - "Python's strings are powerful!" - 'He said, "I love Python!"' + In[]: "Python's strings are powerful!" + In[]: 'He said, "I love Python!"' \end{lstlisting} \begin{itemize} \item Triple quoted strings can be multi-line @@ -34,29 +34,29 @@ \begin{frame}[fragile] \frametitle{Assignment \& Operations} \begin{lstlisting} - a = 'Hello' - b = 'World' - c = a + ', ' + b + '!' + 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} - a = 'Hello' - a * 5 + In[]: a = 'Hello' + In[]: a * 5 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Accessing Elements} \begin{lstlisting} - print a[0], a[4], a[-1], a[-4] + In[]: print a[0], a[4], a[-1], a[-4] \end{lstlisting} \begin{itemize} \item Can we change the elements? \end{itemize} \begin{lstlisting} - a[0] = 'H' + In[]: a[0] = 'H' \end{lstlisting} \begin{itemize} \item Strings are immutable! @@ -84,13 +84,13 @@ \begin{frame}[fragile] \frametitle{Slicing} \begin{lstlisting} - q = "Hello World" - q[0:3] - q[:3] - q[3:] - q[:] - q[-1:1] - q[1:-1] + 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 @@ -100,22 +100,22 @@ \begin{frame}[fragile] \frametitle{Striding} \begin{lstlisting} - q[0:5:1] - q[0:5:2] - q[0:5:3] - q[0::2] - q[2::2] - q[::2] - q[5:0:-1] - q[::-1] + 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} - s.lower() - s.upper() + In[]: s.lower() + In[]: s.upper() s.<TAB> \end{lstlisting} \begin{itemize} @@ -127,11 +127,11 @@ \begin{frame}[fragile] \frametitle{Solution - Day of the Week?} \begin{lstlisting} - s.lower()[:3] in week + In[]: s.lower()[:3] in week \end{lstlisting} OR \begin{lstlisting} - s[:3].lower() in week + In[]: s[:3].lower() in week \end{lstlisting} \end{frame} @@ -143,13 +143,13 @@ \item Possibly, each string separated by a common token \end{itemize} \begin{lstlisting} - email_list = ["info@fossee.in", + In[]: email_list = ["info@fossee.in", "enquiries@fossee.in", "help@fossee.in"] \end{lstlisting} \begin{lstlisting} - '; '.join(email_list) - ', '.join(email_list) + In[]: '; '.join(email_list) + In[]: ', '.join(email_list) \end{lstlisting} \end{frame} @@ -158,11 +158,11 @@ \begin{frame}[fragile] \frametitle{\texttt{if-else} block} \begin{lstlisting} - a = 5 - if a % 2 == 0: - print "Even" - else: - print "Odd" + In[]: a = 5 + In[]: if a % 2 == 0: + ....: print "Even" + ....: else: + ....: print "Odd" \end{lstlisting} \begin{itemize} \item A code block -- \texttt{:} and indentation @@ -174,12 +174,12 @@ \begin{frame}[fragile] \frametitle{\texttt{if-elif-else}} \begin{lstlisting} - if a > 0: - print "positive" - elif a < 0: - print "negative" - else: - print "zero" + 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} @@ -189,12 +189,12 @@ \begin{frame}[fragile] \frametitle{\texttt{else} is optional} \begin{lstlisting} - if user == 'admin': - admin_Operations() - elif user == 'moderator': - moderator_operations() - elif user == 'client': - customer_operations() + 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 @@ -211,14 +211,14 @@ \item \texttt{if-else} construct or the ternary operator \end{itemize} \begin{lstlisting} - if score_str != 'AA': - score = int(score_str) - else: - score = 0 + In[]: if score_str != 'AA': + ....: score = int(score_str) + ....: else: + ....: score = 0 \end{lstlisting} \begin{lstlisting} - ss = score_str - score = int(ss) if ss != 'AA' else 0 + In[]: ss = score_str + In[]: score = int(ss) if ss != 'AA' else 0 \end{lstlisting} \end{frame} @@ -242,11 +242,11 @@ \texttt{while} \end{itemize} \begin{lstlisting} - i = 1 + In[]: i = 1 - while i<10: - print i*i - i += 2 + 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} @@ -260,18 +260,18 @@ \texttt{for} \end{itemize} \begin{lstlisting} - for n in [1, 2, 3]: - print n + 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} - for n in [1, 3, 5, 7, 9]: - print n*n + In[]: for n in [1, 3, 5, 7, 9]: + ....: print n*n - for n in range(1, 10, 2): - print n*n + In[]: for n in range(1, 10, 2): + ....: print n*n \end{lstlisting} \begin{itemize} \item \alert{range([start,] stop[, step])} @@ -288,13 +288,13 @@ \texttt{break} \end{itemize} \begin{lstlisting} - i = 1 + In[]: i = 1 - while True: - print i*i - i += 2 - if i>10: - break + In[]: while True: + ....: print i*i + ....: i += 2 + ....: if i>10: + ....: break \end{lstlisting} \end{frame} @@ -306,10 +306,10 @@ \item Squares of all odd numbers below 10, not multiples of 3 \end{itemize} \begin{lstlisting} - for n in range(1, 10, 2): - if n%3 == 0: - continue - print n*n + In[]: for n in range(1, 10, 2): + ....: if n%3 == 0: + ....: continue + ....: print n*n \end{lstlisting} \end{frame} @@ -319,10 +319,10 @@ \begin{frame}[fragile] \frametitle{Creating Lists} \begin{lstlisting} - empty = [] + In[]: empty = [] - p = ['spam', 'eggs', 100, 1.234] - q = [[4, 2, 3, 4], 'and', 1, 2, 3, 4] + 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 @@ -333,10 +333,10 @@ \begin{frame}[fragile] \frametitle{Accessing Elements} \begin{lstlisting} - print p[0], p[1], p[3] + In[]: print p[0], p[1], p[3] - print p[-1], p[-2], p[-4] - print p[10] + In[]: print p[-1], p[-2], p[-4] + In[]: print p[10] \end{lstlisting} \begin{itemize} \item Indexing starts from 0 @@ -348,11 +348,11 @@ \begin{frame}[fragile] \frametitle{Accessing Elements \& length} \begin{lstlisting} - print p[0], p[1], p[3] + In[]: print p[0], p[1], p[3] - print p[-1], p[-2], p[-4] - print len(p) - print p[10] + 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 @@ -368,18 +368,18 @@ \item The append method adds elements to the end of the list \end{itemize} \begin{lstlisting} - p.append('onemore') - p - p.append([1, 6]) - p + 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} - del p[1] - p.remove(100) + In[]: del p[1] + In[]: p.remove(100) \end{lstlisting} \begin{itemize} \item \alert{When removing by value, first element is removed} @@ -390,68 +390,68 @@ \begin{frame}[fragile] \frametitle{Concatenating lists} \begin{lstlisting} - a = [1, 2, 3, 4] - b = [4, 5, 6, 7] - a + b - print a+b, a, b + 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} - c = a + b - c + In[]: c = a + b + In[]: c \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Slicing \& Striding} \begin{lstlisting} - primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] - primes[4:8] - primes[:4] + In[]: primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + In[]: primes[4:8] + In[]: primes[:4] - num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] - num[1:10:2] - num[:10] - num[10:] - num[::2] - num[::-1] + 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} - a = [5, 1, 6, 7, 7, 10] - a.sort() - a + 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} - a = [5, 1, 6, 7, 7, 10] - sorted(a) - a + In[]: a = [5, 1, 6, 7, 7, 10] + In[]: sorted(a) + In[]: a \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Reversing} \begin{lstlisting} - a = [5, 1, 6, 7, 7, 10] - a.reverse() - a + 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} - a = [5, 1, 6, 7, 7, 10] - a[::-1] - a + In[]: a = [5, 1, 6, 7, 7, 10] + In[]: a[::-1] + In[]: a \end{lstlisting} \end{frame} |