|
@@ -0,0 +1,249 @@ |
|
|
|
|
|
\documentclass{beamer} |
|
|
|
|
|
|
|
|
|
|
|
\usepackage[utf8]{inputenc} |
|
|
|
|
|
\usepackage{hyperref} |
|
|
|
|
|
\usepackage{minted} |
|
|
|
|
|
|
|
|
|
|
|
\usetheme{Madrid} |
|
|
|
|
|
|
|
|
|
|
|
\hypersetup{colorlinks=true} |
|
|
|
|
|
|
|
|
|
|
|
\title{Introduction à la programmation avec Python (chapitre 2)} |
|
|
|
|
|
\author{Dimitri Merejkowsky} |
|
|
|
|
|
\institute{E2L} |
|
|
|
|
|
|
|
|
|
|
|
\begin{document} |
|
|
|
|
|
|
|
|
|
|
|
\frame{\titlepage} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame} |
|
|
|
|
|
|
|
|
|
|
|
\frametitle{Session 2} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame} |
|
|
|
|
|
|
|
|
|
|
|
Note: \\~\\ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Les sources sont |
|
|
|
|
|
\href{https://github.com/E2L/cours-python/tree/master/sources}{sur GitHub}. \\~\\ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mais il vaut mieux recopier le code vous-mêmes. |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{frame} |
|
|
|
|
|
|
|
|
|
|
|
\frametitle{Plan} |
|
|
|
|
|
|
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item Retour sur le chapitre 1 |
|
|
|
|
|
\item Structures de données |
|
|
|
|
|
\item Fonctions |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Retour sur input()} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{minted}{python} |
|
|
|
|
|
# À adapter |
|
|
|
|
|
import random |
|
|
|
|
|
secret = random.randint() |
|
|
|
|
|
|
|
|
|
|
|
print("Devine le nombre auquel je pense") |
|
|
|
|
|
while True: |
|
|
|
|
|
reponse = input("Ta réponse: ") |
|
|
|
|
|
response = int(response) |
|
|
|
|
|
... |
|
|
|
|
|
\end{minted} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Retour sur print} |
|
|
|
|
|
|
|
|
|
|
|
\begin{minted}{python} |
|
|
|
|
|
>>> a = 1 |
|
|
|
|
|
>>> b = 2 |
|
|
|
|
|
>>> print("a is", a, "b is", b) |
|
|
|
|
|
a is 1, b is 2 |
|
|
|
|
|
\end{minted} |
|
|
|
|
|
|
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item On peut fournir plusieurs valeurs, séparées par des virgules |
|
|
|
|
|
\item \textit{print()} insère des espaces |
|
|
|
|
|
\item et va à la ligne |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Retour sur print (2)} |
|
|
|
|
|
|
|
|
|
|
|
\begin{minted}{python} |
|
|
|
|
|
a = 1 |
|
|
|
|
|
b = 2 |
|
|
|
|
|
print("a=", 1, "b=2", sep="", end="") |
|
|
|
|
|
\end{minted} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Retour sur les strings} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Répéter une string} |
|
|
|
|
|
|
|
|
|
|
|
\begin{minted}{python} |
|
|
|
|
|
>>> "argh " * 3 |
|
|
|
|
|
argh argh argh |
|
|
|
|
|
\end{minted} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Faire une longue string sur plusieurs lignes} |
|
|
|
|
|
|
|
|
|
|
|
\begin{minted}{python} |
|
|
|
|
|
poeme = """ |
|
|
|
|
|
Ceci est un poème |
|
|
|
|
|
|
|
|
|
|
|
Qui contient "des quotes" |
|
|
|
|
|
Et parle d'autre choses ... |
|
|
|
|
|
""" |
|
|
|
|
|
\end{minted} |
|
|
|
|
|
|
|
|
|
|
|
\begin{block}{Note} |
|
|
|
|
|
Marche aussi avec des "triples-simple-quotes", mais c'est moins lisible :P |
|
|
|
|
|
\end{block} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Concaténer des strings |
|
|
|
|
|
|
|
|
|
|
|
%!python |
|
|
|
|
|
%message = ( |
|
|
|
|
|
%"Première ligne\n" |
|
|
|
|
|
%"Deuxième ligne\n" |
|
|
|
|
|
%) |
|
|
|
|
|
|
|
|
|
|
|
%Les parenthèse permettent d'aller à la ligne dans le code :) |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Slicer des strings |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> message = "Bonjour, monde !" |
|
|
|
|
|
%>>> message[0] # ça commence à zéro |
|
|
|
|
|
%"B" |
|
|
|
|
|
%>>> message[15] |
|
|
|
|
|
%"!" |
|
|
|
|
|
%>>>> message[-1] # compter à l'envers |
|
|
|
|
|
%"!" |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Slicer des strings (2) |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> message = "Bonjour, monde !" |
|
|
|
|
|
%>>> message[1:4] # début, fin |
|
|
|
|
|
%'onj' |
|
|
|
|
|
%>>> message[:7] # début implicite |
|
|
|
|
|
%'Bonjour' |
|
|
|
|
|
%>>> message[9:-2] # fin négative |
|
|
|
|
|
%'monde' |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Listes |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Créer une liste |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> my_list = [] # liste vide |
|
|
|
|
|
%>>> primes = [2, 3, 5, 7, 11] # liste d'entiers |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Listes hétérogènes |
|
|
|
|
|
|
|
|
|
|
|
%On peut mettre des types différents dans une même liste: |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> pommes_et_carottes = [True, 2, "three"] |
|
|
|
|
|
|
|
|
|
|
|
%Et même des listes dans des listes: |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> liste_de_liste = [[1, 2, 3], ["one", "two", "three"]] |
|
|
|
|
|
|
|
|
|
|
|
%---- |
|
|
|
|
|
|
|
|
|
|
|
%# Slicer des listes |
|
|
|
|
|
|
|
|
|
|
|
%Même principe que pour les strings! |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> liste = [1, 2, 3] |
|
|
|
|
|
%>>> liste[0:2] |
|
|
|
|
|
%[1, 2] |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Modifier une liste |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> liste = [1, 2, 3] |
|
|
|
|
|
%>>> liste[1] = 4 |
|
|
|
|
|
%>>> liste |
|
|
|
|
|
%[1, 4, 3] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%*Attention*: ça ne marche pas avec les strings: |
|
|
|
|
|
|
|
|
|
|
|
%!pycon |
|
|
|
|
|
%>>> message = "Bonjour, monde !" |
|
|
|
|
|
%>>> message[-1] = "?" |
|
|
|
|
|
%TypeError: 'str' object does not support item assignment |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
%# Boucles for |
|
|
|
|
|
|
|
|
|
|
|
%Itérer sur les éléments d'une liste: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%!python |
|
|
|
|
|
%names = ["Alice", "Bob", "Charlie"] |
|
|
|
|
|
%for name in names: |
|
|
|
|
|
%print("Bonjour", name) |
|
|
|
|
|
|
|
|
|
|
|
%Bonjour Alice |
|
|
|
|
|
%Bonjour Bob |
|
|
|
|
|
%Bonjour Charlie |
|
|
|
|
|
|
|
|
|
|
|
%--- |
|
|
|
|
|
|
|
|
|
|
|
\end{document} |