Skip to content

Commit

Permalink
ENH Better while example. Add exercise break
Browse files Browse the repository at this point in the history
  • Loading branch information
luispedro committed Oct 8, 2012
1 parent a47b6e3 commit 68871d8
Showing 1 changed file with 60 additions and 20 deletions.
80 changes: 60 additions & 20 deletions lectures/slides-2.5-day/02-python-i.tex
Expand Up @@ -25,9 +25,12 @@
\frametitle{Python Language History}

\begin{block}{History}
Python was started in the late 80's. It was intended to be both \alert{easy to teach} and \alert{industrial strength}.

It is (has always been) open-source and has become one of the most widely used languages (top 10).
\begin{itemize}
\item Python was started in the late 80's.
\item It was intended to be both \alert{easy to teach} and \alert{industrial strength}.
\item It is (has always been) open-source.
\item In the last 10 years, it has become one of the most widely used languages (top 10).
\end{itemize}
\end{block}
\end{frame}

Expand Down Expand Up @@ -139,11 +142,11 @@
\begin{frame}[fragile]
\frametitle{More Complex Example}

What is 25 + 20\%?
What is 25 times 5?

\pause
\begin{python}
print 25 * 1.2
print 25 * 5
\end{python}
\note{Demo the python shell.

Expand Down Expand Up @@ -211,7 +214,7 @@
\frametitle{Example}

\begin{python}
values = [0.11, 0.23, 0.16, 0.18, 0.23, 0.19]
values = [0.11, -0.23, -0.16, 0.18, 0.23, 0.19]

sum = 0.0
sum2 = 0.0
Expand All @@ -230,7 +233,7 @@
\frametitle{Example}

\begin{python}
values = [0.11, 0.23, 0.16, 0.18, 0.23, 0.19]
values = [0.11, -0.23, -0.16, 0.18, 0.23, 0.19]

sum = 0.0
sum2 = 0.0
Expand All @@ -249,7 +252,7 @@
\frametitle{Example}

\begin{python}
values = [0.11, 0.23, 0.16, 0.18, 0.23, 0.19]
values = [0.11, -0.23, -0.16, 0.18, 0.23, 0.19]

mu = 0.0
mu2 = 0.0
Expand All @@ -268,7 +271,7 @@
\frametitle{Example}

\begin{python}
values = [0.11, 0.23, 0.16, 0.18, 0.23, 0.19]
values = [0.11, -0.23, -0.16, 0.18, 0.23, 0.19]

mu = 0.0
mu2 = 0.0
Expand All @@ -283,24 +286,61 @@
\end{python}
\end{frame}


\begin{frame}[fragile]
\frametitle{Loops (II)}
\frametitle{Exercise}

Adapt the code to ignore negative numbers.

\pause

\begin{python}
values = [0.11, -0.23, -0.16, 0.18, 0.23, 0.19]

mu = 0.0
mu2 = 0.0
n = 0.0
for v in values:
if v >= 0.0:
mu += v
mu2 += v * v
n += 1

mu /= n
mu2 /= n
print 'Average: {0}'.format(mu)
print 'Std Dev: {0}'.format(mu2 - mu*mu)
\end{python}
\note{
Negative? strictly negative or just negative.
}

students = ['Luis',...]
\end{frame}

looking_for = 'Anna'

i = 0
while students[i] != looking_for:
i += 1
\begin{frame}[fragile]
\frametitle{Loops (II)}

if students[i] == looking_for:
print 'Found her'
else:
print "Sorry, she's not registered!"
\begin{block}{Greatest Common Divisor (Euclid's Method)}
\[
gcd(a,b) = \begin{cases}
a & \text{if $b = a$}\\
gcd(a-b, b) & \text{if $a > b$} \\
gcd(a, b-a) & \text{o.w.}
\end{cases}
\]
\end{block}

\pause
\begin{python}
a = 9344
b = 6497

while a != b:
if a > b:
a,b = a-b,b
else:
a,b = a,b-a
print a
\end{python}

\end{frame}
Expand Down

0 comments on commit 68871d8

Please sign in to comment.