Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programming course
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
courses
Programming course
Commits
47f6304b
Commit
47f6304b
authored
6 years ago
by
Mihai Lefter
Browse files
Options
Downloads
Patches
Plain Diff
Started flow control
parent
0e850227
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
introduction/flow_control/flow_control.tex
+313
-0
313 additions, 0 deletions
introduction/flow_control/flow_control.tex
with
313 additions
and
0 deletions
introduction/flow_control/flow_control.tex
0 → 100644
+
313
−
0
View file @
47f6304b
\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}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment