Python for Science

Installing

Before we can start programming we need to install the software we need to program in Python. This is not interesting but it has to be done. Thankfully it's pretty easy.

Installing Python

Python homepage

Visit Python downloads and download the latest version (probably Python 3.11.0). The website should automatically detect your operating system so you have to make an effort to download the wrong thing.

Windows

Just install as normal. The installer has a checkbox for if you want to add Python to PATH (or something like that). Check that box. Adding it to the path will make it easier for other programs to find your Python installation. If you skipped checking the box you can rerun the installer or manually add the correct path to Window's PATH environment variable using Window's environment variable editor. Software not being able to find other files or other software is a common problem. It is never fun to deal with.

Verifying the installation:

  1. Open the start menu (or search menu) and type cmd
  2. Press ENTER to open a command line interface
  3. Type python --version in the command line
  4. Press ENTER
  5. Verify that it says Python 3.11.0 (or whichever Python version you installed)

macOS

I haven't used macOS that much but I found this guide: How to install Python on Mac which looks correct. It shows you how to check in the terminal what Python version you have installed. It also shows you how to set up Visual Studio Code, which you should also install later.

Verifying the installation:

  1. Open Launchpad and search for Terminal in the search field, then presumable you will find a terminal app
  2. Open the Terminal app
  3. Type python --version in the command line
  4. Press ENTER
  5. Verify that it says Python 3.11.0 (or whichever Python version you installed)
  6. If it says Python 2.7, try python3 --version instead

If you had to write python3 --version to get the correct version, substitute all python commands on this website with python3.

Python in the Terminal

If you run python in a terminal you will open an interactive Python session normally referred to as the Python shell. You can exit the session by running quit(). In the Python shell you can input and run Python code directly. This is pretty practical. If I want to check a small Python detail and don't feel like Googling, I might use the Python shell.

I will not focus on the Python shell.

Installing Visual Studio Code

Visual Studio Code homepage

You can write code in text editors like Notepad on Windows or TextEdit on macOS. While this works, simple text editors are usually not very practical for programming. Today many programmers, including me, use Visual Studio Code, or VS Code as it's often called. VS Code is a free cross platform code editor and works on Windows, macOS, and Linux.

Note: You should install Visual Studio Code, not Visual Studio. They are two different programs. Visual Studio is Microsoft's heavy IDE (Integrated Development Evironment). Visual Studio Code is Microsoft's open-source lightweight code editor / IDE.

Installation instructions for Windows

Installation instructions for macOS

To make Python programming nicer you should install an extension for VS Code simply called Python. This is done through VS Code itself. This can be done at any point. When you first open a Python file (a text file with the .py file extension) VS Code will ask if you want to install the Python extension ("extension" as in "plugin").

Next: Running