Dictionary added
[linuxnijmegen-python.git] / p.tex
diff --git a/p.tex b/p.tex
index b5635a4..dc09889 100644 (file)
--- a/p.tex
+++ b/p.tex
@@ -3,6 +3,11 @@
 \frame{\titlepage}
 
 \section{Introduction}
+\begin{frame}
+       \frametitle{What is Python}
+       \includegraphics[width=\linewidth]{2.png}
+\end{frame}
+
 \begin{frame}
        \frametitle{What is Python}
        \begin{itemize}
 
 \begin{frame}[fragile]
        \frametitle{How to start}
-       \begin{block}{Interpreter}
+       \framesubtitle{Interpreter}
+       \begin{minted}[breaklines=true]{pycon}
+Python 3.5.1 (default, Dec  7 2015, 12:58:09)
+[GCC 5.2.0] on linux
+Type "help", "copyright", "credits" or "license" for more
+information.
+>>> print('Hello world!')
+Hello world!
+>>> i = 1972
+>>> print(str(i/46) + ' hoi')
+42.869565217391305 hoi
+       \end{minted}
+\end{frame}
 
+\begin{frame}[fragile]
+       \frametitle{How to start}
+       \framesubtitle{Source files}
+       \begin{minted}{python}
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+print('hello world')
+i = 1972
+print(str(i/46) + ' hoi')
+       \end{minted}
+\end{frame}
+
+\section{Basics}
+\begin{frame}[fragile]
+       \frametitle{Built-in types}
+       \begin{tabular}{ll}
+               \mintinline{python}{bool} & \mintinline{python}{True, False}\\
+               \mintinline{python}{int} & \mintinline{python}{1, -4, 999}\\
+               \mintinline{python}{complex} & \mintinline{python}{4j+1, j}\\
+               \mintinline{python}{float} & \mintinline{python}
+                       {1.0, 4.5, 5e9, float('NaN'), float('inf')}\\
+               \mintinline{python}{str} & \mintinline{python}
+                       {'hi', "hi", '"', "'", """hi"""}\\
+               \mintinline{python}{list} & \mintinline{python}
+                       {[], ..., [1, 2, 3], ['a', 42], [[], [1]]}\\
+               \mintinline{python}{tuple} & \mintinline{python}
+                       {(1,), (1,2), (1, 'a', True), ((4, 3), 4)}\\
+               \mintinline{python}{dict} & \mintinline{python}
+                       {{1: 'a', 2: 'b'}, {"hi": 42, "test": {}}, {}}\\
+               \mintinline{python}{set} & \mintinline{python}
+                       {{1, 2}, {"hi", "a"}, set()}\\
+               Others &  \mintinline{python}
+                       {range, bytes, bytearray, memoryview, frozenset}\\
+               Special & \mintinline{python}
+                       {type, None, ..., NotImplemented}\\
+       \end{tabular}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Basictypes: Operators}
+       \begin{block}{operators}
+               \begin{itemize}
+                       \item Numbers: \mintinline{python}{+ - / * // % @ **}
+                       \item Comparison: \mintinline{python}{< <= > >= == !=}
+                       \item Boolean: \mintinline{python}{and or not}
+                       \item Bitwise: \mintinline{python}{& | ^ ~ << >>}
+                       \item List/Dictionary: \mintinline{python}{[i] [i:j] in}
+                       \item In place: \mintinline{python}{*= += ...}
+                       \item Special: \mintinline{python}{is}
+               \end{itemize}
        \end{block}
+\end{frame}
 
-       \begin{block}{\texttt{.py} files}
-               \begin{minted}{python}
+\section{Control flow}
+\subsection{If, then, else}
+\begin{frame}[fragile]
+       \frametitle{Conditional execution}
+       \framesubtitle{\texttt{fizzbuzz.py}}
+       \begin{minted}{python}
 #!/usr/bin/env python3
-# -*- coding: utf-8 -*-
 
-print('hello world') #This is a comment
+i = int(input('Enter a number: '))
+if i % 3 == 0 and i % 5 == 0:
+    print('fizzbuzz')
+elif i % 3 == 0:
+    print('buzz')
+elif i % 5 == 0:
+    print('fizz')
+else:
+    print(i)
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Conditional execution 2}
+       \framesubtitle{\texttt{fizzbuzz.py}}
+       \begin{minted}{shell-session}
+frobnicator@frobmachine~$ python fizzbuzz.py
+Enter a number: 42
+buzz
+frobnicator@frobmachine~$ python fizzbuzz.py
+Enter a number: 15
+fizzbuzz
+frobnicator@frobmachine~$ python fizzbuzz.py
+Enter a number: 43
+43
+frobnicator@frobmachine~$ python fizzbuzz.py
+Enter a number: 9
+fizz
+       \end{minted}
+\end{frame}
+
+\subsection{While, for}
+\begin{frame}[fragile]
+       \frametitle{Looping}
+       \begin{minted}{python}
+ls = ["hello", "world", "!"]
+
+# Pythonic for loop
+for item in ls:
+    print(ls)
+
+# C style for loop
+for i in range(0, len(ls)):
+    print(ls[i])
+
+# While loop
+i = 10
+while i >= 0:
+    print(i);
+print('fire!')
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Looping: Escape rope}
+       \begin{minted}{python}
+# 100.000st prime, 100.001st prime, and a pseudoprime
+numbers = [1299709, 1299721, 1299709*1299721]
+for n in numbers:
+    for x in range(2, n):
+        if n % x == 0:
+            print(n, 'equals', x, '*', n//x)
+            break
+    else:
+        print(n, 'is a prime number')
+
+# 1299709 is a prime number
+# 1299721 is a prime number
+# 1689259081189 equals 1299709 * 1299721
+
+# pass, continue
+       \end{minted}
+\end{frame}
+
+\subsection{Functions}
+\begin{frame}[fragile]
+       \frametitle{Functions}
+       \begin{minted}{python}
+def fizzbuzzrange(n, m):
+    """Print fizzbuzz for all numbers between n and m"""
+    for i in range(n, m):
+        fizzbuzz(i)
+
+def fizzbuzz(n):
+    """Print for number i the fizzbuzz"""
+    if i % 3 == 0 and i % 5 == 0:
+        print('fizzbuzz')
+    elif i % 3 == 0:
+        print('buzz')
+    elif i % 5 == 0:
+        print('fizz')
+    else:
+        print(i)
+
+fizzbuzzrange(0, 1000)
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Functions: Advanced arguments}
+       \begin{minted}{python}
+def function(arg1, arg2, kwarg1="hi", kwarg2=42):
+    """Docstring,
+       run help(functionname) in the interpreter
+       to see this helpstring
+       """
+    pass
+
+def function(*args):
+    for i, a in enumerate(args):
+        print("argument: ", i, ": ", a)
+
+def function(**kwargs):
+    for k, v in kwargs:
+        print("argumentkey: ", k, "=", v)
+
+def function(arg1, arg2, arg3=4, *args, **kwargs):
+    pass
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Functions: Advanced functions}
+       \begin{minted}{python}
+def compare(this, that, comparefunc=lambda x, y: x > y):
+    return comparefunc(this, that)
+       \end{minted}
+       \begin{minted}{pycon}
+>>> compare(42, 2)
+True
+>>> compare(42, 2, lambda x, y: x < y)
+False
+>>> def compare_len(x, y):
+...     return len(x) > len(y)
+...
+>>> print(compare_len)
+<function compare_list at 0x7f075cd5b730>
+>>> compare([1, 2, 3, 4], [3, 4, 5])
+False
+>>> compare([1, 2, 3, 4], [3, 4, 5], compare_len)
+True
+       \end{minted}
+\end{frame}
+
+\section{Datastructures}
+\begin{frame}
+       \frametitle{Lists}
+       \begin{tabular}{ll}
+               In-situ & Copy\\
+               \mintinline{python}{lst.append(x)}\\
+               \mintinline{python}{lst.extend(x)} &
+                       \mintinline{python}{lst1 + lst2}\\
+               \mintinline{python}{lst.insert(i, x)}\\
+               \mintinline{python}{lst.remove(x)}\\
+               \mintinline{python}{lst.clear(x)} &
+                       \mintinline{python}{lst = []}\\
+               \mintinline{python}{lst.reverse(x)} &
+                       \mintinline{python}{reversed(lst)}\\
+               \mintinline{python}{lst.sort(x)} &
+                       \mintinline{python}{sorted(lst)}\\
+               \ldots
+       \end{tabular}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Dictionary}
+       \begin{minted}{pycon}
+>>> tel = {'jack': 4098, 'sape': 4139}
+>>> tel['guido'] = 4127
+>>> tel
+{'sape': 4139, 'guido': 4127, 'jack': 4098}
+>>> tel['jack']
+4098
+>>> del tel['sape']
+>>> tel['irv'] = 4127
+>>> tel
+{'guido': 4127, 'irv': 4127, 'jack': 4098}
+>>> list(tel.keys())
+['irv', 'guido', 'jack']
+>>> sorted(tel.keys())
+['guido', 'irv', 'jack']
+>>> 'guido' in tel
+True
+>>> 'jack' not in tel
+False
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Sequences}
+       \begin{block}{Tuples}
+               \begin{minted}{python}
+# Auto unpack
+x = (42, 43)
+x1, x2 = x
                \end{minted}
        \end{block}
