summaryrefslogtreecommitdiff
path: root/basic_python/control_flow.tex
diff options
context:
space:
mode:
Diffstat (limited to 'basic_python/control_flow.tex')
-rw-r--r--basic_python/control_flow.tex46
1 files changed, 35 insertions, 11 deletions
diff --git a/basic_python/control_flow.tex b/basic_python/control_flow.tex
index f53e028..a299563 100644
--- a/basic_python/control_flow.tex
+++ b/basic_python/control_flow.tex
@@ -72,6 +72,28 @@ else:
\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}
+
+
\section{Control flow}
\subsection{Basic Looping}
@@ -249,19 +271,21 @@ while b < 500:
\end{frame}
\begin{frame}[fragile]
- \frametitle{\typ{continue} example}
-Try this:
-\begin{lstlisting}
-a = 0
-while a < 10:
- if a % 2 == 0:
- continue
- print(a, end=' ')
- a += 1
-\end{lstlisting}
-Carefully notice what it does
+ \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}
+
\begin{frame}[fragile]
\frametitle{\typ{pass} example}
Try this: