You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

350 lines
5.6 KiB

  1. % !TEX encoding = UTF-8 Unicode
  2. \documentclass{beamer}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{hyperref}
  5. \usepackage{listings}
  6. \lstset{language=Python, showstringspaces=false}
  7. \usetheme{Madrid}
  8. \hypersetup{colorlinks=true}
  9. \title{Introduction à la programmation avec Python \\ (chapitre 2)}
  10. \author{Dimitri Merejkowsky}
  11. \institute{E2L}
  12. \begin{document}
  13. \frame{\titlepage}
  14. \begin{frame}
  15. Note: \\~\\
  16. Les sources sont sur GitHub:
  17. \url{https://github.com/E2L/cours-python/tree/master/sources}. \\~\\
  18. Mais il vaut mieux recopier le code vous-mêmes.
  19. \end{frame}
  20. \begin{frame}
  21. \frametitle{Plan}
  22. \begin{itemize}
  23. \item Retours sur le chapitre 1
  24. \item Fonctions
  25. \item Structures de données
  26. \end{itemize}
  27. \end{frame}
  28. \begin{frame}[fragile]
  29. \centering
  30. \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
  31. Retours sur le chapitre 1
  32. \end{beamercolorbox}
  33. \end{frame}
  34. \begin{frame}[fragile]
  35. \frametitle{Retour sur les strings}
  36. \begin{lstlisting}
  37. >>> text = "Je suis un message\nSur deux lignes")
  38. >>> print(text)
  39. Je suis un message
  40. Sur deux lignes
  41. \end{lstlisting}
  42. \end{frame}
  43. \begin{frame}[fragile]
  44. \frametitle{Concaténation implicite}
  45. \begin{lstlisting}
  46. >>> text = "Je suis une " "longue" " string"
  47. >>> text
  48. 'Je suis une longue string'
  49. \end{lstlisting}
  50. \end{frame}
  51. \begin{frame}[fragile]
  52. \frametitle{Concaténer des strings (2)}
  53. \begin{lstlisting}
  54. message = (
  55. "ligne 1\n"
  56. "ligne 2\n"
  57. )
  58. \end{lstlisting}
  59. Les parenthèse permettent d'aller à la ligne dans le code :)
  60. \end{frame}
  61. \begin{frame}[fragile]
  62. \frametitle{Répéter une string}
  63. \begin{lstlisting}
  64. >>> "argh " * 3
  65. argh argh argh
  66. \end{lstlisting}
  67. \end{frame}
  68. \begin{frame}[fragile]
  69. \frametitle{Faire une longue string sur plusieurs lignes}
  70. \begin{lstlisting}
  71. poeme = """
  72. Ceci est un poeme
  73. Qui contient "des quotes"
  74. Et parle d'autre choses ...
  75. """
  76. \end{lstlisting}
  77. \begin{block}{Note}
  78. Marche aussi avec des "triples-simple-quotes", mais c'est moins lisible :P
  79. \end{block}
  80. \end{frame}
  81. \begin{frame}[fragile]
  82. \frametitle{Retour sur input()}
  83. On peut afficher un message avant de lire l'entrée utilisateur.
  84. \begin{lstlisting}
  85. # A adapter
  86. import random
  87. secret = random.randint()
  88. print("Devine le nombre auquel je pense")
  89. while True:
  90. reponse = input("Ta reponse: ")
  91. response = int(response)
  92. ...
  93. \end{lstlisting}
  94. \end{frame}
  95. \begin{frame}[fragile]
  96. \frametitle{Retour sur print()}
  97. On peut spécifier le caractère de fin. \textbackslash n par défaut.
  98. \begin{lstlisting}
  99. # Dans hello.py
  100. print("Bonjour", end=" ")
  101. print("monde", end="!\n")
  102. \end{lstlisting}
  103. \begin{lstlisting}
  104. $ python hello.py
  105. Bonjour monde!
  106. \end{lstlisting}
  107. \end{frame}
  108. \begin{frame}[fragile]
  109. \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
  110. Fonctions
  111. \end{beamercolorbox}
  112. \end{frame}
  113. \begin{frame}[fragile]
  114. \frametitle{Sans arguments}
  115. \begin{lstlisting}
  116. def say_hello():
  117. print("Hello")
  118. say_hello()
  119. \end{lstlisting}
  120. \vfill
  121. Notez l'utilisation des parenthèses pour \emph{appeler} la fonction
  122. \end{frame}
  123. \begin{frame}[fragile]
  124. \frametitle{Définition simple}
  125. \begin{lstlisting}
  126. def add(a, b):
  127. return a + b
  128. a = 1
  129. b = 2
  130. c = add(a, b)
  131. print(c)
  132. \end{lstlisting}
  133. \end{frame}
  134. \begin{frame}[fragile]
  135. \frametitle{Paramètres nommés}
  136. \begin{lstlisting}
  137. def greet(name, shout=False):
  138. result = "Hello, "
  139. result += name
  140. if shout:
  141. result += "!"
  142. return result
  143. >>> greet("John")
  144. 'Hello, John'
  145. >>> greet("Jane", shout=True)
  146. 'Hello, Jane!'
  147. \end{lstlisting}
  148. \end{frame}
  149. \begin{frame}[fragile]
  150. \frametitle{Note}
  151. \texttt{print()} est une fonction :)
  152. \end{frame}
  153. \begin{frame}[fragile]
  154. \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
  155. Structures de données
  156. \end{beamercolorbox}
  157. \end{frame}
  158. \begin{frame}[fragile]
  159. \frametitle{Créer une liste}
  160. \begin{lstlisting}
  161. >>> ma_liste = list() # liste vide
  162. >>> ma_liste = [] # aussi une liste vide
  163. >>> ma_liste = [1, 2, 3] # trois entiers
  164. \end{lstlisting}
  165. \vfill
  166. \begin{alertblock}{Note}
  167. \texttt{list()} est \textbf{aussi} une fonction :)
  168. \end{alertblock}
  169. \end{frame}
  170. \begin{frame}[fragile]
  171. \frametitle{Listes hétérogènes}
  172. On peut mettre des types différents dans une même liste:
  173. \begin{lstlisting}
  174. >>> pommes_et_carottes = [True, 2, "three"]
  175. \end{lstlisting}
  176. \vfill
  177. Et même des listes dans des listes:
  178. \begin{lstlisting}
  179. >>> liste_de_liste = [[1, 2], ["one", "two"]]
  180. \end{lstlisting}
  181. \end{frame}
  182. \begin{frame}[fragile]
  183. \frametitle{Connaître la taille d'une liste}
  184. Avec la fonction \texttt{len()}:
  185. \vfill
  186. \begin{lstlisting}
  187. >>> liste_vide = []
  188. >>> len(liste_vide)
  189. 0
  190. >>> autre_liste = [1, 2, 3]
  191. >>> len(autre_liste)
  192. 3
  193. \end{lstlisting}
  194. \end{frame}
  195. \begin{frame}[fragile]
  196. \frametitle{Indexer une liste}
  197. \begin{lstlisting}
  198. >>> liste = [1, 2, 3]
  199. >>> liste[0] # ca commence a zero
  200. 1
  201. >>> liste[4] # erreur!
  202. \end{lstlisting}
  203. \vfill
  204. Astuce: l'index maximal est \texttt{len(list) -1}. Retenez-le :)
  205. \end{frame}
  206. \begin{frame}[fragile]
  207. \frametitle{Modifer une liste}
  208. \begin{lstlisting}
  209. >>> liste = [1, 2, 3]
  210. >>> liste[1] = 4
  211. >>> liste
  212. [1, 4, 3]
  213. \end{lstlisting}
  214. \end{frame}
  215. \begin{frame}[fragile]
  216. \frametitle{Itérer sur les éléments d'une liste}
  217. \begin{lstlisting}
  218. names = ["Alice", "Bob", "Charlie"]
  219. for name in names:
  220. print("Bonjour", name)
  221. Bonjour Alice
  222. Bonjour Bob
  223. Bonjour Charlie
  224. \end{lstlisting}
  225. \end{frame}
  226. \begin{frame}[fragile]
  227. \frametitle{Test de présence}
  228. Avec le mot-clé \texttt{in}:
  229. \vfill
  230. \begin{lstlisting}
  231. >>> fruits = ["pomme", "banane"]
  232. >>> "pomme" in fruits
  233. True
  234. >>> "orange" in fruits
  235. False
  236. \end{lstlisting}
  237. \end{frame}
  238. \end{document}