+
+       \begin{block}{Sequence modifiers}
+               \begin{minted}{python}
+x = {3, 2, 1}
+lx = list(x)       # items in random order
+sx = sorted(x)     # [1, 2, 3]
+rx = reversed(sx)  # [3, 2, 1]
+rx = enumerate(rx) # [(0, 3), (1, 2), (2, 1)]
+               \end{minted}
+       \end{block}
+\end{frame}
+
+\section{Input/Output}
+\begin{frame}[fragile]
+       \frametitle{Strings}
+       \begin{minted}{pycon}
+>>> s = 'Hello, world.'
+>>> str(s)
+'Hello, world.'
+>>> repr(s)
+"'Hello, world.'"
+>>> str(1/7)
+'0.14285714285714285'
+>>> x = 10 * 3.25
+>>> y = 200 * 200
+>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
+>>> print(s)
+The value of x is 32.5, and y is 40000...
+>>> hello = 'hello, world\n'
+>>> hellos = repr(hello)
+>>> print(hellos)
+'hello, world\n'
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{String formatting}
+       \begin{minted}{python}
+'We are the {} who say "{}!"'.format('knights', 'Ni')
+# We are the knights who say "Ni!"
+
+'{0} {0} {1}'.format('ekke', 'ptang')
+# ekke ekke ptang
+
+# Number formatting and padding
+'{0:.3f} {1:03d} "{2:<5}" "{2:,>5}"'.format(
+       1.0/3, 42, 'hi')
+# '0.333 042 "hi   " ",,,hi"
+
+# Other Usefull string function
+','.join(['a', 'b', 'c'])
+'a,b,c'.split(',')
+       \end{minted}
+\end{frame}
+
+\section{And much more\ldots}
+\begin{frame}[fragile]
+       \frametitle{Error handling}
+       \begin{minted}{python}
+try:
+    x = 5/0
+except ZeroDivisionError as e:
+    print(e)
+
+try:
+    do_some_stuff()
+except:
+    rollback()
+    raise
+else:
+    commit()
+finally:
+    cleanup()
+       \end{minted}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{Libraries}
+       \mintinline{python}{import modulename}\\
+       \mintinline{python}{from modulename import name1, name2}\\
+       \begin{enumerate}
+               \item System: \mintinline{python}{os, sys, glob}
+               \item Math: \mintinline{python}{math, random, statistics}
+               \item Text: \mintinline{python}{re, locale, gettext, codecs}
+               \item Internet: \mintinline{python}{irllib, email, smtplib, poplib}
+               \item Data: \mintinline{python}{sqlite, json, csv, xml.etree}
+               \item Command line:
+                       \mintinline{python}{curses, urwid, readline, getopts, argparse}
+               \item GUI: \mintinline{python}{Tkinter, PySide/PyQt, PyGobject}
+               \item \ldots
+       \end{enumerate}
+\end{frame}
+
+\begin{frame}[fragile]
+       \frametitle{And much more\ldots}
+       \begin{itemize}
+               \item Function annotations: \mintinline{python}{def fun(n) -> str:}
+               \item Iteration through: \mintinline{python}{yield}
+               \item List comprehension: 
+                       \mintinline{python}{[x**2 for x in range(0, 5)]}
+               \item Functional style: \mintinline{python}{map, filter, reduce}
+               \item Classes, inheritance: \mintinline{python}{class}
+               \item Context blocks: \mintinline{python}{with}
+               \item String templating
+               \item Coding style: \url{https://www.python.org/dev/peps/pep-0008/}
+       \end{itemize}
 \end{frame}
 \end{document}