flow_control.tex 7.25 KB
Newer Older
Mihai's avatar
Mihai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
\documentclass[aspectratio=1610,slidestop]{beamer}

\author{Mihai Lefter}
\title{Python Programming}
\providecommand{\mySubTitle}{Flow Control}
\providecommand{\myConference}{Programming Course}
\providecommand{\myDate}{27-11-2018}
\providecommand{\myGroup}{}
\providecommand{\myDepartment}{}
\providecommand{\myCenter}{}

\usetheme{lumc}

\usepackage{minted}
\usepackage{tikz}
\usepackage[many]{tcolorbox}

\definecolor{monokaibg}{HTML}{272822}
\definecolor{emailc}{HTML}{1e90FF}
\definecolor{scriptback}{HTML}{CDECF0}

\newenvironment{ipython}
 {\begin{tcolorbox}[title=IPython,
                   title filled=false,
                   fonttitle=\scriptsize,
                   fontupper=\footnotesize,
                   enhanced,
                   colback=monokaibg,
                   drop small lifted shadow,
                   boxrule=0.1mm,
                   left=0.1cm,
                   arc=0mm,
                   colframe=black]}
 {\end{tcolorbox}}


\newenvironment{terminal}
 {\begin{tcolorbox}[title=terminal,
                   title filled=false,
                   fonttitle=\scriptsize,
                   fontupper=\footnotesize,
                   enhanced,
                   colback=monokaibg,
                   drop small lifted shadow,
                   boxrule=0.1mm,
                   left=0.1cm,
                   arc=0mm,
                   colframe=black]}
 {\end{tcolorbox}}


