Python for Engineers#
Lorenz Kies digitale-werkzeuge@imes.uni-hannover.de
Links:
Introduction#
This course is meant as an introduction to Python and programming in general for engineers. We live in an age where practically unimaginable computing resources are almost universally available. Being able to make use of these resources has become a requirement to work productively in many science and engineering related fields. In addition to that, programming might also help you in other avenues of your life. It is therefore important, that you as engineers have a basic understanding of programming and some commonly available tools.
Python as a language and ecosystem comes with a lot of libraries, tools and other resources for almost everything you might want to do. In addition to that it is a fairly beginner-friendly language, which make this is a great candidate for this course.
In this course you will learn:
The fundamentals of programming in Python (variables and expressions, control structures, functions, common data structures)
The basics Python’s rich scientific tool set (NumPy, SciPy, Matplotlib, Pandas, Python Control)
How to find and read documentation
How to use the tools required for developing and running Python code
And how to use those skills to solve everyday engineering problems
Additionally, there will be a lot of pointers for further reading and learning to lead you to more advanced topics.
Course Structure#
The course is structured into:
12 live coding lectures & tutorials
4 exercise sheets and 1 mini project (solutions presented in tutorials)
2 graded programming tests (probably on 04.06.2026 and 16.07.2026 14:30)
And multiple opportunities to work with real world experiments
The course as a whole is on pass/fail basis you will not get a number grade. To pass you need to achieve at least 50% of the respective points across the two graded tests and a minimum number of points in each test. The tests will be 90 minutes each to give you plenty of time to work on the problems.
The lectures are held as live coding sessions in Jupyter notebooks where you the students should type along. Questions and other forms of interaction are welcome. Don’t hesitate to ask curious questions beyond the immediate scope of the lecture, sidetracks are welcome and should make for a more lively learning experience.
The exercise sheets are also (mostly) Jupyter notebooks and are meant to be solved before the respective tutorial. It is recommended, that you work on these in pairs. You can use tools like VS Code Live Share to work together remotely if needed. The mini project should give more insight into Python outside of Jupyter notebooks and should also be done in pairs. You will not need to submit the exercises, but you should still consider them as mandatory. Unlike some other courses, doing nothing until a few days before the test is absolutely not a feasible strategy. Learning to program requires consistent practice.
There will be additional bonus exercises in the form of “Hardware Challenges”, if you submit working code for these problems, we will invite you to the lab where you can run your code on the actual experiment.
Schedule (Preliminary)#
Date |
Lecture |
Tutorial |
|---|---|---|
14.04.2026 |
Getting Started |
- |
21.04.2026 |
Moving On |
Exercise 0.1 to 0.3 |
28.04.2026 |
Built-in Data Structures |
Exercise 0.4 to 0.7 |
05.05.2026 |
NumPy & Matplotlib |
Exercise 1.1.1 to 1.1.9 |
12.05.2026 |
Strings, Files & File Systems |
Exercise 1.2.1 to 1.4.1 |
19.05.2026 |
Object-Oriented Programming |
Exercise 2.1.1 to 2.4 |
26.05.2026 |
- |
- |
02.06.2026 |
Bits and Pieces |
Test Preparation |
09.06.2026 |
Pandas |
Exercise 2.5 to 2.6.2 |
16.06.2026 |
Advanced Plotting |
Exercise 2.7.1 & 2.7.2 & 2.8.1 & 3.1 |
23.06.2026 |
Differential Equations & Systems |
Exercise 3.2.1 & 3.2.2 |
30.06.2026 |
Root Finding & Optimization |
Exercise 3.3.1 to 3.3.5 |
07.07.2026 |
Python Projects |
Exercise 3.4.1 to 3.4.4 |
14.07.2026 |
Buffer |
Exercise 4.1 to 4.7 |
Why Is All of This in English?#
All of the course material is in English, this is not to inconvenience you, but it should ideally be to help you. Pretty much all programming languages as well as related tools and most online resources are in English. This makes programming as a whole mostly an exclusively English thing. To help you get used to programming in English i.e. English variable names, interfaces and documentation the course material is in English. To not pose too much of an additional challenge to people who are not as comfortable in English, the lectures will be held in German.
AI Tools - A Word of Caution#
Large language model based AI tools have seen a rapid increase in capabilities and popularity in recent years. This is very much a double-edged sword in the context of learning to program. On the one hand they allow you to do more things faster with less knowledge, but on the other hand they allow you to do more things faster with less knowledge. In a few more words:
AI tools are now capable of solving many small common problems decently, so they can be an attractive option if you are not interested in learning and just need quick results. They are especially good at solving most of the classical introduction to programming exercises. This might easily you lead to believe, that there is no use in learning “how to generate a list of Fibonacci numbers” since you could always just have an AI do it, just like you don’t do arithmetic with pen and paper since you have a calculator. But solving these problems is not about having generating a solution to these problems, but about the skills you learn along the way. Skills that you will need to solve more complex problems even with the help of AI tools.
Here is an analogy to illustrate this idea: In your very first swimming lessons, you will likely be in a pool where you can easily stand up (i.e. use AI tools). So you might ask why you would need to learn to swim when you can just stand up. But you are not learning to swim so you can keep you head above water in the beginners pool (i.e. solve practice problems) you are learning to swim so you won’t drown when you can’t stand (i.e. solve real world problems).
Not only are these tools very good at solving practice problems, they can be even worse: depending on how tightly they are integrated into your workflow, they might not only deprive you of the valuable learning experience, but fool you into believing you learned something that you didn’t. So while AI tools, if used right, can be a helpful tool in learning to program, they can quickly become a crutch that hinders learning. That is why, for learning to program, I recommend that you do not use any AI tools.
Setting up your Environment#
To be able to follow the live lectures and solve the exercise sheets you will need to set up your development environment. It is recommended that you install Python using uv and use Visual Studio Code as your code editor. The installation of those tools is described in the following sections.
Shells & Package Managers#
For the installation of programming environments, but also for programming itself, it is very useful to have at least some understanding of shells / command line interfaces and package managers. Shells are text-based interfaces to the operating system, which allow you to interact with the system by typing commands into a terminal and getting text-based output back.
While there are also other options, the shells you will typically be using are:
PowerShell on Windows
Bash on Linux (the application is usually called “Terminal”)
Zsh on MacOS (the application is also called “Terminal”)
You can launch them using the search function of your operating system.
In these terminals you can then use package mangers to install applications and other software. Package managers are kind of like the app stores on your phones only that they are text-based and much more comprehensive, but they mostly fulfill mostly the same purpose.
Again, there are many alternatives but the common ones are:
WinGet for Windows
APT for Linux
Homebrew for MacOS (brew for short)
Package managers are not limited to installation of applications for operating systems, they are also used for programming languages. But unlike package managers for operating systems, these usually don’t install applications, they mostly install libraries which are basically common pieces of code that you wouldn’t want to rewrite yourself every time. For Python there are PIP, Conda and a few more.
In the following you should install the required applications using the package managers mentioned above.
Setting up Python with uv#
There are many ways to install Python on your system. As mentioned above, we will be using uv which includes an alternative implementation of PIP in addition to some more advanced features for project management.
Windows#
Install uv using WinGet in PowerShell:
winget install astral-sh.uv
You may have to agree to the terms of service the first time you use WinGet.
Linux & MacOS#
Download and execute the installation script in the terminal:
curl -LsSf https://astral.sh/uv/install.sh | sh
or using wget if you don’t have curl:
wget -qO- https://astral.sh/uv/install.sh | sh
Create the Python Environment#
First, test if your installation of uv was successful by entering the following command in your terminal:
uv
You should see an output similar to this:
An extremely fast Python package manager.
Usage: uv [OPTIONS] <COMMAND>
...
Unlike many other methods of installing Python, uv does not install Python globally on your system. Instead, uv is used to create isolated Python installations called virtual environments for each project. For these purposes, we will consider this course to be the project. Start by creating a directory where you want to store the material for this course. Then open your shell in said directory. You can usually do this by right-clicking in your file explorer while holding the shift key and selecting “Open PowerShell window here” or “Open in Terminal” or something similar.
Start by verifying that you are in the correct directory with:
pwd
This should display the path to the directory you just created.
If the location is correct, initialize the project with uv:
uv init . --bare --name python-for-engineers
Then add the required packages to the project:
uv add numpy scipy matplotlib pandas control ipykernel ipywidgets ipympl pyarrow otter-grader pytest ruff vscdark
This will add them to your project specification in stored in a file called pyproject.toml and install them in a virtual environment for this project.
Executing Python Code#
In the first few lectures, we will only be using Jupyter notebooks, so you can skip this section for now.
There are two primary ways to execute Python code in this environment.
The first option is to execute Python through uv:
uv run python <your_script.py>
Alternatively, you can activate the virtual environment created by uv and then execute Python directly:
Using Windows PowerShell:
.\venv\Scripts\Activate.ps1
You may first need to allow the execution of scripts by opening a new PowerShell window as administrator and running:
Set-ExecutionPolicy RemoteSigned
On Linux and MacOS you can activate the environment with:
source venv/bin/activate
Setting up Visual Studio Code#
To edit your code, you will need a code editor. For this we will use Visual Studio Code. You can also use the graphical installer from the Visual Studio Code website if you are having trouble with the instructions.
Windows#
Install Visual Studio Code using WinGet in PowerShell:
winget install vscode
Linux#
Install with snap in the terminal:
sudo snap install --classic code
MacOS#
Install with brew in the terminal:
brew install --cask visual-studio-code
Extensions#
So set up VS-Code for Python development you will need to install some extensions. You can open the extensions tab by pressing Ctrl+Shift+X or by clicking the respective icon in the sidebar.
For the course you will need:
ms-python.pythonPythonms-toolsai.jupyterJupyter
Install them by clicking the blue “Install” button next to the extension.
It is also recommended to install the following extensions:
charliermarsh.ruffRuffusernamehw.errorlensError Lensnjpwerner.autodocstringautoDocstringstreetsidesoftware.code-spell-checkerCode Spell Checkeryzhang.markdown-all-in-oneMarkdown All in Onemechatroner.rainbow-csvRainbow CSV
Using Visual Studio Code#
To use all of VS Code’s features, it needs to know what files you are working on and what Python environment you are using. To do this, you need to open the folder you created for this course as a workspace in VS Code. You can do this by clicking File -> Open Folder... and selecting the folder you created for this course.
Next you need to make sure, that VS Code is using the correct Python environment. When you open a Python file, you should see a notification in the bottom right corner asking you to select a Python environment. Click on it and select the environment that is located in the venv folder in your project directory. If you don’t see the notification, you can also select the environment by clicking on the Python version displayed in the bottom left corner. Alternatively, you can also open the dialog for selecting the Python environment with the command palette (Ctrl+Shift+P) and searching for “Python: Select Interpreter”.
Similarly, when you open a Jupyter notebook, you should see a notification asking you to select a kernel. Click on it and select the kernel that is located in the venv folder in your project directory. If you don’t see the notification, you can also select the kernel by clicking on the kernel name displayed in the top right corner of the notebook editor.
Shortcuts#
Code editors like VS-Code come with a lot of shortcuts to make editing code faster and less tedious. Here are some of the most useful ones, you can find a more comprehensive list here.
Key Combination |
Action |
|---|---|
|
Move cursor left/right by one word |
|
Move cursor to beginning/end of line |
|
Comment / uncomment line or selection |
|
Duplicate line or selection up/down |
|
Move line or selection up/down |
|
Add cursor to next occurrence of selected text |
|
Rename symbol (variable, function, class, etc.) |
|
Execute current cell in Jupyter notebook |
|
Execute current cell and move to next cell in Jupyter notebook |
|
Open command palette |