aboutsummaryrefslogtreecommitdiff
path: root/src/Lecture4/slides/6-PythonIntroduction.tex
blob: 6c2ec4593ea803f608d0794cead4daf335ad1c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
\documentclass[11pt]{beamer}
\usetheme{Madrid}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{svg}

\author[\texttt{sebastiano.tronto@uni.lu}]{Sebastiano Tronto}
\title[Python Intro and a bit of Sage]%
{A Practical Introduction to Python (and a bit of Sage)}
\logo{\includegraphics[scale=0.1]{img/unilu.jpg}} 
%\institute{University of Luxembourg} 

\date{2021-04-02} 

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}{Second part - schedule}
  \begin{tabular}{l|c|c|l}
    \textbf{Date} & \textbf{Topics} & \textbf{Homework} & \textbf{Deadline}  \\
    \hline
    April 2   & Python ``review'', a bit of Sage & (see March 26) & April 18 \\
    \hline
    April 23  & Sage: Algebra and Crypto & Homework 3 & May 9                \\
    \hline
    May 7     & Sage: Analysis and Statistics  & Homework 4 & May 30         \\
    \hline
    May 21    & Fast code, other software  &
  \end{tabular}
\end{frame}


\begin{frame}{What is programming?}
  \begin{columns}
    \column{0.35\textwidth}
      \includegraphics[scale=0.12]{img/laptop.png}
    \column{0.1\textwidth}
      \begin{center}\includegraphics[scale=0.12]{img/equal.png}\end{center}
    \column{0.35\textwidth}
      \includegraphics[scale=0.9]{img/calc.jpg}
  \end{columns}
  \begin{itemize}
    \item Software (apps): commands written in a \emph{programming language}
    \item Programming language: mix of English and symbols
    \item The code is \emph{compiled} to machine language (C, C++) or\\
          \emph{interpreted} by some software in the middle (Python)
  \end{itemize}
\end{frame}


