Skip to content

cod3licious/python_tutorial

Repository files navigation

Python Tutorial

This repository contains Jupyter Notebooks to teach you the basics in Python that you need for my machine learning course (→ book and exercises). Additionally, you might want to refresh your memory on the linear algebra basics, which will also come in very handy.

Using Python

This tutorial requires a recent Python installation (version 3.8 or later) together with Jupyter Notebook and the libraries listed in requirements.txt. You can either install these locally on your own machine or work in a preconfigured cloud environment.
If you're using a company computer, please consult with your IT department.

Working on your own computer

  • Download the contents of this repository (by clicking the green "Code" button at the top and then "Download ZIP").
  • Install Python either directly from the official website or using pyenv in case you need to manage multiple Python version in parallel on your machine. A recent version (Python 3.10 or later) is recommended.
  • Install the required dependencies (i.e., additional libraries like numpy):
    • Option 1: Global installation using the Python package manager pip, e.g., by calling pip install -r requirements.txt from a terminal in the repository folder to install all dependencies listed in the requirements.txt document.
    • Option 2: Installation in an isolated virtual environment using poetry (recommended): Install poetry itself with pip install poetry and then call poetry install to install the dependencies from the pyproject.toml or poetry.lock file and activate the virtual environment with poetry shell.
  • In either case should now have Jupyter Notebook installed, which you can open from the terminal with jupyter notebook, which should then open in the browser. Navigate to the repository folder and click on the first notebook to get started.

Working in the cloud

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 powered by MyBinder by clicking here: Open in Binder (right-click to open in a new tab; might take a while to launch).
Alternatively, you can also open the notebooks in Google Colab, which is faster, but requires a Google account: Open In Colab

Getting started

When starting Jupyter Notebook, you should see something similar to this (on your local computer you might need to navigate to the correct folder first):

screenshot_notebook1

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:

screenshot_notebook2

The tutorial is interactive, i.e., you are supposed to execute the code yourself and experiment with the given examples to better understand what is going on. To execute a so-called "cell" with code in it, make sure the cell is selected (has a colored border around it), and click the "run" button (or press "shift"+"enter" on your keyboard). After the cell was successfully executed, the In [ ]: next to it will change to something like In [5]:.

screenshot_notebook3

Execute all the cells from top to bottom!

After you're done with the first notebook, look at 2_exercises.ipynb, which contains some exercises that you should complete. The solutions to these exercises can be found in 3_exercise_solutions.ipynb (but try to solve them on your own first!!).

When you're comfortable with the basics, have a look at 4_numpy_pandas.ipynb, which gives a quick introduction to the basic data science Python libraries numpy, pandas, and matplotlib. These will be especially important to solve the exercises, so even if you already feel comfortable with Python, please work through this part of the tutorial anyways!

If you have any questions, please send me an email.

Have fun!