Skip to content

Commit c14577e

Browse files
committed
lambda function in tutorial and updated links
1 parent e5a857b commit c14577e

4 files changed

Lines changed: 29 additions & 85 deletions

File tree

1_python_basics.ipynb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"# Python Basics\n",
88
"\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",
1010
"\n",
1111
"Please make sure you understand everything mentioned here and are able to solve the exercises in the next notebook!\n",
1212
"\n",
@@ -19,13 +19,13 @@
1919
"\n",
2020
"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",
2121
"\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",
2323
"\n",
2424
"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",
2525
"\n",
2626
"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",
2727
"\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."
2929
]
3030
},
3131
{
@@ -443,7 +443,7 @@
443443
"# strings also have a bunch of methods that can be called on them to change the string\n",
444444
"# for a complete list check the documentation: \n",
445445
"# 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",
447447
"# everything in lower case\n",
448448
"print(a_str.lower())\n",
449449
"# calling such a method on a string returns a new string, the original variable remains unchanged\n",
@@ -1693,6 +1693,28 @@
16931693
"print(\"return value for 11:\", is_greater_than_10(11))"
16941694
]
16951695
},
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+
},
16961718
{
16971719
"cell_type": "markdown",
16981720
"metadata": {},

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Python Tutorial
22

3-
This repository contains Jupyter Notebooks to teach you the basics in Python that you need for machine learning (→ [book](https://franziskahorn.de/mlbook/ml4industry_guide.html) and [exercises](https://github.com/cod3licious/ml_exercises)). Additionally, you might want to have a look at the [math_basics.pdf](https://github.com/cod3licious/python_tutorial/blob/master/math_basics.pdf) document to refresh your memory on the linear algebra basics, which will also come in very handy.
3+
This repository contains Jupyter Notebooks to teach you the basics in Python that you need for machine learning (→ [book](https://franziskahorn.de/mlbook/) and [exercises](https://github.com/cod3licious/ml_exercises)). Additionally, you might want to have a look at the [math_basics.pdf](https://github.com/cod3licious/python_tutorial/blob/master/math_basics.pdf) document to refresh your memory on the linear algebra basics, which will also come in very handy.
44

55
**Working on your own computer:** <br>
66
If you already have Python and Jupyter Notebook installed locally, you can download the repository (click the green "Code" button at the top of the [repository main page](https://github.com/cod3licious/python_tutorial) and then "Download ZIP"), open Jupyter Notebook on your computer, navigate to the repository folder, and click on one of the notebooks to open it and work with it. <br>
7-
To install Python for personal use, [anaconda](https://docs.anaconda.com/anaconda/install/) is a good option, as it already comes with most of the needed libraries. But you can also install [Python](https://www.python.org/downloads/) and [Jupyter Notebook](https://jupyter.org/install) directly and then install the needed libraries with the Python package manager [pip](https://pip.pypa.io/en/stable/installing/). If you're using a company computer, please consult with your IT department. In any case, make sure you're using Python 3 and all libraries listed in the `requirements.txt` file are installed and up to date (you can also verify this with the `test_installation.ipynb` notebook).
7+
To install Python for personal use, [anaconda](https://docs.anaconda.com/anaconda/install/) is a good option, as it already comes with most of the needed libraries. If this is not an option due to the licensing restrictions, [mini-forge](https://github.com/conda-forge/miniforge), basically a smaller community version of anaconda, might be a better option for you. But you can also install [Python](https://www.python.org/downloads/) and [Jupyter Notebook](https://jupyter.org/install) directly and then install the needed libraries with the Python package manager [pip](https://pip.pypa.io/en/stable/installing/). If you're using a company computer, please consult with your IT department. In any case, make sure you're using Python 3 and all libraries listed in the `requirements.txt` file are installed and up to date.
88

99
**Working in the cloud:** <br>
1010
If you can't or don't want to install Python on your own computer, you can also work in an online version of Jupyter Notebook by following [this link](https://mybinder.org/v2/gh/cod3licious/python_tutorial/master) (right-click to open in a new tab; might take a while to launch). <br>
@@ -13,7 +13,7 @@ Alternatively, you can also open the notebooks in **Google Colab** (recommended)
1313

1414

1515

16-
In any case you should see something similar to this:
16+
In any case you should see something similar to this (on your local computer you might need to navigate to the right folder first):
1717
<img src="doc/screenshot1.png" alt="screenshot_notebook1" width="720"/>
1818

1919
Start with `1_python_basics.ipynb`, which contains the introductory Python tutorial. Just click on it and a new tab with the notebook should open:

requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
numpy
22
matplotlib
33
pandas
4-
scipy
5-
scikit-learn
6-
plotly

test_installation.ipynb

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)