Programming is being used in different fields of science and engineering for a long time, from space exploration to cancer research, from geological studies to new material development, and in countless other applications for scientific computing and much more.
Many programming languages were developed to cater to the needs of numerical and scientific computing like FORTRAN (FORmula TRANslation) and MATLAB (MATrix LABoratory). FORTRAN was THE language for scientific computing due to its speed, before MATLAB came along and became popular. But MATLAB has one shortcoming, it is proprietory.
Other general-purpose languages like C, C++ are also being used for scientific computing, but they are a bit difficult to learn due to their verbose syntax and complex procedures.
In this case, a comparatively new programming language, Python, comes to our rescue. The secret of the tremendous rise in the popularity of Python in recent years lies within its versatility and ease.
In this article, we will see how to set up Python for scientific computing within minutes and get your computer ready for your scientific computing adventures.
Why choose Python for scientific computing?
The most significant advantage of Python over MATLAB is that Python is an open-source programming language supported and maintained by the community of programmers spread across the world. This gives us the freedom to create our programs and share them with the world without any restrictions.
The about page of the official Python website says
Python is powerful… and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open.
Python shares one trait with MATLAB of having a plain English like syntax. This improves the readability of the language. This also makes Python easy to learn and a great choice to be the first programming language you learn.
The readability ensures the rapid development by letting you focus on the actual topic of scientific research than spending time on writing and maintaining the complex code.
The code is read more often than it is written – Guido van Rossum
A set of programs, accomplishing specific tasks, put together are called libraries or packages. Python offers a plethora of such packages for scientific computing, so you don’t have to start from scratch. You can use someone else’s code to solve common problems in science, saving your precious time by avoiding reinventing the wheel.
NumPy, SciPy, Matplotlib, Pandas are some of the most popular packages used for scientific computing for tasks like mathematical operations, data handling and visualization, statistics, and so on. New packages are added frequently, and you also can contribute to this ever-growing collection.
Python setup using Anaconda
The easiest way to get the latest Python 3 version installed in your computer with the packages required for scientific computing is by installing
Anaconda Distribution of Python.
The distribution is available for Windows, macOS and Linux operating systems. Download the appropriate installer for your computer and follow the onscreen instructions to complete the installation process.
After installation, search for the newly installed Anaconda Navigator and start the application. The application will look something like this-
Spyder IDE
In the navigator console, you will find Spyder IDE (Integrated Development Environment). Unlike MATLAB, Python doesn’t have it’s own official GUI (Graphical User Interface). If you are coming from a MATLAB background, you will find the Spyder GUI very familiar.
In the editor, you can write your code, and it will be saved as a .py file. You can run this code by clicking or from the ‘Run‘ menu in the menu bar. You can also run the code by simply pressing ‘F5‘ on your keyboard.
The output will be displayed in the IPython console on the right side. It works similarly as the Command Window in MATLAB. You can write your code directly in the IPython console and hit ‘Enter‘ or ‘Return‘ to run it.
Above IPython Console, you will find the Variable explorer tab, which displays the names, types, sizes and values of the variables that have been created. File explorer shows files in your ‘Working Directory‘, which can be set using icon in the top-right corner. Use Help to get description and usage of any command in Python.
Jupyter Notebook
Another great way to get started with Python is with the Jupyter Notebook. This application runs in your browser and lets you write both the Python code and its explanation in plain text.
This feature of the Jupyter Notebook makes it very attractive for the scientific community as you can create interactive documents with the code and the associated theoretical explanations of your work. It comes in handy while presenting or sharing your work with others in an organized and easily understandable way.
One added advantage of the Jupyter Notebook is that you can see the output right after the line of code, which lets you understand the working of the code and correct the mistakes as you go, reducing the time of debugging.
There are multiple ways you can launch the Jupyter Notebook. You can do it from the Anaconda Navigator just by clicking the launch button, or you can launch it from the command prompt (cmd) or terminal. Search for cmd in the ‘Start’ menu and open it.
Type cd (change directory) followed by space and the path of the folder to go to the desired folder, (or the directory). Your newly created notebooks will be saved at this location with the file extension .ipynb.
In Windows, you can also start the command prompt by going to the desired folder and typing cmd in the navigation bar. It will automatically start the command prompt in the current folder. You just have to type jupyter notebook there and hit ‘Enter‘ or ‘Return‘.
Create a new Python file from the ‘New‘ dropdown menu. Jupyter notebook works with the cell structure. You can have cells containing Python code or a markdown text.
In a markdown cell, use ‘#’ character to write your headings. ‘#’ is for level 1 heading, ‘##’ for level 2 heading and so on.
Press ‘ESC‘ (escape) button and then press ‘H‘ on your keyboard to get the list of all keyboard shortcuts available. Few useful shortcuts are:
- Shift + Enter: Run the current cell and select a cell below
- Ctrl + Enter: Run selected cell
- Alt + Enter: Run the current cell and insert a cell below
- Ctrl + S: Save and create checkpoint
Visual Studio Code (VS Code)
VS Code is a free editor developed by Microsoft. As it is not an IDE, generally, you cannot run the code directly from the VS Code. You have to save the file and run it from the command prompt. But VS code offers a way to convert it to an IDE using Python Extention.
This extension includes features like debugging, autocompletion, syntax highlighting, code formatting for Python. You can install it with a few simple steps-
If you don’t see the ‘Run‘ button in the top-right corner, install the Code Runner extension by following the same procedure shown above.
You can either use Spyder or VS Code, depending on your preferences and comfort, for writing a specific code. But I would recommend you to use Jupyter Notebook while you are learning the Python language.
Manually setting up Python
Although it is not recommended for beginners, if you want, you can manually set up Python by downloading the installer for the latest version of Python from the official Python website. Follow onscreen instructions to complete the installation procedure. Check ‘Add to PATH‘ box while installing, which will allow us to start Python from the terminal.
Open command prompt or terminal by searching cmd in Start menu and type
to get the version of the installed Python. Check whether this is the same version you have downloaded.
Installing packages
To install the Python packages, you can use pip, which is a ‘package installer for Python’ for installing packages from different indexes like PyPI (Python Package Index). Type
in terminal to get the list of all the installed packages.
To install a new package, use the pip install command followed by the name of the package. For example, if you want to install the Numpy Package, you can do it by typing
To upgrade the package, use:
You can also install multiple packages by adding their names in the pip command. If you encounter any problem using the pip command, you can use the Python interpreter to run pip:
Install Spyder IDE and Jupyter notebook using pip:
You can launch the Spyder IDE from command prompt by typing
If you have trouble launching the Spyder IDE, upgrade to install all the dependancies required, like ‘PyQt5′.
Install Visual Studio Code by downloading the installer from
You can set it up for Python by following the procedure mentioned above.
And you are good to go!! Start your programming journey now and use the power of programming in your scientific or engineering work to save time, to set yourself apart from the crowd and to excel in your field. Believe me, starting to use Python for scientific computing will be one of the great things you do this year.
Check any of the articles on Python implementation of Mechanical Engineering topics for getting an idea about the use of Python for science.
Please comment below, whether you found this article useful and do suggest, what kind of information you were expecting from this article. I will try to write a part two of this article to cover those topics.