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.
 
 
 
 
 
 

494 lines
8.0 KiB

  1. \documentclass{beamer}
  2. \usepackage[utf8]{inputenc}
  3. \usepackage{hyperref}
  4. \usepackage{listings}
  5. \lstset{language=Python, showstringspaces=false}
  6. \usetheme{Madrid}
  7. \hypersetup{colorlinks=true}
  8. \title{Introduction à la programmation avec Python \\ (chapitre 2)}
  9. \author{Dimitri Merejkowsky}
  10. \institute{E2L}
  11. \begin{document}
  12. \frame{\titlepage}
  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. \end{frame}
  50. \begin{frame}[fragile]
  51. \frametitle{Concaténer des strings (2)}
  52. \begin{lstlisting}
  53. message = (
  54. "ligne 1\n"
  55. "ligne 2\n"
  56. )
  57. \end{lstlisting}
  58. Les parenthèse permettent d'aller à la ligne dans le code :)
  59. \end{frame}
  60. \begin{frame}[fragile]
  61. \frametitle{Répéter une string}
  62. \begin{lstlisting}
  63. >>> "argh " * 3
  64. argh argh argh
  65. \end{lstlisting}
  66. \end{frame}
  67. \begin{frame}[fragile]
  68. \frametitle{Faire une longue string sur plusieurs lignes}
  69. \begin{lstlisting}
  70. poeme = """
  71. Ceci est un poeme
  72. Qui contient "des quotes"
  73. Et parle d'autre choses ...
  74. """
  75. \end{lstlisting}
  76. \begin{block}{Note}
  77. Marche aussi avec des "triples-simple-quotes", mais c'est moins lisible :P
  78. \end{block}
  79. \end{frame}
  80. \begin{frame}[fragile]
  81. \frametitle{Retour sur input()}
  82. On peut afficher un message avant de lire l'entrée utilisateur.
  83. \begin{lstlisting}
  84. # A adapter
  85. import random
  86. secret = random.randint()
  87. print("Devine le nombre auquel je pense")
  88. while True:
  89. reponse = input("Ta reponse: ")
  90. response = int(response)
  91. ...
  92. \end{lstlisting}
  93. \end{frame}
  94. \begin{frame}[fragile]
  95. \frametitle{Retour sur print()}
  96. On peut spécifier le caractère de fin. \textbackslash n par défaut.
  97. \begin{lstlisting}
  98. # Dans hello.py
  99. print("Bonjour", end=" ")
  100. print("monde", end="!\n")
  101. \end{lstlisting}
  102. \begin{lstlisting}
  103. $ python hello.py
  104. Bonjour monde!
  105. \end{lstlisting}
  106. \end{frame}
  107. \begin{frame}[fragile]
  108. \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
  109. Fonctions
  110. \end{beamercolorbox}
  111. \end{frame}
  112. \begin{frame}[fragile]
  113. \frametitle{Sans arguments}
  114. \begin{lstlisting}
  115. def say_hello():
  116. print("Hello")
  117. say_hello()
  118. \end{lstlisting}
  119. \vfill
  120. Notez l'utilisation des parenthèses pour \emph{appeler} la fonction
  121. \end{frame}
  122. \begin{frame}[fragile]
  123. \frametitle{Définition simple}
  124. \begin{lstlisting}
  125. def add(a, b):
  126. return a + b
  127. a = 1
  128. b = 2
  129. c = add(a, b)
  130. print(c)
  131. \end{lstlisting}
  132. \end{frame}
  133. \begin{frame}[fragile]
  134. \frametitle{Paramètres nommés}
  135. \begin{lstlisting}
  136. def greet(name, shout=False):
  137. result = "Hello, "
  138. result += name
  139. if shout:
  140. result += "!"
  141. return result
  142. >>> greet("John")
  143. 'Hello, John'
  144. >>> greet("Jane", shout=True)
  145. 'Hello, Jane!'
  146. \end{lstlisting}
  147. \end{frame}
  148. \begin{frame}[fragile]
  149. \frametitle{Note}
  150. \texttt{print()} est une fonction :)
  151. \end{frame}
  152. \begin{frame}[fragile]
  153. \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
  154. Structures de données
  155. \end{beamercolorbox}
  156. \end{frame}
  157. \begin{frame}[fragile]
  158. \frametitle{Créer une liste}
  159. \begin{lstlisting}
  160. >>> ma_liste = list() # liste vide
  161. >>> ma_liste = [] # aussi une liste vide
  162. >>> ma_liste = [1, 2, 3] # trois entiers
  163. \end{lstlisting}
  164. \vfill
  165. \begin{alertblock}{Note}
  166. \texttt{list()} est \textbf{aussi} une fonction :)
  167. \end{alertblock}
  168. \end{frame}
  169. \begin{frame}[fragile]
  170. \frametitle{Listes hétérogènes}
  171. On peut mettre des types différents dans une même liste:
  172. \begin{lstlisting}
  173. >>> pommes_et_carottes = [True, 2, "three"]
  174. \end{lstlisting}
  175. \vfill
  176. Et même des listes dans des listes:
  177. \begin{lstlisting}
  178. >>> liste_de_liste = [[1, 2], ["one", "two"]]
  179. \end{lstlisting}
  180. \end{frame}
  181. \begin{frame}[fragile]
  182. \frametitle{Connaître la taille d'une liste}
  183. Avec la fonction \texttt{len()}:
  184. \vfill
  185. \begin{lstlisting}
  186. >>> liste_vide = []
  187. >>> len(liste_vide)
  188. 0
  189. >>> autre_liste = [1, 2, 3]
  190. >>> len(autre_liste)
  191. 3
  192. \end{lstlisting}
  193. \end{frame}
  194. \begin{frame}[fragile]
  195. \frametitle{Indexer une liste}
  196. \begin{lstlisting}
  197. >>> liste = [1, 2, 3]
  198. >>> liste[0] # ca commence a zero
  199. 1
  200. >>> liste[4] # erreur!
  201. \end{lstlisting}
  202. \vfill
  203. Astuce: l'index maximal est \texttt{len(list) -1} ;)
  204. \end{frame}
  205. \begin{frame}[fragile]
  206. \frametitle{Modifer une liste}
  207. \begin{lstlisting}
  208. >>> liste = [1, 2, 3]
  209. >>> liste[1] = 4
  210. >>> liste
  211. [1, 4, 3]
  212. \end{lstlisting}
  213. \end{frame}
  214. \begin{frame}[fragile]
  215. \frametitle{Itérer sur les éléments d'une liste}
  216. \begin{lstlisting}
  217. names = ["Alice", "Bob", "Charlie"]
  218. for name in names:
  219. print("Bonjour", name)
  220. Bonjour Alice
  221. Bonjour Bob
  222. Bonjour Charlie
  223. \end{lstlisting}
  224. \end{frame}
  225. \begin{frame}[fragile]
  226. \frametitle{Test de présence}
  227. Avec le mot-clé \texttt{in}:
  228. \vfill
  229. \begin{lstlisting}
  230. >>> fruits = ["pomme", "banane"]
  231. >>> "pomme" in fruits
  232. True
  233. >>> "orange" in fruits
  234. False
  235. \end{lstlisting}
  236. \end{frame}
  237. \begin{frame}[fragile]
  238. \frametitle{Ajout d'un élément}
  239. Avec \texttt{append()}
  240. \begin{lstlisting}
  241. >>> fruits.append("poire")
  242. >>> fruits
  243. ['pomme', 'banane', 'poire']
  244. \end{lstlisting}
  245. \vfill
  246. Notez le point entre `fruits` et `append`
  247. \end{frame}
  248. \begin{frame}[fragile]
  249. \frametitle{Autres opérations}
  250. \begin{lstlisting}
  251. >>> fruits = ["pomme", "poire"]
  252. >>> fruits.insert(1, "abricot")
  253. # ['pomme', 'abricot', 'poire']
  254. >>> fruits.remove("pomme")
  255. # ['abricot', 'poire']
  256. >>> fruits.remove("pas un fruit")
  257. Erreur!
  258. \end{lstlisting}
  259. \end{frame}
  260. \begin{frame}[fragile]
  261. \frametitle{Dictionnaires}
  262. Des clés et des valeurs:
  263. \begin{lstlisting}
  264. >>> mon_dico = dict() # dictionaire vide
  265. >>> mon_dico = {} # aussi un dictionnaire vide
  266. # deux cles et deux valeurs:
  267. >>> scores = {"john": 24, "jane": 23}
  268. >>> scores.keys()
  269. ["john", "jane"
  270. >>> mon_dico.values()
  271. [24, 23]
  272. \end{lstlisting}
  273. \end{frame}
  274. \begin{frame}[fragile]
  275. \frametitle{Insertion}
  276. \begin{lstlisting}
  277. >>> scores = {"john": 10 }
  278. >>> scores["john"] = 12 # John marque deux points
  279. >>> scores["bob"] = 3 # Bob entre dans la partie
  280. >>> scores["personne"]
  281. Erreur!
  282. \end{lstlisting}
  283. \end{frame}
  284. \begin{frame}[fragile]
  285. \frametitle{Fusion de dictionnaires}
  286. \begin{lstlisting}
  287. >>> s1 = {"john": 12, "bob": 2}
  288. >>> s2 = {"bob": 3, "charlie": 4}
  289. >>> s1.update(s2)
  290. >>> s1
  291. {"john": 12, "bob": 3, "charlie": 4}
  292. \end{lstlisting}
  293. \end{frame}
  294. \begin{frame}[fragile]
  295. \frametitle{Destruction}
  296. \begin{lstlisting}
  297. >>> scores = {"john": 12, "bob": 23}
  298. >>> scores.pop("john")
  299. # {"bob': 23}
  300. \end{lstlisting}
  301. \end{frame}
  302. \begin{frame}[fragile]
  303. \frametitle{Ensembles}
  304. Des objets sans ordre ni doublons.
  305. Création avec la fonction \texttt{set()}:
  306. \begin{lstlisting}
  307. >>> sac = set()
  308. >>> sac = {} # oups, c'est un dictionnaire vide!
  309. >>> sac = {"one", "two"} # un set avec deux strings
  310. \end{lstlisting}
  311. \end{frame}
  312. \begin{frame}[fragile]
  313. \frametitle{Ajout d'un élement dans un ensoble}
  314. \begin{lstlisting}
  315. >>> sac = {"one", "two"}
  316. >>> sac.add("three"}
  317. # {"one", "two", "three"}
  318. >>> sac.add("one")
  319. # {"one", "two", "three"} # pas de changement
  320. \end{lstlisting}
  321. \end{frame}
  322. \begin{frame}[fragile]
  323. \frametitle{Autres opérations}
  324. \begin{lstlisting}
  325. >>> s1 = {"one", "two"}
  326. >>> s2 = {"one", "three"}
  327. # {"two"}
  328. \end{lstlisting}
  329. Aussi:
  330. \begin{itemize}
  331. \item \texttt{update()}
  332. \item \texttt{union()}
  333. \item \texttt{intersection()}
  334. \end{itemize}
  335. \end{frame}
  336. \begin{frame}[fragile]
  337. \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
  338. Jeu du pendu
  339. \end{beamercolorbox}
  340. \end{frame}
  341. \end{document}