summaryrefslogtreecommitdiff
path: root/slides/basic_python/strings_loops_lists.tex
diff options
context:
space:
mode:
Diffstat (limited to 'slides/basic_python/strings_loops_lists.tex')
-rw-r--r--slides/basic_python/strings_loops_lists.tex148
1 files changed, 83 insertions, 65 deletions
diff --git a/slides/basic_python/strings_loops_lists.tex b/slides/basic_python/strings_loops_lists.tex
index 1b514a6..1e5d05f 100644
--- a/slides/basic_python/strings_loops_lists.tex
+++ b/slides/basic_python/strings_loops_lists.tex
@@ -72,7 +72,7 @@
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}
+ \emph{sat, Sat, SAT}
\end{block}
\begin{itemize}
\item Get the first 3 characters of the string
@@ -143,13 +143,13 @@
\item Possibly, each string separated by a common token
\end{itemize}
\begin{lstlisting}
- In[]: email_list = ["info@fossee.in",
+ In[]: email_list = ["info@fossee.in",
"enquiries@fossee.in",
"help@fossee.in"]
\end{lstlisting}
\begin{lstlisting}
- In[]: '; '.join(email_list)
- In[]: ', '.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}
- In[]: a = 5
- In[]: 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
@@ -189,12 +189,12 @@
\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()
+ 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
@@ -242,11 +242,11 @@
\texttt{while}
\end{itemize}
\begin{lstlisting}
- In[]: i = 1
+ In[]: i = 1
- In[]: 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}
- In[]: 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}
- In[]: for n in [1, 3, 5, 7, 9]:
- ....: print n*n
+ In[]: for n in [1, 3, 5, 7, 9]:
+ ....: print n*n
- In[]: 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}
- In[]: i = 1
+ In[]: i = 1
- In[]: 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,13 +306,31 @@
\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
+ In[]: for n in range(1, 10, 2):
+ ....: if n%3 == 0:
+ ....: continue
+ ....: print n*n
\end{lstlisting}
\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}
+
\section{Lists}
@@ -333,10 +351,10 @@
\begin{frame}[fragile]
\frametitle{Accessing Elements}
\begin{lstlisting}
- In[]: print p[0], p[1], p[3]
+ In[]: print p[0], p[1], p[3]
- In[]: print p[-1], p[-2], p[-4]
- In[]: 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 +366,11 @@
\begin{frame}[fragile]
\frametitle{Accessing Elements \& length}
\begin{lstlisting}
- In[]: print p[0], p[1], p[3]
+ In[]: print p[0], p[1], p[3]
In[]: print p[-1], p[-2], p[-4]
In[]: print len(p)
- In[]: print p[10]
+ In[]: print p[10]
\end{lstlisting}
\begin{itemize}
\item Indexing starts from 0
@@ -368,9 +386,9 @@
\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.append('onemore')
+ In[]: p
+ In[]: p.append([1, 6])
In[]: p
\end{lstlisting}
\begin{itemize}
@@ -390,10 +408,10 @@
\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
+ 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
@@ -407,13 +425,13 @@
\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[]: 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 = [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]
@@ -423,35 +441,35 @@
\begin{frame}[fragile]
\frametitle{Sorting}
\begin{lstlisting}
- In[]: a = [5, 1, 6, 7, 7, 10]
- In[]: a.sort()
- In[]: 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}
- In[]: a = [5, 1, 6, 7, 7, 10]
- In[]: sorted(a)
- In[]: 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}
- In[]: a = [5, 1, 6, 7, 7, 10]
- In[]: a.reverse()
- In[]: 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}
- In[]: a = [5, 1, 6, 7, 7, 10]
- In[]: a[::-1]
- In[]: a
+ In[]: a = [5, 1, 6, 7, 7, 10]
+ In[]: a[::-1]
+ In[]: a
\end{lstlisting}
\end{frame}