mathsoftware

A course about LaTeX and SageMath
git clone https://git.tronto.net/mathsoftware
Download | Log | Files | Refs | README | LICENSE

6-PythonIntroduction.tex (15168B)


      1 \documentclass[11pt]{beamer}
      2 \usetheme{Madrid}
      3 \usepackage[utf8]{inputenc}
      4 \usepackage{amsmath}
      5 
      6 \usepackage{tikz}
      7 \usetikzlibrary{calc}
      8 \usepackage{svg}
      9 
     10 \author[\texttt{sebastiano.tronto@uni.lu}]{Sebastiano Tronto}
     11 \title[Python Intro and a bit of Sage]%
     12 {A Practical Introduction to Python (and a bit of Sage)}
     13 \logo{\includegraphics[scale=0.1]{img/unilu.jpg}} 
     14 %\institute{University of Luxembourg} 
     15 
     16 \date{2021-04-02} 
     17 
     18 \begin{document}
     19 
     20 \begin{frame}
     21   \titlepage
     22 \end{frame}
     23 
     24 \begin{frame}{Second part - schedule}
     25   \begin{tabular}{l|c|c|l}
     26     \textbf{Date} & \textbf{Topics} & \textbf{Homework} & \textbf{Deadline}  \\
     27     \hline
     28     April 2   & Python ``review'', a bit of Sage & (see March 26) & April 18 \\
     29     \hline
     30     April 23  & Sage: Algebra and Crypto & Homework 3 & May 9                \\
     31     \hline
     32     May 7     & Sage: Analysis and Statistics  & Homework 4 & May 30         \\
     33     \hline
     34     May 21    & Fast code, other software  &
     35   \end{tabular}
     36 \end{frame}
     37 
     38 
     39 \begin{frame}{What is programming?}
     40   \begin{columns}
     41     \column{0.35\textwidth}
     42       \includegraphics[scale=0.12]{img/laptop.png}
     43     \column{0.1\textwidth}
     44       \begin{center}\includegraphics[scale=0.12]{img/equal.png}\end{center}
     45     \column{0.35\textwidth}
     46       \includegraphics[scale=0.9]{img/calc.jpg}
     47   \end{columns}
     48   \begin{itemize}
     49     \item Software (apps): commands written in a \emph{programming language}
     50     \item Programming language: mix of English and symbols
     51     \item The code is \emph{compiled} to machine language (C, C++) or\\
     52           \emph{interpreted} by some software in the middle (Python)
     53   \end{itemize}
     54 \end{frame}
     55 
     56 
     57 \begin{frame}{Python}
     58   \begin{columns}
     59     \column{0.7\textwidth}
     60       \url{https://www.python.org}
     61 
     62       \vspace{0.3cm}
     63       \begin{itemize}
     64         \item Interpreted: slower than C, usable interactively
     65         \item Simple syntax, easy to learn
     66         \item Very popular
     67         \item Sage is based on Python
     68       \end{itemize}
     69     \column{0.25\textwidth}
     70       \includegraphics[scale=0.16]{img/python.png}
     71   \end{columns}
     72 \end{frame}
     73 
     74 \begin{frame}{The interpreter - Python as a calculator}
     75   \begin{center}
     76     \includegraphics[scale=0.3]{img/term.png}
     77   \end{center}
     78 \end{frame}
     79 
     80 \begin{frame}{The interpreter - Python as a calculator}
     81   \url{https://www.python.org/shell}
     82 
     83   \vspace{0.3cm}
     84   \begin{itemize}
     85     \item Each line executed as you enter it, result is printed
     86     \item Usual math operations work: try them!
     87       \begin{center}
     88         \texttt{a+b, a-b, a*b, a/b}\\
     89         \texttt{a**b (power), a//b (integer division), a\%b (remainder)}
     90       \end{center}
     91     \item More math functions:
     92       \begin{center} \begin{tabular}{l}
     93         \texttt{import math} \\
     94         \texttt{math.sqrt(3)}
     95       \end{tabular} \end{center}
     96   \end{itemize}
     97 \end{frame}
     98 
     99 
    100 \begin{frame}{Help!}
    101   \begin{itemize}
    102     \item Type \texttt{help()} for interactive help
    103     \item Try \texttt{help("math")} and \texttt{help("import")}
    104     \item What does
    105       \begin{center} \texttt{from math import *} \end{center}
    106     do?
    107   \end{itemize}
    108 \end{frame}
    109 
    110 \begin{frame}{Variables}
    111   \texttt{variable\_name = value \qquad \# This is an assignment}
    112 
    113   \vspace{0.3cm}
    114   \begin{itemize}
    115     \item Save results, use them later
    116     \item \texttt{variable\_name}: combination of letters, numbers, underscores
    117     \item \texttt{=} always means \emph{assignment}, never \emph{equality}
    118   \end{itemize}
    119 \end{frame}
    120 
    121 \begin{frame}{Types}
    122   \texttt{type(variable\_name) \qquad \# Get the type of a variable}
    123 
    124   \vspace{0.3cm}
    125   \begin{itemize}
    126     %\item Tells the computer how to read a variable
    127     \item In other languages you must specify the type of a variable
    128     \item Python figures out automatically (\emph{dynamic typing})
    129     \item Each type allows different operations
    130   \end{itemize}
    131 \end{frame}
    132 
    133 
    134 \begin{frame}{Other types: Boolean and String}
    135   \texttt{my\_bool = True}
    136 
    137   \texttt{s1 = "hello!" \qquad \# Same as 'hello!'}
    138 
    139   \vspace{0.3cm}
    140   \begin{itemize}
    141     \item \texttt{bool}: \texttt{True} or \texttt{False}
    142       \begin{itemize}
    143         \item Operations on \texttt{bool}:
    144               \texttt{and}, \texttt{or}, \texttt{not}
    145         \item Operations with boolean result: \texttt{==}, \texttt{!=},
    146               \texttt{>}, \texttt{<}, \texttt{>=}, \texttt{<=}
    147       \end{itemize}
    148     \item \texttt{str}: a string of characters
    149       \begin{itemize}
    150         \item Useful operations:
    151 
    152           \begin{tabular}{ll}
    153             \texttt{len({\bf str})} & \texttt{\# Length, integer value}   \\
    154             \texttt{{\bf str} + {\bf str}} & \texttt{\# Concatenation} \\
    155             \texttt{{\bf int} * {\bf str}} & \texttt{\# Repetition}
    156           \end{tabular}
    157       \end{itemize}
    158   \end{itemize}
    159 \end{frame}
    160 
    161 \begin{frame}{Lists and sets}
    162   %\texttt{(2.5, True, "hello") \qquad \# Tuple}
    163 
    164   \texttt{[2.5, True, "hello"] \qquad \# List}
    165 
    166   \texttt{\{2.5, True, "hello"\} \qquad \# Set}
    167 
    168   \vspace{0.3cm}
    169   \begin{itemize}
    170     %\item Different collections of objects
    171     %\item Tuple: immutable
    172     \item Lists: keep order and duplicates
    173     \item Sets: disregard order and duplicates, allow set operations
    174   \end{itemize}
    175 \end{frame}
    176 
    177 
    178 \begin{frame}{Lists and sets}
    179   Things in common (\emph{\texttt{A} is a list or a set}):
    180 
    181   \vspace{0.3cm}
    182   \begin{itemize}
    183     \item \texttt{len(A)}: number of elements (\texttt{\bf int})
    184     \item \texttt{x in A}: check if \texttt{x} is in \texttt{A}
    185           (\texttt{\bf bool})
    186     \item If \texttt{A} contains numbers: \texttt{max(A)}, \texttt{min(A)},
    187           \texttt{sum(A)}
    188   \end{itemize}
    189 
    190   \vspace{0.3cm}
    191   Pass from one type to the other: \texttt{set(A)} and \texttt{list(A)}
    192 \end{frame}
    193 
    194 \begin{frame}{List (and set) comprehension}
    195   \texttt{[x**2 {\bf for} x {\bf in} [-1,4,1,0] {\bf if} x < 3]
    196           \quad\# Result: [1,1,0]}
    197 
    198   \texttt{\{x**2 {\bf for} x {\bf in} [-1,4,1,0] {\bf if} x < 3\}
    199           \quad\# Result: \{0,1\}}
    200 
    201   \vspace{0.3cm}
    202   \begin{itemize}
    203     \item Mathematical way to define lists and sets
    204     \item Complete syntax:
    205 
    206           \vspace{0.2cm}
    207           \texttt{[f(x,y,\dots) {\bf for} x {\bf in} L {\bf for} y {\bf in} M
    208                    \dots \quad {\bf if} cond(x,y,\dots)]}
    209 
    210           \vspace{0.2cm}
    211           \begin{itemize}
    212             \item Use as many variables \texttt{x, y, \dots} as you want
    213             \item \texttt{L, M, \dots} are lists or sets or other collections
    214             \item \texttt{f(x,y,\dots)} is any expression depending on the
    215                   variables
    216             \item \texttt{cond(x,y,\dots)} has Boolean value
    217           \end{itemize}
    218   \end{itemize}
    219 \end{frame}
    220 
    221 \begin{frame}{Lists: access elements and sublists}
    222   \begin{tabular}{ll}
    223     \texttt{A[i]} &
    224     \texttt{\# i-th element of A ($i\in \{0,\dots,\texttt{len(A)}-1\}$)} \\
    225     \texttt{A[i] = value} & \texttt{\# Change i-th element of A} \\
    226     \texttt{A[i:j:k]} & \texttt{\# Sublist from A[i] to A[j] with step k} \\
    227     \texttt{A[i:j]} & \texttt{\# Same as A[i:j:1]} \\
    228     \texttt{A[i:]} & \texttt{\# Same as A[i:len(A):1]} \\
    229     \texttt{A[:j]} & \texttt{\# Same as A[0:j:1]}
    230   \end{tabular}
    231 \end{frame}
    232 
    233 \begin{frame}{List operations}
    234   \begin{tabular}{ll}
    235     \texttt{A.append(x)} & \texttt{\# Append x to A ({\bf change A})} \\
    236     \texttt{A.insert(i,x)} &
    237     \texttt{\# Insert x in position i ({\bf change A})} \\
    238     \texttt{del A[i]} & \texttt{\# Remove i-th element of A ({\bf change A})}\\
    239   \end{tabular}
    240 
    241   \vspace{1cm}
    242   \begin{tabular}{ll}
    243     \texttt{A+B} & \texttt{\# Concatenation of A and B ({\bf list})} \\
    244     \texttt{A*n} & \texttt{\# Repetition of A ({\bf list})} \\
    245   \end{tabular}
    246 \end{frame}
    247 
    248 \begin{frame}{Set operations}
    249   \begin{tabular}{ll}
    250     \texttt{A.add(x)} & \texttt{\# Add x to A ({\bf change A})} \\
    251     \texttt{A.remove(x)} & \texttt{\# Remove x from A ({\bf change A})}\\
    252   \end{tabular}
    253 
    254   \vspace{0.5cm}
    255   \begin{tabular}{ll}
    256     \texttt{A < B} (or \texttt{A <= B}) &
    257     \texttt{\# A contained in (or equal to) B ({\bf bool})} \\
    258     \texttt{A > B} (or \texttt{A >= B}) &
    259     \texttt{\# A containes (or is equal to) B ({\bf bool})} \\
    260   \end{tabular}
    261 
    262   \vspace{0.5cm}
    263   \begin{tabular}{ll}
    264     \texttt{A | B} & \texttt{\# Union ({\bf set})} \\
    265     \texttt{A \& B} & \texttt{\# Intersection ({\bf set})}\\
    266     \texttt{A - B} & \texttt{\# Set difference ({\bf set})} \\
    267   \end{tabular}
    268 \end{frame}
    269 
    270 \begin{frame}{Writing more complex programs}
    271   \begin{center}\includegraphics[scale=0.25]{img/geany.png}\end{center}
    272 \end{frame}
    273 
    274 \begin{frame}{Non-interactive Python}
    275   \begin{itemize}
    276     \item You can write a file (for example with \url{https://www.geany.org})
    277     \item Output results with \texttt{print("string", or, other, values)}
    278     \item Get input (\texttt{str}) with \texttt{x = input("Prompt: ")},
    279           convert with \texttt{int(x)} or \texttt{float(x)}\dots
    280     \item Blocks of code: use \emph{indentation} (see next slides)
    281   \end{itemize}
    282 \end{frame}
    283 
    284 \begin{frame}[fragile]{\texttt{if} statement}
    285   \begin{columns}
    286     \column{0.45\textwidth}
    287       \texttt{{\bf if} \emph{condition}:}
    288 
    289       \texttt{\qquad instruction1}
    290       
    291       \texttt{\qquad instruction2}
    292 
    293       \texttt{\qquad \dots}
    294 
    295       \texttt{{\bf else}: \# This is optional}
    296 
    297       \texttt{\qquad other inst}
    298     \column{0.55\textwidth}
    299       \begin{tikzpicture}
    300         \tikzstyle{s} = [rectangle, rounded corners, text centered,
    301                                                        draw=black, fill=red!30]
    302         \tikzstyle{p} = [rectangle, text centered, draw=black, fill=orange!30]
    303         \tikzstyle{d} = [rectangle, text centered, draw=black, fill=green!30]
    304 
    305         \node(start)  [s]                     {Start};
    306         \node(cond)   [d, below of=start]     {\texttt{\emph{condition}}?};
    307         \node(inst1)  [p, right of=cond, xshift=2.7cm] {\texttt{instruction1}};
    308         \node(inst2)  [p, below of=inst1]     {\texttt{instruction2}};
    309         \node(dots)   [p, below of=inst2]     {\texttt{\dots}};
    310         \node(other)  [p, below of=cond, yshift=-0.5cm]  {\texttt{other inst}};
    311         \node(end)    [s, below of=other, yshift=-0.5cm] {End};
    312 
    313         \draw[->] (start) -- (cond);
    314         \draw[->] (cond) -- node[anchor=south] {\texttt{True}} (inst1);
    315         \draw[->] (inst1) -- (inst2);
    316         \draw[->] (inst2) -- (dots);
    317         \draw[->] (cond) -- node[anchor=east] {\texttt{False}} (other);
    318         \draw[->] (dots) |- (end);
    319         \draw[->] (other) -- (end);
    320       \end{tikzpicture}
    321   \end{columns}
    322 \end{frame}
    323 
    324 
    325 \begin{frame}[fragile]{\texttt{while} loop}
    326   \begin{columns}
    327     \column{0.45\textwidth}
    328       \texttt{{\bf while} \emph{condition}:}
    329 
    330       \texttt{\qquad instruction1}
    331       
    332       \texttt{\qquad instruction2}
    333 
    334       \texttt{\qquad \dots}
    335     \column{0.55\textwidth}
    336       \begin{tikzpicture}
    337         \tikzstyle{s} = [rectangle, rounded corners, text centered,
    338                                                        draw=black, fill=red!30]
    339         \tikzstyle{p} = [rectangle, text centered, draw=black, fill=orange!30]
    340         \tikzstyle{d} = [rectangle, text centered, draw=black, fill=green!30]
    341 
    342         \node(start)  [s]                     {Start};
    343         \node(cond)   [d, below of=start]     {\texttt{\emph{condition}}?};
    344         \node(inst1)  [p, right of=cond, xshift=2.7cm] {\texttt{instruction1}};
    345         \node(inst2)  [p, below of=inst1]     {\texttt{instruction2}};
    346         \node(dots)   [p, below of=inst2]     {\texttt{\dots}};
    347         \node(end)    [s, below of=other, yshift=-0.5cm] {End};
    348 
    349         \draw[->] (start) -- (cond);
    350         \draw[->] (cond) -- node[anchor=south] {\texttt{True}} (inst1);
    351         \draw[->] (inst1) -- (inst2);
    352         \draw[->] (inst2) -- (dots);
    353         \draw[->] (cond) -- node[anchor=east] {\texttt{False}} (end);
    354         \draw[->] (dots) -| ($(cond.south)+(0.4,0)$);
    355       \end{tikzpicture}
    356   \end{columns}
    357 \end{frame}
    358 
    359 \begin{frame}{\texttt{for} loop}
    360   \texttt{{\bf for} i {\bf in} A:}
    361 
    362   \texttt{\qquad instruction1}
    363   
    364   \texttt{\qquad instruction2}
    365 
    366   \texttt{\qquad \dots}
    367 
    368   \vspace{0.3cm}
    369   \begin{itemize}
    370     \item Repeats instructions as \texttt{i} varies in \texttt{A}
    371     \item \texttt{A} can be list, set or other collection
    372     \item Example: \texttt{A} can be \texttt{range(\emph{a,b,step})}
    373   \end{itemize}
    374 \end{frame}
    375 
    376 
    377 \begin{frame}{Functions}
    378   \texttt{{\bf def} f(x, y, \dots): }
    379 
    380   \texttt{\qquad instruction1}
    381   
    382   \texttt{\qquad instruction2}
    383 
    384   \texttt{\qquad \dots}
    385 
    386   \texttt{\qquad {\bf return} some\_value}
    387 
    388   \vspace{0.3cm}
    389   \begin{itemize}
    390     \item Useful to divide programs into ``pieces''
    391     \item The result of \texttt{f(x,y,\dots)} is given by \texttt{return \dots}
    392   \end{itemize}
    393 \end{frame}
    394 
    395 \begin{frame}{An example of function (with recursion)}
    396   \texttt{def fibonacci(n):}
    397 
    398   \texttt{\qquad if n == 0:}
    399 
    400   \texttt{\qquad \qquad return 0}
    401 
    402   \texttt{\qquad if n == 1:}
    403 
    404   \texttt{\qquad \qquad return 1}
    405 
    406   \texttt{\qquad return fibonacci(n-1) + fibonacci(n-2)}
    407 
    408   \vspace{0.3cm}
    409   \begin{itemize}
    410     \item Elegant, but slow (in this case)
    411     \item Can get stuck in infinite loop: when?
    412   \end{itemize}
    413 \end{frame}
    414 
    415 \begin{frame}{Sage}
    416   \begin{center}
    417     \includesvg[scale=0.5]{img/sage}
    418 
    419     \url{https://www.sagemath.org}
    420   \end{center}
    421 
    422   \vspace{0.3cm}
    423   \begin{itemize}
    424     \item Mathematical software, uses Python as a language
    425     \item Use it interactively or with Jupyter notebook
    426     \item Try it online: \url{https://sagecell.sagemath.org} or
    427           \url{https://cocalc.com/app}
    428   \end{itemize}
    429 \end{frame}
    430 
    431 \begin{frame}{Differences with Python}
    432   \begin{tabular}{l|l}
    433     \textbf{Python} & \textbf{Sage}                                          \\
    434     \texttt{{\color{gray}>>>} {\color{blue}type(5)}} &
    435     \texttt{{\color{gray}sage:} {\color{blue}type(5)}}                       \\
    436     \texttt{<class 'int'>} & \texttt{<class 'sage.rings.integer.Integer'>}   \\
    437     \texttt{{\color{gray}>>>} {\color{blue}5/2}} &
    438     \texttt{{\color{gray}sage:} {\color{blue}5/2}}                           \\
    439     \texttt{2.5} & \texttt{5/2}                                              \\
    440     \texttt{{\color{gray}>>>} {\color{blue}type(5/2)}} &
    441     \texttt{{\color{gray}sage:} {\color{blue}type(5/2)}}                     \\
    442     \texttt{<class 'float'>} &
    443     \texttt{<class 'sage.rings.rational.Rational'>}                          \\
    444     \texttt{{\color{gray}>>>} {\color{blue}type(2.5)}} &
    445     \texttt{{\color{gray}sage:} {\color{blue}type(2.5)}}                     \\
    446     \texttt{<class 'float'>} &
    447     \texttt{<class 'sage.rings.real\_mpfr.RealLiteral'>}                     \\
    448     \texttt{{\color{gray}>>>} {\color{blue}5**3}} &
    449     \texttt{{\color{gray}sage:} {\color{blue}5\^{}3}}                        \\
    450     \texttt{125} & \texttt{125}
    451   \end{tabular}
    452 \end{frame}
    453 
    454 \begin{frame}{Sage Documentation}
    455   \begin{itemize}
    456     \item Tutorial (guided examples): type \texttt{tutorial()} or visit
    457           \url{https://doc.sagemath.org/html/en/tutorial}
    458     \item \texttt{help()}: works as in Python
    459     \item Reference manual (detailed technical information):
    460           \url{https://doc.sagemath.org/html/en/reference}
    461   \end{itemize}
    462 \end{frame}
    463 
    464 
    465 \end{document}
    466