diff options
author | Prabhu Ramachandran | 2017-01-22 19:52:39 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2017-01-22 19:52:39 +0530 |
commit | c0032d69db78f5003b5263430cf621de42add7bc (patch) | |
tree | d8a3aae113ad6ba7903c56c310bae92b18921fa7 /basic_python | |
parent | 5a8811f2e4978cc2e10468a8c7d09bafb42d7ece (diff) | |
download | python-workshops-c0032d69db78f5003b5263430cf621de42add7bc.tar.gz python-workshops-c0032d69db78f5003b5263430cf621de42add7bc.tar.bz2 python-workshops-c0032d69db78f5003b5263430cf621de42add7bc.zip |
Introduce the assert statement early on.
Diffstat (limited to 'basic_python')
-rw-r--r-- | basic_python/basics.tex | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/basic_python/basics.tex b/basic_python/basics.tex index d21f893..79c4946 100644 --- a/basic_python/basics.tex +++ b/basic_python/basics.tex @@ -388,9 +388,50 @@ Out[]: False \end{frame} \begin{frame}[fragile] + \frametitle{The \typ{assert} statement} + \begin{itemize} + \item You will see it in tests and your exam! + \end{itemize} + + \begin{small} +\begin{lstlisting} +In []: assert p != n +In []: assert p == n +------------------------------------------------------ +AssertionError Traceback (most recent call last) +----> 1 assert p == n + +AssertionError: +\end{lstlisting} + \end{small} + \begin{itemize} + \item No error if condition is True + \item Raises error if False + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{\typ{assert} examples} + \begin{small} +\begin{lstlisting} +In []: assert p == n, "Oops condition failed" +------------------------------------------------------ +AssertionError Traceback (most recent call last) +----> 1 assert p == n + +AssertionError: Oops condition failed +\end{lstlisting} + \end{small} + \begin{itemize} + \item Can supply an optional message + \end{itemize} +\end{frame} + + +\begin{frame}[fragile] \frametitle{String containership} \begin{lstlisting} -In []: fruits = 'apple, banana, pear, mango' +In []: fruits = 'apple, banana, pear' In []: 'apple' in fruits Out[]: True |