\newcommand{\hrefcc}[2]{\textcolor{#1}{\href{#2}{#2}}}
\newcommand{\hrefc}[3]{\textcolor{#1}{\href{#2}{#3}}}

\newcounter{cntr}
\renewcommand{\thecntr}{\texttt{[\arabic{cntr}]}}

\newenvironment{pythonin}[1]
{\VerbatimEnvironment
  \begin{minipage}[t]{0.11\linewidth}
   \textcolor{green}{\texttt{{\refstepcounter{cntr}\label{#1}In \thecntr:}}}
  \end{minipage}
  \begin{minipage}[t]{0.89\linewidth}
  \begin{minted}[
    breaklines=true,style=monokai]{#1}}
 {\end{minted}
 \end{minipage}}

\newenvironment{pythonout}[1]
{\VerbatimEnvironment
  \addtocounter{cntr}{-1}
  \begin{minipage}[t]{0.11\linewidth}
   \textcolor{red}{\texttt{{\refstepcounter{cntr}\label{#1}Out\thecntr:}}}
  \end{minipage}
  \begin{minipage}[t]{0.89\linewidth}
  \begin{minted}[
    breaklines=true,style=monokai]{#1}}
 {\end{minted}
 \end{minipage}}

\newenvironment{pythonerr}[1]
{\VerbatimEnvironment
  \begin{minted}[
    breaklines=true,style=monokai]{#1}}
 {\end{minted}}


\newenvironment{pythonfile}[1]
 {\begin{tcolorbox}[title=#1,
                    title filled=false,
                    coltitle=LUMCDonkerblauw,
                    fonttitle=\scriptsize,
                    fontupper=\footnotesize,
                    enhanced,
                    drop small lifted shadow,
                    boxrule=0.1mm,
                    leftrule=5mm,
                    rulecolor=white,
                    left=0.1cm,
                    colback=white!92!black,
                    colframe=scriptback]}
 {\end{tcolorbox}}

\begin{document}

% This disables the \pause command, handy in the editing phase.
%\renewcommand{\pause}{}

% Make the title slide.
\makeTitleSlide{\includegraphics[height=3.5cm]{../../images/Python.pdf}}

% First page of the presentation.
\section{Introduction}
\makeTableOfContents


\section{Working with scripts}

\begin{pframe}
 Interpreters are great for prototyping, but not really suitable if you want to
 share or release code. To do so, we write our Python commands in scripts (and
 later, modules).

 A script is a simple text file containing Python instructions to execute.
\end{pframe}


\subsection{Executing scripts}
\begin{pframe}
 There are two common ways to execute a script:
  \begin{itemize}
   \item As an argument of the Python interpreter command.
   \item As a standalone executable (with the appropriate shebang line and
   file mode).
  \end{itemize}
  \medskip

  IPython gives you a third option:
  \begin{itemize}
   \item As an argument of the \lstinline{%run} magic.
  \end{itemize}
\end{pframe}


\subsection{Writing your script}
\begin{pframe}
 Let's start with a simple GC calculator. Open your text editor, and write the
 following Python statements (remember your indentations):

 \begin{pythonfile}{first\_script.py}
  \begin{minted}[linenos]{python}
print("Hello world!")
  \end{minted}
 \end{pythonfile}
Save the file as first\_script.py and go to your shell.
\end{pframe}

\subsection{Running the script}
\begin{pframe}
 Let's try the first method: using your script as an argument:
 \begin{terminal}
  \color{white}{
  \begin{lstlisting}[frame=,style=,numbers=none]
$ python first_script.py
  \end{lstlisting}}
 \end{terminal}
  Is the output as you expect?
\end{pframe}

\begin{pframe}
  For the second method, we need to do two more things:
  \begin{itemize}
   \item Open the script in your editor and add the following line to the very
   top:
   \begin{itemize}
    \item \mintinline{python}{#!/usr/bin/env python}
   \end{itemize}
   \item Save the file, go back to the shell, and allow the file to be executed.
  \end{itemize}
 \begin{terminal}
  \color{white}{
  \begin{lstlisting}[frame=,style=,numbers=none]
$ chmod +x first_script.py
  \end{lstlisting}}
 \end{terminal}
You can now execute the file directly:
\begin{terminal}
  \color{white}{
  \begin{lstlisting}[frame=,style=,numbers=none]
$ ./first_script.py
  \end{lstlisting}}
 \end{terminal}
 Is the output the same as the previous method?
\end{pframe}

\begin{pframe}
 Finally, try out the third method. Open an IPython interpreter session and do:
 \begin{ipython}
  \begin{pythonin}{python}
%run seq_toolbox.py
  \end{pythonin}
 \end{ipython}
\end{pframe}


\section{Conditionals}

\subsection{if statements}
\begin{pframe}
 \begin{pythonfile}{if.py}
  \begin{minted}[linenos]{python}
if 26 <= 17:
    print('Fact: 26 is less than or equal to 17')
elif (26 + 8 > 14) == True:
    print('Did we need the ` == True` part here?')
else:
    print('Nothing seems true')
  \end{minted}
 \end{pythonfile}

 \begin{terminal}
  \color{white}{
  \begin{lstlisting}[frame=,style=,numbers=none]
$ python if.py
Did we need the ` == True` part here?
  \end{lstlisting}}
 \end{terminal}
\end{pframe}


\section{Loops}

\subsection{while statements}
\begin{pframe}
 \begin{pythonfile}{while.py}
  \begin{minted}[linenos]{python}
i = 0
while i < 5:
    print(i)
    i += 1
  \end{minted}
 \end{pythonfile}

 \begin{terminal}
  \color{white}{
  \begin{lstlisting}[frame=,style=,numbers=none]
$ python while.py
0
1
2
3
4
  \end{lstlisting}}
 \end{terminal}
\end{pframe}

\subsection{Iterating over a sequence}
\begin{pframe}
 \begin{pythonfile}{for.py}
  \begin{minted}[linenos]{python}
colors = ['red', 'white', 'blue', 'orange']
cities = ['leiden', 'utrecht', 'warmond', 'san francisco']

# The for statement can iterate over sequence items.
for color in colors:
    print(color)

for character in 'blue':
    print(character)
  \end{minted}
 \end{pythonfile}
\end{pframe}

\begin{pframe}
 \begin{terminal}
  \color{white}{
  \begin{lstlisting}[frame=,style=,numbers=none]
$ python for.py
red
white
blue
orange
b
l
u
e  \end{lstlisting}}
 \end{terminal}
\end{pframe}



\section{Hands on!}
\begin{pframe}
 \vspace{-0.5cm}
 \begin{enumerate}
  \item
  \begin{enumerate}[a]
  \item
  \end{enumerate}
 \end{enumerate}
\end{pframe}


% Make the acknowledgements slide.
\makeAcknowledgementsSlide{
  \begin{tabular}{ll}
    Martijn Vermaat\\
    Jeroen Laros\\
    Jonathan Vis
  \end{tabular}
}

\end{document}