|
167 | 167 | "\n", |
168 | 168 | "[Back to ToC](#toc)\n", |
169 | 169 | "\n", |
170 | | - "## Conditional statements : `if` , `elif`, `else` <a id='2'></a>\n", |
| 170 | + "## Conditional statements: `if` , `elif`, `else` <a id='2'></a>\n", |
171 | 171 | "------------------------------------------------------\n", |
172 | 172 | "\n", |
173 | 173 | "There are several ways to control the flow of your code using logical statements.\n", |
174 | 174 | "* The **`if`** keyword followed by an expression defines a block that will be executed only if the \n", |
175 | | - " given statement is `True`.\n", |
| 175 | + " given expression evaluates to `True`.\n", |
176 | 176 | "* The **`else`** keyword defines a block to be executed if the previous `if` or `elif` expressions\n", |
177 | 177 | " evaluated to `False`.\n", |
178 | 178 | "* Tests for additional conditions can be added using the **`elif`** keyword (contraction of `else if`).\n" |
|
187 | 187 | "" |
188 | 188 | ] |
189 | 189 | }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "* Single **`if`**:" |
| 195 | + ] |
| 196 | + }, |
190 | 197 | { |
191 | 198 | "cell_type": "code", |
192 | 199 | "execution_count": null, |
|
202 | 209 | "print(\"This is a\", toppings, \"sandwich\")" |
203 | 210 | ] |
204 | 211 | }, |
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "* **`if... else...`** statement: " |
| 217 | + ] |
| 218 | + }, |
205 | 219 | { |
206 | 220 | "cell_type": "code", |
207 | 221 | "execution_count": null, |
|
256 | 270 | " `==`,`>`,`<`,`>=`,`<=`,`!=`.\n", |
257 | 271 | "\n", |
258 | 272 | "Conditions may be further combined using **logical operators** :\n", |
259 | | - "* `and` : combines 2 statements and returns `True` if both are `True`\n", |
260 | | - "* `or` : combines 2 statements and returns `True` if at least one is `True`\n", |
261 | | - "* `in` : returns `True` if the element to its left is found inside the container to its right\n", |
262 | | - "* `not` : inverts a `True` to `False` and vice-versa\n", |
263 | | - "* `is` : returns `True` if two variable reference the same object **(but we have not talked about this yet...)**\n" |
| 273 | + "* **`and`**: combines 2 statements and returns `True` if both are `True`.\n", |
| 274 | + "* **`or`** : combines 2 statements and returns `True` if at least one is `True`.\n", |
| 275 | + "* **`in`** : returns `True` if the element to its left is found inside the container to its right (**`not in`** can be used to check the reverse).\n", |
| 276 | + "* **`not`** : inverts a `True` to `False` and vice-versa.\n", |
| 277 | + "* **`is`** : returns `True` if two variable reference the same object - but we have not talked about this yet... (**`is not`** can be used to check the reverse).\n" |
264 | 278 | ] |
265 | 279 | }, |
266 | 280 | { |
|
341 | 355 | "\n", |
342 | 356 | "In the first lesson we have already used a few of python's built-in functions: `help()`,`print()`,`len()`, ... , as well as some objects methods, which are functions as well.\n", |
343 | 357 | "\n", |
344 | | - "While it is recomended to use python's built-in functions when available (they will almost always be much faster than your own code), they obviously do not cover all the possible functionalities we might need. It it is thus really useful to be able to write our own functions!\n", |
| 358 | + "While it is recomended to use python's built-in functions when available (they will almost always be much faster than your own code), they obviously do not cover all the possible functionalities we might need. This is why it is really useful to be able to write our own functions!\n", |
345 | 359 | "\n", |
346 | 360 | "In python, **functions are declared using the `def` keyword**, followed by the named of the function, brakets `()` where arguments can be specified, and finally a column `:` character.\n", |
347 | 361 | "\n", |
|
380 | 394 | "\n", |
381 | 395 | "### Function arguments <a id='5'></a>\n", |
382 | 396 | "\n", |
383 | | - "The above function has not arguments - no variable is defined between the `()` in its declaration. While not a problem *per se*, it limits the usefulness and flexibility of the function, since it will always do the exact same thing each time we call it.\n", |
| 397 | + "Our above `greetings()` function has no arguments - no variable is defined between the `()` in its declaration. While not a problem *per se*, it limits the usefulness and flexibility of the function, since it will always do the exact same thing each time we call it.\n", |
384 | 398 | "\n", |
385 | | - "Here is a variation of this function, with a `name` **argument** added. An **argument** is a value that is passed to a function and that can make its behavior change." |
| 399 | + "Here is a variation of this function, with a `name` **argument** added. An **argument** is a value that is passed to a function, can be used in the function as a variable, and that can make its behavior change." |
386 | 400 | ] |
387 | 401 | }, |
388 | 402 | { |
|
0 commit comments