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.
 
 
 
 
 
 

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