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.
 
 
 
 
 
 

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