% !TEX encoding = UTF-8 Unicode \documentclass{beamer} \usepackage[utf8]{inputenc} \usepackage{hyperref} \usepackage{listings} \lstset{language=Python, showstringspaces=false} \usetheme{Madrid} \hypersetup{colorlinks=true} \title{Introduction à la programmation avec Python \\ (chapitre 2)} \author{Dimitri Merejkowsky} \institute{E2L} \begin{document} \frame{\titlepage} \begin{frame} Note: \\~\\ Les sources sont sur GitHub: \url{https://github.com/E2L/cours-python/tree/master/sources}. \\~\\ Mais il vaut mieux recopier le code vous-mêmes. \end{frame} \begin{frame} \frametitle{Plan} \begin{itemize} \item Retours sur le chapitre 1 \item Fonctions \item Structures de données \end{itemize} \end{frame} \begin{frame}[fragile] \centering \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title} Retours sur le chapitre 1 \end{beamercolorbox} \end{frame} \begin{frame}[fragile] \frametitle{Retour sur les strings} \begin{lstlisting} >>> text = "Je suis un message\nSur deux lignes") >>> print(text) Je suis un message Sur deux lignes \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Concaténation implicite} \begin{lstlisting} >>> text = "Je suis une " "longue" " string" >>> text 'Je suis une longue string' \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Concaténer des strings (2)} \begin{lstlisting} message = ( "ligne 1\n" "ligne 2\n" ) \end{lstlisting} Les parenthèse permettent d'aller à la ligne dans le code :) \end{frame} \begin{frame}[fragile] \frametitle{Répéter une string} \begin{lstlisting} >>> "argh " * 3 argh argh argh \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Faire une longue string sur plusieurs lignes} \begin{lstlisting} poeme = """ Ceci est un poeme Qui contient "des quotes" Et parle d'autre choses ... """ \end{lstlisting} \begin{block}{Note} Marche aussi avec des "triples-simple-quotes", mais c'est moins lisible :P \end{block} \end{frame} \begin{frame}[fragile] \frametitle{Retour sur input()} On peut afficher un message avant de lire l'entrée utilisateur. \begin{lstlisting} # A adapter import random secret = random.randint() print("Devine le nombre auquel je pense") while True: reponse = input("Ta reponse: ") response = int(response) ... \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Retour sur print()} On peut spécifier le caractère de fin. \textbackslash n par défaut. \begin{lstlisting} # Dans hello.py print("Bonjour", end=" ") print("monde", end="!\n") \end{lstlisting} \begin{lstlisting} $ python hello.py Bonjour monde! \end{lstlisting} \end{frame} \begin{frame}[fragile] \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title} Fonctions \end{beamercolorbox} \end{frame} \begin{frame}[fragile] \frametitle{Sans arguments} \begin{lstlisting} def say_hello(): print("Hello") say_hello() \end{lstlisting} \vfill Notez l'utilisation des parenthèses pour \emph{appeler} la fonction \end{frame} \begin{frame}[fragile] \frametitle{Définition simple} \begin{lstlisting} def add(a, b): return a + b a = 1 b = 2 c = add(a, b) print(c) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Paramètres nommés} \begin{lstlisting} def greet(name, shout=False): result = "Hello, " result += name if shout: result += "!" return result >>> greet("John") 'Hello, John' >>> greet("Jane", shout=True) 'Hello, Jane!' \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Note} \texttt{print()} est une fonction :) \end{frame} \begin{frame}[fragile] \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title} Structures de données \end{beamercolorbox} \end{frame} \begin{frame}[fragile] \frametitle{Créer une liste} \begin{lstlisting} >>> ma_liste = list() # liste vide >>> ma_liste = [] # aussi une liste vide >>> ma_liste = [1, 2, 3] # trois entiers \end{lstlisting} \vfill \begin{alertblock}{Note} \texttt{list()} est \textbf{aussi} une fonction :) \end{alertblock} \end{frame} \begin{frame}[fragile] \frametitle{Listes hétérogènes} On peut mettre des types différents dans une même liste: \begin{lstlisting} >>> pommes_et_carottes = [True, 2, "three"] \end{lstlisting} \vfill Et même des listes dans des listes: \begin{lstlisting} >>> liste_de_liste = [[1, 2], ["one", "two"]] \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Connaître la taille d'une liste} Avec la fonction \texttt{len()}: \vfill \begin{lstlisting} >>> liste_vide = [] >>> len(liste_vide) 0 >>> autre_liste = [1, 2, 3] >>> len(autre_liste) 3 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Indexer une liste} \begin{lstlisting} >>> liste = [1, 2, 3] >>> liste[0] # ca commence a zero 1 >>> liste[4] # erreur! \end{lstlisting} \vfill Astuce: l'index maximal est \texttt{len(list) -1}. Retenez-le :) \end{frame} \begin{frame}[fragile] \frametitle{Modifer une liste} \begin{lstlisting} >>> liste = [1, 2, 3] >>> liste[1] = 4 >>> liste [1, 4, 3] \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Itérer sur les éléments d'une liste} \begin{lstlisting} names = ["Alice", "Bob", "Charlie"] for name in names: print("Bonjour", name) Bonjour Alice Bonjour Bob Bonjour Charlie \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Test de présence} Avec le mot-clé \texttt{in}: \vfill \begin{lstlisting} >>> fruits = ["pomme", "banane"] >>> "pomme" in fruits True >>> "orange" in fruits False \end{lstlisting} \end{frame} \end{document}