Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.

Python-S03-E04.md 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. % Python Épisode 4
  2. % Dimitri Merejkowsky
  3. % E2L
  4. \huge \center Rappels
  5. # print
  6. Afficher quelque chose dans la console:
  7. \vfill
  8. ```python
  9. print("Bonjour, monde")
  10. ```
  11. \vfill
  12. *Affiche: Bonjour, monde*
  13. # Commentaires
  14. Les lignes qui commencent par un `#` sont ignorées:
  15. \vfill
  16. ```python
  17. # Ceci est un commentaire
  18. print("Bonjour, monde")
  19. ```
  20. \vfill
  21. *Affiche: Bonjour, monde*
  22. # Maths (1)
  23. * `+`, `*`, `-`, `/`
  24. \vfill
  25. ```python
  26. print(1 + 2)
  27. ```
  28. \vfill
  29. *Affiche: 3*
  30. # Maths (2)
  31. Opérations: `//`, `%`
  32. ```python
  33. print(14 // 3)
  34. print(14 % 3)
  35. ```
  36. \vfill
  37. *Affiche 4, puis 2*
  38. # Instructions et expressions
  39. * Les instructions sont *exécutées*
  40. * Les expressions sont *évaluées*
  41. # Exemple
  42. ```python
  43. print(1+2)
  44. ```
  45. \vfill
  46. * Évaluation de l'expression `1+2` (ça fait `3`)
  47. * Exécution de l'instruction `print`
  48. * Affiche `3`
  49. # Variables
  50. ```python
  51. a = 2
  52. b = 3
  53. c = a + b
  54. print(c)
  55. ```
  56. \vfill
  57. * Assigne 2 à la variable `a`
  58. * Assigne 3 à la variable `b`
  59. * Assigne le résultat de l'évaluation de `a+b` à c (5)
  60. * Affiche 5
  61. # Chaînes de caractères (strings)
  62. Avec des `"` ou des `'`
  63. \vfill
  64. ```python
  65. print("Il a dit: 'bonjour' ce matin.")
  66. ```
  67. *Affiche: Il a dit: 'bonjour' ce matin*
  68. \vfill
  69. ```python
  70. print('Il a dit: "bonjour" ce matin')
  71. ```
  72. *Affiche: Il a dit: "bonjour" ce matin*
  73. # Concaténation
  74. Avec `+`
  75. \vfill
  76. ```python
  77. prénom = "Marie"
  78. message = "Bonjour " + prénom
  79. print(message)
  80. ```
  81. \vfill
  82. *Affiche: Bonjour Marie*
  83. # Conversions (1)
  84. Entier vers string avec `str()`:
  85. \vfill
  86. ```python
  87. x = 40
  88. y = 2
  89. message = "La réponse est: " + str(x + y)
  90. print(message)
  91. ```
  92. \vfill
  93. *Affiche: La réponse est 42*
  94. # Conversions (2)
  95. String vers entier avec `int()`:
  96. \vfill
  97. ```python
  98. quarante_en_chiffres = "40"
  99. réponse = int(quarante_en_chiffres) + 2
  100. ```
  101. \vfill
  102. *Assigne 42 à la variable `réponse`*.
  103. # Opérations booléennes (1)
  104. Renvoient `True` ou `False` après évaluation:
  105. \vfill
  106. +-------+-----------------------------+
  107. |``==`` | égal |
  108. +-------+-----------------------------+
  109. |``!=`` | différent |
  110. +-------+-----------------------------+
  111. |``>`` | strictement supérieur |
  112. +-------+-----------------------------+
  113. |``>=`` | supérieur ou égal |
  114. +-------+-----------------------------+
  115. |``<`` | strictement inférieur |
  116. +-------+-----------------------------+
  117. |``<=`` | inférieur ou égal |
  118. +-------+-----------------------------+
  119. # Exemples
  120. ```python
  121. âge = 14
  122. peut_conduire = (âge >= 18)
  123. ```
  124. \vfill
  125. *Assigne* la valeur `False` *à la variable* `peut_conduire`.
  126. # Opérations (2)
  127. +-------+-----------+
  128. |``not``| négation |
  129. +-------+-----------+
  130. |``and``| et |
  131. +-------+-----------+
  132. |``or`` | ou |
  133. +-------+-----------+
  134. ```python
  135. il_pleut = True
  136. j_ai_un_parapluie = False
  137. je_suis_mouillé = il_pleut and not j_ai_un_parapluie
  138. ```
  139. \vfill
  140. *Assigne la valeur* `True` *à la variable* `je_suis_mouillé`.
  141. #
  142. \center \huge Contrôle de flux
  143. # Contrôle de flux
  144. * Modifier l'ordre d'exécution des instructions.
  145. * Utiliser des blocs:
  146. * `:`, retour à la ligne, indentation
  147. # if / else
  148. ```python
  149. a = 3
  150. b = 4
  151. if a == b:
  152. print("a et b sont égaux")
  153. else:
  154. print("a et b sont différents")
  155. ```
  156. \vfill
  157. * Assigne `3` à la variable `a`
  158. * Assigne `4` à la variable `b`
  159. * Évalue l'expression `a == b`. (`False`)
  160. * Saute l'exécution du bloc après le `if`
  161. * Exécute le bloc après le `else`
  162. * Affiche *a et b sont différents*
  163. # while
  164. Évalue le bloc tant que l'expression après le `while` renvoie
  165. `True`:
  166. ```python
  167. i = 0
  168. while i < 3:
  169. print(i)
  170. i = i + 1
  171. ```
  172. \vfill
  173. *Affiche 0, puis 1, puis 2*
  174. # while, if, et break
  175. Interrompre une boucle avec `break`:
  176. ```python
  177. i = 0
  178. while True:
  179. i = i + 1
  180. print(i)
  181. if i > 3:
  182. break
  183. ```
  184. \vfill
  185. *Affiche 1, puis 2, puis 3, puis 4*