From: Mart Lubbers Date: Sat, 5 Mar 2016 16:43:11 +0000 (+0100) Subject: update, a lot of things added X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=3dabda2dd56b78688b7502a956fa767ac062cc01;p=linuxnijmegen-python.git update, a lot of things added --- diff --git a/p.tex b/p.tex index 25a4693..9ebb806 100644 --- a/p.tex +++ b/p.tex @@ -23,7 +23,7 @@ \frametitle{How to start} \framesubtitle{Interpreter} \begin{minted}[breaklines=true]{pycon} -Python 3.5.1 (default, Dec 7 2015, 12:58:09) +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. @@ -58,7 +58,7 @@ print(str(i/46) + ' hoi') \mintinline{python}{float} & \mintinline{python} {1.0, 4.5, 5e9, float('NaN'), float('inf')}\\ \mintinline{python}{str} & \mintinline{python} - {'hi', "hi", '"', "'", """hi""", "\n\"\t", '\"'}\\ + {'hi', "hi", '"', "'", """hi"""}\\ \mintinline{python}{list} & \mintinline{python} {[], ..., [1, 2, 3], ['a', 42], [[], [1]]}\\ \mintinline{python}{tuple} & \mintinline{python} @@ -174,19 +174,205 @@ for n in numbers: \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) + +>>> 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}{reverse(lst)}\\ + \ldots + \end{tabular} +\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} -\section{Errors} +\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} -\section{Standard Library} +\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} diff --git a/pre.tex b/pre.tex index b54458d..07eba83 100644 --- a/pre.tex +++ b/pre.tex @@ -11,6 +11,9 @@ \graphicspath{{./img/}} +\setminted{% +} + \AtBeginSection[]{% \begin{frame} \frametitle{Table of Contents}