1. Introduction#
Kristaps Stolarovs and Michael Hrycek-Robinson
kristaps.stolarovs@manchester.ac.uk
Department of Mechanical and Aerospace Engineering, The University of Manchester, Manchester, UK
1.1. Intended Learning Outcomes#
After reading this page, you should be able to:
Install our run Python on your machine
Pick an IDE
Write your first Python script
1.2. What is Python?#
Python is a powerful programming language that excels at handling mathematical and scientific computations. It emphasises readability and ease-of-use, and is generally much simpler to work with than lower-level languages such as C++ or Rust. What mainly interests us however, is the incredible amount of libraries which make it particularly suitable for numerical methods, data science, simulations, statistics and engineering.
Python is the language of choice for scientific computing. It is begginer friendly, has powerful mathematical libraries such as NumPy, SciPy and Matplotlib. These libraries provide us with a collection of precompiled codes that can be used later on in a program for some specific well-defined operations. Additionally, unlike MATLAB, Python is open source. Its use is completely free, and any part of its code can be viewed and modified.
1.3. Getting started#
To start working with Python, we need to set up our development environment.
To begin, we will need:
The Python interpreter itself.
A collection of libraries used for scientific computing.
Some form of editor. This can range from a simple text editor, all the way to a specialised IDE (integrated development environment).
1.3.1. The Python interpreter#
If you Google “download python”, you will most likely end up on the python.org website. Here, you can download the base interpreter. Unlike MATLAB however, Python isn’t a “turn-key” solution. You would be downloading just the Python interpreter, with no editor, and only the base libraries.
You can, however, install a Python distribution, which pre-packages a lot of useful libraries and tools. The most common example of this is Anaconda, which we recommend using for this course.
1.3.2. Libraries used#
In this course, we will stick to the base Python libraries, along with four very popular and mature libraries:
NumPy: This library adds support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions.SciPy: This library adds support for optimisation, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and engineering.Matplotlib: This library adds support for creating plots in Python.JupyterLab: This library implements Jupyter notebooks, an interactive environment for creating notebook documents combining both text and code. This book is written using Jupyter notebooks.
If you are using anaconda, these libraries will be pre-installed in the base anaconda environment.
1.3.3. Setting Up Your Environment#
To start working with Python for numerical methods, you’ll need three components:
1.3.3.1. Python Distribution:#
Instead of downloading Python directly from python.org, we recommend installing Anaconda. Anaconda is like a pre-packed mathematical workspace - it includes Python plus all the scientific libraries we’ll need. Download Anaconda from: https://www.anaconda.com/download/success
1.3.3.2. Development Environment:#
We’ll primarily use Jupyter notebooks, which allow us to:
Write and execute code in small, testable chunks
Include mathematical notation and explanations alongside our code
Create interactive visualizations of our numerical results
1.3.3.3. Additional Tools (Optional):#
While Jupyter notebooks will be our main workspace, you might also want to try:
VS Code: A versatile editor with excellent Python support (along with most other languages)
Spyder: An IDE designed specifically for scientific Python
PyCharm Community Edition: A powerful Python IDE with advanced features
1.4. Installing Python#
First, you should pick the combination of distribution and editor you want to use. If you are a complete beginner, we recommend installing the Anaconda Distribution. Follow the install instructions for your specific system. If you are using a university cluster, these already have Python, Anaconda and the necessary scientific libraries installed. The Anaconda distribution includes Anaconda Navigator, a graphical interface that makes it easy to launch applications and manage packages. Here’s how to get started:
1.4.1. Installation Steps#
1.4.1.1. Download Anaconda:#
Go to the Anaconda download page
Choose the version for your operating system (Windows, macOS, or Linux)
Download the latest Python version
1.4.1.2. Install Anaconda:#
Windows:
Double-click the downloaded .exe file
Select “Install for me only” (recommended for beginners)
Keep the default installation location
Check “Add Anaconda to my PATH environment variable”
macOS:
Double-click the downloaded .pkg file
Follow the installation prompts
Keep all default settings
Linux:
Open terminal in the download directory
Run:
bash Anaconda3-xxxx.xx-Linux-x86_64.shFollow the terminal prompts
1.4.1.3. Verify Your Installation:#
Open Anaconda Navigator:
Windows: Search for “Anaconda Navigator” in the Start menu
macOS: Open “Anaconda Navigator” from Applications or Launchpad
Linux: Type
anaconda-navigatorin the terminal
You should see a dashboard with various applications like JupyterLab, Spyder, etc., like so:
1.5. Introduction to JupyterLab#
1.5.1. Starting your first Notebook#
For this course, we’ll primarily use Jupyter Notebooks. To start:
Open Anaconda Navigator
Click “Launch” under JupyterLab Your default web browser will open with JupyterLab, presenting a similar window:
Here, you can immediately click “Python [conda env:base]”, which will open a new Jupyter notebook in your current directory. You can use the File Browser tab to navigate to a directory of your choice, like so:
In your new notebook, you can use the toolbar to save your notebook, create, cut, copy, paste, run “cells”, which are self-contained code or markdown (text) blocks that you can run individually.
Any variables, functions, classes, etc. you declare in these cells can be used by any other cell. At any time, you can also restart the “Kernel”, which is the Python interpreter running behind the scenes. This clears all variables and functions from memory, giving you a fresh start.
1.5.2. Working with Cells#
There are two main types of cells in Jupyter notebooks:
Code Cells: Where you write and execute Python code.
Markdown Cells: Where you write text, mathematical equations, and documentation.
1.5.3. Cell Execution Order#
Understanding cell execution order is crucial:
Cells can be manually executed in any order
The number in brackets [1] to the left of a cell shows its execution order.
Variables and functions defined in one cell are available to all cells below.
If you manually run a cell (say cell [2]), and then run a different cell above it (cell [1]), the variables and functions defined in the lower cell [2] will be available to cell [1]. Keep this in mind to avoid confusion in the future.
Re-running a cell that defines a variable will update that variable’s value.
1.5.4. Adding Mathematical Equations#
For numerical methods, we often need to write mathematical equations. Jupyter supports LaTeX syntax:
Use
$...$for inline equationsExample: \(a^2 + b^2 = c^2\)
Use
$$...$$for displayed equationsExample: $\( a^2 + b^2 = c^2 \)$
1.5.5. Reading this book#
As this book is written using Jupyter Notebooks, you can download any chapter locally. You can use this to complete the exercises or try out some of the examples yourself. The download button in the top-right of the page gives the option to download either the Jupyter Notebook itself (.ipynb), or a PDF version of the chapter (.pdf).

1.6. Your first Python program#
After creating your first notebook, create a new code cell using the “+” symbol found in the toolbar.
Add this code to the cell:
print("Hello World!")
Hello World!
Run this cell using the “⏵” run cell button. Just below the cell, you should now see the text “Hello World!”, same as in the image below:
Congratulations, you have written a Python program!