\begin{frame}{Python}
  \begin{columns}
    \column{0.7\textwidth}
      \url{https://www.python.org}

      \vspace{0.3cm}
      \begin{itemize}
        \item Interpreted: slower than C, usable interactively
        \item Simple syntax, easy to learn
        \item Very popular
        \item Sage is based on Python
      \end{itemize}
    \column{0.25\textwidth}
      \includegraphics[scale=0.16]{img/python.png}
  \end{columns}
\end{frame}

\begin{frame}{The interpreter - Python as a calculator}
  \begin{center}
    \includegraphics[scale=0.3]{img/term.png}
  \end{center}
\end{frame}

\begin{frame}{The interpreter - Python as a calculator}
  \url{https://www.python.org/shell}

  \vspace{0.3cm}
  \begin{itemize}
    \item Each line executed as you enter it, result is printed
    \item Usual math operations work: try them!
      \begin{center}
        \texttt{a+b, a-b, a*b, a/b}\\
        \texttt{a**b (power), a//b (integer division), a\%b (remainder)}
      \end{center}
    \item More math functions:
      \begin{center} \begin{tabular}{l}
        \texttt{import math} \\
        \texttt{math.sqrt(3)}
      \end{tabular} \end{center}
  \end{itemize}
\end{frame}


\begin{frame}{Help!}
  \begin{itemize}
    \item Type \texttt{help()} for interactive help
    \item Try \texttt{help("math")} and \texttt{help("import")}
    \item What does
      \begin{center} \texttt{from math import *} \end{center}
    do?
  \end{itemize}
\end{frame}

\begin{frame}{Variables}
  \texttt{variable\_name = value \qquad \# This is an assignment}

  \vspace{0.3cm}
  \begin{itemize}
    \item Save results, use them later
    \item \texttt{variable\_name}: combination of letters, numbers, underscores
    \item \texttt{=} always means \emph{assignment}, never \emph{equality}
  \end{itemize}
\end{frame}

\begin{frame}{Types}
  \texttt{type(variable\_name) \qquad \# Get the type of a variable}

  \vspace{0.3cm}
  \begin{itemize}
    %\item Tells the computer how to read a variable
    \item In other languages you must specify the type of a variable
    \item Python figures out automatically (\emph{dynamic typing})
    \item Each type allows different operations
  \end{itemize}
\end{frame}


\begin{frame}{Other types: Boolean and String}
  \texttt{my\_bool = True}

  \texttt{s1 = "hello!" \qquad \# Same as 'hello!'}

  \vspace{0.3cm}
  \begin{itemize}
    \item \texttt{bool}: \texttt{True} or \texttt{False}
      \begin{itemize}
        \item Operations on \texttt{bool}:
              \texttt{and}, \texttt{or}, \texttt{not}
        \item Operations with boolean result: \texttt{==}, \texttt{!=},
              \texttt{>}, \texttt{<}, \texttt{>=}, \texttt{<=}
      \end{itemize}
    \item \texttt{str}: a string of characters
      \begin{itemize}
        \item Useful operations:

          \begin{tabular}{ll}
            \texttt{len({\bf str})} & \texttt{\# Length, integer value}   \\
            \texttt{{\bf str} + {\bf str}} & \texttt{\# Concatenation} \\
            \texttt{{\bf int} * {\bf str}} & \texttt{\# Repetition}
          \end{tabular}
      \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Lists and sets}
  %\texttt{(2.5, True, "hello") \qquad \# Tuple}

  \texttt{[2.5, True, "hello"] \qquad \# List}

  \texttt{\{2.5, True, "hello"\} \qquad \# Set}

  \vspace{0.3cm}
  \begin{itemize}
    %\item Different collections of objects
    %\item Tuple: immutable
    \item Lists: keep order and duplicates
    \item Sets: disregard order and duplicates, allow set operations
  \end{itemize}
\end{frame}


\begin{frame}{Lists and sets}
  Things in common (\emph{\texttt{A} is a list or a set}):

  \vspace{0.3cm}
  \begin{itemize}
    \item \texttt{len(A)}: number of elements (\texttt{\bf int})
    \item \texttt{x in A}: check if \texttt{x} is in \texttt{A}
          (\texttt{\bf bool})
    \item If \texttt{A} contains numbers: \texttt{max(A)}, \texttt{min(A)},
          \texttt{sum(A)}
  \end{itemize}

  \vspace{0.3cm}
  Pass from one type to the other: \texttt{set(A)} and \texttt{list(A)}
\end{frame}

\begin{frame}{List (and set) comprehension}
  \texttt{[x**2 {\bf for} x {\bf in} [-1,4,1,0] {\bf if} x < 3]
          \quad\# Result: [1,1,0]}

  \texttt{\{x**2 {\bf for} x {\bf in} [-1,4,1,0] {\bf if} x < 3\}
          \quad\# Result: \{0,1\}}

  \vspace{0.3cm}
  \begin{itemize}
    \item Mathematical way to define lists and sets
    \item Complete syntax:

          \vspace{0.2cm}
          \texttt{[f(x,y,\dots) {\bf for} x {\bf in} L {\bf for} y {\bf in} M
                   \dots \quad {\bf if} cond(x,y,\dots)]}

          \vspace{0.2cm}
          \begin{itemize}
            \item Use as many variables \texttt{x, y, \dots} as you want
            \item \texttt{L, M, \dots} are lists or sets or other collections
            \item \texttt{f(x,y,\dots)} is any expression depending on the
                  variables
            \item \texttt{cond(x,y,\dots)} has Boolean value
          \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Lists: access elements and sublists}
  \begin{tabular}{ll}
    \texttt{A[i]} &
    \texttt{\# i-th element of A ($i\in \{0,\dots,\texttt{len(A)}-1\}$)} \\
    \texttt{A[i] = value} & \texttt{\# Change i-th element of A} \\
    \texttt{A[i:j:k]} & \texttt{\# Sublist from A[i] to A[j] with step k} \\
    \texttt{A[i:j]} & \texttt{\# Same as A[i:j:1]} \\
    \texttt{A[i:]} & \texttt{\# Same as A[i:len(A):1]} \\
    \texttt{A[:j]} & \texttt{\# Same as A[0:j:1]}
  \end{tabular}
\end{frame}

\begin{frame}{List operations}
  \begin{tabular}{ll}
    \texttt{A.append(x)} & \texttt{\# Append x to A ({\bf change A})} \\
    \texttt{A.insert(i,x)} &
    \texttt{\# Insert x in position i ({\bf change A})} \\
    \texttt{del A[i]} & \texttt{\# Remove i-th element of A ({\bf change A})}\\
  \end{tabular}

  \vspace{1cm}
  \begin{tabular}{ll}
    \texttt{A+B} & \texttt{\# Concatenation of A and B ({\bf list})} \\
    \texttt{A*n} & \texttt{\# Repetition of A ({\bf list})} \\
  \end{tabular}
\end{frame}

\begin{frame}{Set operations}
  \begin{tabular}{ll}
    \texttt{A.add(x)} & \texttt{\# Add x to A ({\bf change A})} \\
    \texttt{A.remove(x)} & \texttt{\# Remove x from A ({\bf change A})}\\
  \end{tabular}

  \vspace{0.5cm}
  \begin{tabular}{ll}
    \texttt{A < B} (or \texttt{A <= B}) &
    \texttt{\# A contained in (or equal to) B ({\bf bool})} \\
    \texttt{A > B} (or \texttt{A >= B}) &
    \texttt{\# A containes (or is equal to) B ({\bf bool})} \\
  \end{tabular}

  \vspace{0.5cm}
  \begin{tabular}{ll}
    \texttt{A | B} & \texttt{\# Union ({\bf set})} \\
    \texttt{A \& B} & \texttt{\# Intersection ({\bf set})}\\
    \texttt{A - B} & \texttt{\# Set difference ({\bf set})} \\
  \end{tabular}
\end{frame}

\begin{frame}{Writing more complex programs}
  \begin{center}\includegraphics[scale=0.25]{img/geany.png}\end{center}
\end{frame}

\begin{frame}{Non-interactive Python}
  \begin{itemize}
    \item You can write a file (for example with \url{https://www.geany.org})
    \item Output results with \texttt{print("string", or, other, values)}
    \item Get input (\texttt{str}) with \texttt{x = input("Prompt: ")},
          convert with \texttt{int(x)} or \texttt{float(x)}\dots
    \item Blocks of code: use \emph{indentation} (see next slides)
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{\texttt{if} statement}
  \begin{columns}
    \column{0.45\textwidth}
      \texttt{{\bf if} \emph{condition}:}

      \texttt{\qquad instruction1}
      
      \texttt{\qquad instruction2}

      \texttt{\qquad \dots}

      \texttt{{\bf else}: \# This is optional}

      \texttt{\qquad other inst}
    \column{0.55\textwidth}
      \begin{tikzpicture}
        \tikzstyle{s} = [rectangle, rounded corners, text centered,
                                                       draw=black, fill=red!30]
        \tikzstyle{p} = [rectangle, text centered, draw=black, fill=orange!30]
        \tikzstyle{d} = [rectangle, text centered, draw=black, fill=green!30]

        \node(start)  [s]                     {Start};
        \node(cond)   [d, below of=start]     {\texttt{\emph{condition}}?};
        \node(inst1)  [p, right of=cond, xshift=2.7cm] {\texttt{instruction1}};
        \node(inst2)  [p, below of=inst1]     {\texttt{instruction2}};
        \node(dots)   [p, below of=inst2]     {\texttt{\dots}};
        \node(other)  [p, below of=cond, yshift=-0.5cm]  {\texttt{other inst}};
        \node(end)    [s, below of=other, yshift=-0.5cm] {End};

        \draw[->] (start) -- (cond);
        \draw[->] (cond) -- node[anchor=south] {\texttt{True}} (inst1);
        \draw[->] (inst1) -- (inst2);
        \draw[->] (inst2) -- (dots);
        \draw[->] (cond) -- node[anchor=east] {\texttt{False}} (other);
        \draw[->] (dots) |- (end);
        \draw[->] (other) -- (end);
      \end{tikzpicture}
  \end{columns}
\end{frame}


\begin{frame}[fragile]{\texttt{while} loop}
  \begin{columns}
    \column{0.45\textwidth}
      \texttt{{\bf while} \emph{condition}:}

      \texttt{\qquad instruction1}
      
      \texttt{\qquad instruction2}

      \texttt{\qquad \dots}
    \column{0.55\textwidth}
      \begin{tikzpicture}
        \tikzstyle{s} = [rectangle, rounded corners, text centered,
                                                       draw=black, fill=red!30]
        \tikzstyle{p} = [rectangle, text centered, draw=black, fill=orange!30]
        \tikzstyle{d} = [rectangle, text centered, draw=black, fill=green!30]

        \node(start)  [s]                     {Start};
        \node(cond)   [d, below of=start]     {\texttt{\emph{condition}}?};
        \node(inst1)  [p, right of=cond, xshift=2.7cm] {\texttt{instruction1}};
        \node(inst2)  [p, below of=inst1]     {\texttt{instruction2}};
        \node(dots)   [p, below of=inst2]     {\texttt{\dots}};
        \node(end)    [s, below of=other, yshift=-0.5cm] {End};

        \draw[->] (start) -- (cond);
        \draw[->] (cond) -- node[anchor=south] {\texttt{True}} (inst1);
        \draw[->] (inst1) -- (inst2);
        \draw[->] (inst2) -- (dots);
        \draw[->] (cond) -- node[anchor=east] {\texttt{False}} (end);
        \draw[->] (dots) -| ($(cond.south)+(0.4,0)$);
      \end{tikzpicture}
  \end{columns}
\end{frame}

\begin{frame}{\texttt{for} loop}
  \texttt{{\bf for} i {\bf in} A:}

  \texttt{\qquad instruction1}
  
  \texttt{\qquad instruction2}

  \texttt{\qquad \dots}

  \vspace{0.3cm}
  \begin{itemize}
    \item Repeats instructions as \texttt{i} varies in \texttt{A}
    \item \texttt{A} can be list, set or other collection
    \item Example: \texttt{A} can be \texttt{range(\emph{a,b,step})}
  \end{itemize}
\end{frame}


\begin{frame}{Functions}
  \texttt{{\bf def} f(x, y, \dots): }

  \texttt{\qquad instruction1}
  
  \texttt{\qquad instruction2}

  \texttt{\qquad \dots}

  \texttt{\qquad {\bf return} some\_value}

  \vspace{0.3cm}
  \begin{itemize}
    \item Useful to divide programs into ``pieces''
    \item The result of \texttt{f(x,y,\dots)} is given by \texttt{return \dots}
  \end{itemize}
\end{frame}

\begin{frame}{An example of function (with recursion)}
  \texttt{def fibonacci(n):}

  \texttt{\qquad if n == 0:}

  \texttt{\qquad \qquad return 0}

  \texttt{\qquad if n == 1:}

  \texttt{\qquad \qquad return 1}

  \texttt{\qquad return fibonacci(n-1) + fibonacci(n-2)}

  \vspace{0.3cm}
  \begin{itemize}
    \item Elegant, but slow (in this case)
    \item Can get stuck in infinite loop: when?
  \end{itemize}
\end{frame}

\begin{frame}{Sage}
  \begin{center}
    \includesvg[scale=0.5]{img/sage}

    \url{https://www.sagemath.org}
  \end{center}

  \vspace{0.3cm}
  \begin{itemize}
    \item Mathematical software, uses Python as a language
    \item Use it interactively or with Jupyter notebook
    \item Try it online: \url{https://sagecell.sagemath.org} or
          \url{https://cocalc.com/app}
  \end{itemize}
\end{frame}

\begin{frame}{Differences with Python}
  \begin{tabular}{l|l}
    \textbf{Python} & \textbf{Sage}                                          \\
    \texttt{{\color{gray}>>>} {\color{blue}type(5)}} &
    \texttt{{\color{gray}sage:} {\color{blue}type(5)}}                       \\
    \texttt{<class 'int'>} & \texttt{<class 'sage.rings.integer.Integer'>}   \\
    \texttt{{\color{gray}>>>} {\color{blue}5/2}} &
    \texttt{{\color{gray}sage:} {\color{blue}5/2}}                           \\
    \texttt{2.5} & \texttt{5/2}                                              \\
    \texttt{{\color{gray}>>>} {\color{blue}type(5/2)}} &
    \texttt{{\color{gray}sage:} {\color{blue}type(5/2)}}                     \\
    \texttt{<class 'float'>} &
    \texttt{<class 'sage.rings.rational.Rational'>}                          \\
    \texttt{{\color{gray}>>>} {\color{blue}type(2.5)}} &
    \texttt{{\color{gray}sage:} {\color{blue}type(2.5)}}                     \\
    \texttt{<class 'float'>} &
    \texttt{<class 'sage.rings.real\_mpfr.RealLiteral'>}                     \\
    \texttt{{\color{gray}>>>} {\color{blue}5**3}} &
    \texttt{{\color{gray}sage:} {\color{blue}5\^{}3}}                        \\
    \texttt{125} & \texttt{125}
  \end{tabular}
\end{frame}

\begin{frame}{Sage Documentation}
  \begin{itemize}
    \item Tutorial (guided examples): type \texttt{tutorial()} or visit
          \url{https://doc.sagemath.org/html/en/tutorial}
    \item \texttt{help()}: works as in Python
    \item Reference manual (detailed technical information):
          \url{https://doc.sagemath.org/html/en/reference}
  \end{itemize}
\end{frame}


\end{document}

Generated with cgit - Back to sebastiano.tronto.net