7 \frametitle{What is Python
}
8 \includegraphics[width=
\linewidth]{2.png
}
12 \frametitle{What is Python
}
22 \begin{frame
}[fragile
]
23 \frametitle{How to start
}
24 \framesubtitle{Interpreter
}
25 \begin{minted
}[breaklines=true
]{pycon
}
26 Python
3.5.1 (default, Dec
7 2015,
12:
58:
09)
28 Type "help", "copyright", "credits" or "license" for more
30 >>> print('Hello world!')
33 >>> print(str(i/
46) + ' hoi')
34 42.869565217391305 hoi
38 \begin{frame
}[fragile
]
39 \frametitle{How to start
}
40 \framesubtitle{Source files
}
41 \begin{minted
}{python
}
42 #!/usr/bin/env python3
43 # -*- coding: utf-
8 -*-
47 print(str(i/
46) + ' hoi')
52 \begin{frame
}[fragile
]
53 \frametitle{Built-in types
}
55 \mintinline{python
}{bool
} &
\mintinline{python
}{True, False
}\\
56 \mintinline{python
}{int
} &
\mintinline{python
}{1, -
4,
999}\\
57 \mintinline{python
}{complex
} &
\mintinline{python
}{4j+
1, j
}\\
58 \mintinline{python
}{float
} &
\mintinline{python
}
59 {1.0,
4.5,
5e9, float('NaN'), float('inf')
}\\
60 \mintinline{python
}{str
} &
\mintinline{python
}
61 {'hi', "hi", '"', "'", """hi"""
}\\
62 \mintinline{python
}{list
} &
\mintinline{python
}
63 {[], ...,
[1,
2,
3],
['a',
42],
[[],
[1]]}\\
64 \mintinline{python
}{tuple
} &
\mintinline{python
}
65 {(
1,), (
1,
2), (
1, 'a', True), ((
4,
3),
4)
}\\
66 \mintinline{python
}{dict
} &
\mintinline{python
}
67 {{1: 'a',
2: 'b'
},
{"hi":
42, "test":
{}},
{}}\\
68 \mintinline{python
}{set
} &
\mintinline{python
}
69 {{1,
2},
{"hi", "a"
}, set()
}\\
70 Others &
\mintinline{python
}
71 {range, bytes, bytearray, memoryview, frozenset
}\\
72 Special &
\mintinline{python
}
73 {type, None, ..., NotImplemented
}\\
77 \begin{frame
}[fragile
]
78 \frametitle{Basictypes: Operators
}
79 \begin{block
}{operators
}
81 \item Numbers:
\mintinline{python
}{+ - / * //
% @ **}
82 \item Comparison:
\mintinline{python
}{< <= > >= == !=
}
83 \item Boolean:
\mintinline{python
}{and or not
}
84 \item Bitwise:
\mintinline{python
}{& | ^ ~ << >>
}
85 \item List/Dictionary:
\mintinline{python
}{[i
] [i:j
] in
}
86 \item In place:
\mintinline{python
}{*= += ...
}
87 \item Special:
\mintinline{python
}{is
}
92 \section{Control flow
}
93 \subsection{If, then, else
}
94 \begin{frame
}[fragile
]
95 \frametitle{Conditional execution
}
96 \framesubtitle{\texttt{fizzbuzz.py
}}
97 \begin{minted
}{python
}
98 #!/usr/bin/env python3
100 i = int(input('Enter a number: '))
101 if i
% 3 == 0 and i % 5 == 0:
112 \begin{frame
}[fragile
]
113 \frametitle{Conditional execution
2}
114 \framesubtitle{\texttt{fizzbuzz.py
}}
115 \begin{minted
}{shell-session
}
116 frobnicator@frobmachine~$ python fizzbuzz.py
119 frobnicator@frobmachine~$ python fizzbuzz.py
122 frobnicator@frobmachine~$ python fizzbuzz.py
125 frobnicator@frobmachine~$ python fizzbuzz.py
131 \subsection{While, for
}
132 \begin{frame
}[fragile
]
134 \begin{minted
}{python
}
135 ls =
["hello", "world", "!"
]
142 for i in range(
0, len(ls)):
153 \begin{frame
}[fragile
]
154 \frametitle{Looping: Escape rope
}
155 \begin{minted
}{python
}
156 #
100.000st prime,
100.001st prime, and a pseudoprime
157 numbers =
[1299709,
1299721,
1299709*
1299721]
159 for x in range(
2, n):
161 print(n, 'equals', x, '*', n//x)
164 print(n, 'is a prime number')
166 #
1299709 is a prime number
167 #
1299721 is a prime number
168 #
1689259081189 equals
1299709 *
1299721
174 \subsection{Functions
}
175 \begin{frame
}[fragile
]
176 \frametitle{Functions
}
177 \begin{minted
}{python
}
178 def fizzbuzzrange(n, m):
179 """Print fizzbuzz for all numbers between n and m"""
180 for i in range(n, m):
184 """Print for number i the fizzbuzz"""
185 if i
% 3 == 0 and i % 5 == 0:
194 fizzbuzzrange(
0,
1000)
198 \begin{frame
}[fragile
]
199 \frametitle{Functions: Advanced arguments
}
200 \begin{minted
}{python
}
201 def function(arg1, arg2, kwarg1="hi", kwarg2=
42):
203 run help(functionname) in the interpreter
204 to see this helpstring
209 for i, a in enumerate(args):
210 print("argument: ", i, ": ", a)
212 def function(**kwargs):
214 print("argumentkey: ", k, "=", v)
216 def function(arg1, arg2, arg3=4, *args, **kwargs):
221 \begin{frame}[fragile]
222 \frametitle{Functions: Advanced functions}
223 \begin{minted}{python}
224 def compare(this, that, comparefunc=lambda x, y: x > y):
225 return comparefunc(this, that)
227 \begin{minted}{pycon}
230 >>> compare(42, 2, lambda x, y: x < y)
232 >>> def compare_len(x, y):
233 ... return len(x) > len(y)
235 >>> print(compare_len)
236 <function compare_list at 0x7f075cd5b730>
237 >>> compare([1, 2, 3, 4], [3, 4, 5])
239 >>> compare([1, 2, 3, 4], [3, 4, 5], compare_len)
244 \section{Datastructures}
249 \mintinline{python}{lst.append(x)}\\
250 \mintinline{python}{lst.extend(x)} &
251 \mintinline{python}{lst1 + lst2}\\
252 \mintinline{python}{lst.insert(i, x)}\\
253 \mintinline{python}{lst.remove(x)}\\
254 \mintinline{python}{lst.clear(x)} &
255 \mintinline{python}{lst = []}\\
256 \mintinline{python}{lst.reverse(x)} &
257 \mintinline{python}{reversed(lst)}\\
258 \mintinline{python}{lst.sort(x)} &
259 \mintinline{python}{sorted(lst)}\\
264 \begin{frame}[fragile]
265 \frametitle{Dictionary}
266 \begin{minted}{pycon}
267 >>> tel = {'jack': 4098, 'sape': 4139}
268 >>> tel['guido'] = 4127
270 {'sape': 4139, 'guido': 4127, 'jack': 4098}
274 >>> tel['irv'] = 4127
276 {'guido': 4127, 'irv': 4127, 'jack': 4098}
278 ['irv', 'guido', 'jack']
279 >>> sorted(tel.keys())
280 ['guido', 'irv', 'jack']
283 >>> 'jack' not in tel
288 \begin{frame}[fragile]
289 \frametitle{Sequences}
290 \begin{block}{Tuples}
291 \begin{minted}{python}
298 \begin{block}{Sequence modifiers}
299 \begin{minted}{python}
301 lx = list(x) # items in random order
302 sx = sorted(x) # [1, 2, 3]
303 rx = reversed(sx) # [3, 2, 1]
304 rx = enumerate(rx) # [(0, 3), (1, 2), (2, 1)]
309 \section{Input/Output}
310 \begin{frame}[fragile]
312 \begin{minted}{pycon}
313 >>> s = 'Hello, world.'
319 '0.14285714285714285'
322 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
324 The value of x is 32.5, and y is 40000...
325 >>> hello = 'hello, world\n'
326 >>> hellos = repr(hello)
332 \begin{frame}[fragile]
333 \frametitle{String formatting}
334 \begin{minted}{python}
335 'We are the {} who say "{}!"'.format('knights', 'Ni')
336 # We are the knights who say "Ni!"
338 '{0} {0} {1}'.format('ekke', 'ptang')
341 # Number formatting and padding
342 '{0:.3f} {1:03d} "{2:<5}" "{2:,>5}"'.format(
344 # '0.333 042 "hi " ",,,hi"
346 # Other Usefull string function
347 ','.join(['a', 'b', 'c'])
352 \section{And much more\ldots}
353 \begin{frame}[fragile]
354 \frametitle{Error handling}
355 \begin{minted}{python}
358 except ZeroDivisionError as e:
373 \begin{frame}[fragile]
374 \frametitle{Libraries}
375 \mintinline{python}{import modulename}\\
376 \mintinline{python}{from modulename import name1, name2}\\
378 \item System: \mintinline{python}{os, sys, glob}
379 \item Math: \mintinline{python}{math, random, statistics}
380 \item Text: \mintinline{python}{re, locale, gettext, codecs}
381 \item Internet: \mintinline{python}{irllib, email, smtplib, poplib}
382 \item Data: \mintinline{python}{sqlite, json, csv, xml.etree}
384 \mintinline{python}{curses, urwid, readline, getopts, argparse}
385 \item GUI: \mintinline{python}{Tkinter, PySide/PyQt, PyGobject}
390 \begin{frame}[fragile]
391 \frametitle{And much more\ldots}
393 \item Function annotations: \mintinline{python}{def fun(n) -> str:}
394 \item Iteration through: \mintinline{python}{yield}
395 \item List comprehension:
396 \mintinline{python}{[x**2 for x in range(0, 5)]}
397 \item Functional style: \mintinline{python}{map, filter, reduce}
398 \item Classes, inheritance: \mintinline{python}{class}
399 \item Context blocks: \mintinline{python}{with}
400 \item String templating
401 \item Coding style: \url{https://www.python.org/dev/peps/pep-0008/}