Python Tutorial

This repository contains a tutorial for python beginners. The tutorial is served at https://johannesloetzsch.github.io/python-tutorial.

Lectures are written in markdown and can be found in the doc directory.

The examples directory contains the python code samples described in the lectures.

Dependencies

All dependencies and tools used in this tutorial are provided reliably using NIX.

The simples way to get all you need for this tutorial is just running:

nix develop github:johannesloetzsch/python-tutorial

Alternatively each chapter provides links how to install the required dependencies in the legacy way.

The directory doc/en contains documentation in english language.

mdBook can be used to build the handbook.

The tutorial is served at https://johannesloetzsch.github.io/python-tutorial.

Python

Python is a beginner friendly high-level script programming language. It is garbage-collected, so as a programmer you don't need to worry about the memory allocation.

Other tutorials

Online Python Interpreter

For a very simple start you can evaluate your first python code online at online-python.com.

Feel free to use such tools for your first steps to get familiar with the python language.

This repository focuses on some more advanced examples, which require additional dependencies.

Getting started

This lecture explains how to install python, pip and ipython.

If you have the Nix package manager with Flakes, all dependencies are provided by:

nix develop github:johannesloetzsch/python-tutorial#example_getting_started_ipython

For Debian this should work:

sudo apt install python3 python3-pip ipython

Installation

For a reliable and secure setup, you should avoid downloading and installing programs manually.

Instead of following the instructions from https://www.python.org/downloads/, I highly recommend using the packet manager or app store of your operating system.

A good guide on how to install python on your system can be found at https://realpython.com/installing-python.

First test

You can run your first tiny python scripts directly from the command line using the -c argument of python:

python -c 'print("Hello world")'

python -c 'print(6*7)'

Tool recommendations

For bigger projects and professional development, several additional tools are recommended.

This tutorial is going to give an introduction into some of them.

venv

If you develop and integrate python projects, you will need different isolated versions of python and python packages for different projects. This can be achived by setting up virtual environments (venv) for each project:

python -m venv .venv
source .venv/bin/activate

pip

Soon we want to install additional python packages (libraries) from the Python Package Index (PyPI).

There exist serveral different package-management systems for python. The most widespread is pip.

For this tutorial we expect pip to be installed. In most cases the following command should work as described in the documentation:

python -m ensurepip

Otherwise this guide how to install pip should be helpfull.

After successful installation this command should work:

pip --help

IPython

For development of productive code, you will later write your python scripts into files.

Python allows another quick and easy way of testing ideas: you can open an interactive interpreter and run code directly there.

## Using the python-command without any arguments opens the interactive interpreter
python
>>> print("The interactive python shell is so usefull :)")
>>> exit()

A more enhanced interactive python shell is provided by the IPython package.

You can install it with the package manager of your operating system or use pip to install it:

 pip install ipython
ipython
>>> print("The interactive IPython shell is awesome :D")
>>> exit()

The IPython Tutorial explains how to efficiently use python interactively…

Turtle

Turtle graphics is a very nice way of learning your first steps of programming :)

If you have the Nix package manager with Flakes, all dependencies are provided by:

nix develop github:johannesloetzsch/python-tutorial#example_getting_started_turtle

Otherwise use your system package manager or pip:

pip install turtle

Have fun with the official python turtle tutorial

Tools

This chapter introduces some usefull tools for managing bigger projects by increasing the maintainability and improving the code quality.

If you have the Nix package manager with Flakes, all dependencies are provided by:

nix develop github:johannesloetzsch/python-tutorial#example_tools
git clone https://github.com/johannesloetzsch/python-tutorial.git
cd python-tutorial

This chapter consists of several lectures. Here just some of the links with a minimal example:

Testing

pytest examples/tools

Linter

pycodestyle -v examples/tools/**/*.py

flake8 -v examples/tools/**/*.py

pylint examples/tools/**/*.py

Further reading

JupyterLab (Notebook)

  • https://jupyter.org/
  • https://jupyter.org/install

Spyder IDE

  • https://www.spyder-ide.org/

Packaging

  • https://packaging.python.org/en/latest/tutorials/packaging-projects/

Testing

  • https://realpython.com/python-testing/

  • https://docs.pytest.org/en/stable/

  • https://docs.python.org/3/library/unittest.html

  • https://docs.pytest.org/en/stable/explanation/goodpractices.html#test-discovery

Linter

  • https://pypi.org/project/pylint/
  • https://flake8.pycqa.org/en/latest/
  • https://pycodestyle.pycqa.org/

Typing

  • https://mypy.readthedocs.io/en/stable/index.html
  • https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html
  • https://docs.python.org/3/library/typing.html
  • https://typing.python.org/en/latest/

Debugger

  • https://docs.python.org/3/library/pdb.html
  • https://pypi.org/project/ipdb/

CLI

  • https://packaging.python.org/en/latest/guides/creating-command-line-tools/

  • https://typer.tiangolo.com/

  • https://click.palletsprojects.com/en/stable/

  • https://docopt.readthedocs.io/en/latest/

  • https://docs.python.org/3/howto/argparse.html

    • https://www.geeksforgeeks.org/command-line-interface-programming-python/

GUI

Pygame

https://www.pygame.org/docs/

Code + Artwork

git clone git@github.com:plasticuproject/UltimatePygameIntro.git
cd UltimatePygameIntro
ipython runner_video.py

Matplotlib

  • https://matplotlib.org/

PyQt

  • https://en.wikipedia.org/wiki/PyQt
  • https://doc.qt.io/qtforpython-6/

API

REST

  • https://johannesloetzsch.github.io/LF7/rest.html

Machine Learning / AI

  • https://heise-academy.de/classrooms/mlpython124

  • https://keras.io/

  • https://pytorch.org/

  • https://github.com/Orbiter/llm-rag-cheat-sheet

    • https://media.ccc.de/v/clt25-269-open-datafreie-daten-in-ki-chatbots-nutzen

Data analysis / scientific computing

  • https://numpy.org/
  • https://pandas.pydata.org/
  • https://scipy.org/

Computer vision

  • https://opencv.org/

Embedded

  • https://pythonprogramming.net/gpio-raspberry-pi-tutorials/

  • https://www.raspberrypi.org/courses/learn-python

  • https://docs.micropython.org/