|
6 | 6 | "source": [ |
7 | 7 | "# Python Basics\n", |
8 | 8 | "\n", |
9 | | - "This is an interactive Jupyter Notebook explaining the very basics of Python that you'll need for the Machine Learning Workshop.\n", |
| 9 | + "This is an interactive Jupyter Notebook explaining the very basics of Python that you'll need for the Machine Learning course.\n", |
10 | 10 | "\n", |
11 | 11 | "Please make sure you understand everything mentioned here and are able to solve the exercises in the next notebook!\n", |
12 | 12 | "\n", |
|
19 | 19 | "\n", |
20 | 20 | "You can execute the code in the cells by clicking into a cell and pressing \"shift\"+\"enter\" or clicking the \"run\" button at the top. Feel free to experiment a bit by changing the code in the examples to see what happens. Don't worry if you get any error messages - consider them hints on how to do better ;-)\n", |
21 | 21 | "\n", |
22 | | - "Instead of just running through the examples, it helps to first think about what you would expect the output of a code cell to be and then execute it to see if you were right. If you weren't, please make sure you understand why and what is happening in the code!\n", |
| 22 | + "Instead of just running through the examples, **it helps to first think about what you would expect the output of a code cell to be and then execute it to see if you were right**. If you weren't, please make sure you understand why and what is happening in the code!\n", |
23 | 23 | "\n", |
24 | 24 | "If you see a number in the square brakets next to a cell (e.g. `In [1]`), then the cell was already executed. If the brakets are empty, run the cell to execute the code in it and see the output. If you want to try out something else but not erase the given code, press the \"+\" button at the top to add a new cell below the currently selected cell. \n", |
25 | 25 | "\n", |
26 | 26 | "You have to execute all the cells starting at the beginning for everything to work as expected. If you change any variable names or their values, be prepared for different outcomes. You can execute cells multiple times and jump back and forth; the number next to the cell tells you the order of execution - if a cell below changes a variable used in the cell above it and you execute the cell above again, the output might be different from before as it uses the updated variable value. \n", |
27 | 27 | "\n", |
28 | | - "Should the code get stuck (`In [*]` means the cell is currently executing the code in it; in our examples this should never take more than a few seconds), click the \"stop\" button at the top or select \"Kernel > Interrupt\" from the menu bar. If all goes wrong, just close the tab with the notebook, go back to the [GitHub repository](https://github.com/cod3licious/python_tutorial) and open it again." |
| 28 | + "Should the code get stuck (`In [*]` means the cell is currently executing the code in it; in our examples this should never take more than a few seconds), click the \"stop\" button at the top or select \"Kernel > Interrupt\" from the menu bar." |
29 | 29 | ] |
30 | 30 | }, |
31 | 31 | { |
|
443 | 443 | "# strings also have a bunch of methods that can be called on them to change the string\n", |
444 | 444 | "# for a complete list check the documentation: \n", |
445 | 445 | "# https://docs.python.org/3/library/stdtypes.html#string-methods\n", |
446 | | - "a_str = \"This is a sentence. And here we have another sentences which is longer.\"\n", |
| 446 | + "a_str = \"This is a sentence. And here we have another sentence, which is longer.\"\n", |
447 | 447 | "# everything in lower case\n", |
448 | 448 | "print(a_str.lower())\n", |
449 | 449 | "# calling such a method on a string returns a new string, the original variable remains unchanged\n", |
|
1693 | 1693 | "print(\"return value for 11:\", is_greater_than_10(11))" |
1694 | 1694 | ] |
1695 | 1695 | }, |
| 1696 | + { |
| 1697 | + "cell_type": "markdown", |
| 1698 | + "metadata": {}, |
| 1699 | + "source": [ |
| 1700 | + "#### Lambda functions\n", |
| 1701 | + "\n", |
| 1702 | + "Lambda functions are anonymous functions, i.e., they are not defined by name and instead of the keyword `def`, they are defined with `lambda`. Usually, they are very short (one liners!) and straightforward, and therefore often used to define functions that are passed as arguments to other functions such as `sorted()` or `max()`.\n", |
| 1703 | + "\n", |
| 1704 | + "For example, in the cell above, we sorted `a_list` by passing the `square()` function to the `key` argument to sort the values in the list based on their squared values. This can be done in a more concise way by using a lambda function instead of defining a separate function (especially if this function is not used anywhere else in the code):" |
| 1705 | + ] |
| 1706 | + }, |
| 1707 | + { |
| 1708 | + "cell_type": "code", |
| 1709 | + "execution_count": null, |
| 1710 | + "metadata": {}, |
| 1711 | + "outputs": [], |
| 1712 | + "source": [ |
| 1713 | + "# x is the argument that is passed to the function and what comes after the : is the return value\n", |
| 1714 | + "# (also works with multiple arguments, i.e., could also define, e.g., lambda x, y: x + y)\n", |
| 1715 | + "sorted(a_list, key=lambda x: x**2)" |
| 1716 | + ] |
| 1717 | + }, |
1696 | 1718 | { |
1697 | 1719 | "cell_type": "markdown", |
1698 | 1720 | "metadata": {}, |
|
0 commit comments