# Use Python - it is time to stop with Matlab

*Disclaimer: this post reflects the author's opinion, and is partly subjective. There are many webpages online discussing the 'Matlab versus Python', 'Choose Python rather than Matlab' and other similar sounding questions, and I just wanted to report my experience and personal opinion about this.*

As a student who studied mostly Physics and Mechanics at university, I was trained with Matlab. I started using Python (against my will, I was a pro-Matlab guy back then) on a side project in 2016 (I needed to use Tensorflow, the originally Google-developed, now Open Source library for Machine Learning that comes with a Python API), and I am now changing all my workflow towards 100 percent Python - except for legacy code that I do not want to re-write - because I am much happier using Python for everything from heavy data processing to plotting. In this post, I will present you my opinions and experiences about Python vs. Matlab, and I hope to convince you to take the leap also.

Understand me well: Matlab was certainly a great improvement over Fortran coding when it first came out, we should be grateful for all the good ideas it brought to scientific and engineering computations, and Numpy + Scipy is clearly a copy of the good attributes of Matlab. But Matlab itself is now outdated and will / should probably fade away soon - so now you should move on to the next good technical solution.

## Python + Numpy + Scipy + Matplotlib is just as good as Matlab

In my experience, Python together with the Numpy, Scipy and Matplotlib packages (and other packages if you have specific needs, such as Pillow for image processing) is just as good as if not better than Matlab. Numpy gives you access to an array object that is both very easy to use (actually, a close copy of the Matlab array object on all aspects), and highly efficient (all operations on Numpy array are done under the hood by highly optimized C++ or Fortran code). Actually, this is the strength of Python and the Python ecosystem: the Python language is more and more used as an API (i.e., a nice interface in a high level, expressive language) to higly optimized compiled functions. Like using very fast C++ or Fortran code, but through a nice interpreted language. All functions, optimisation routines and other tools you can need for your teaching / research / work as an engineer are just a few imports away.

## There are lots of awesome Python packages without a real equivalent in Matlab: SymPy, Keras + Tensorflow, ScikitLearn, etc

While Matlab was dominant because of its number of packages until a few years back in time, I think that Python has the advantage now. If you want to perform formal symbolic computations using a CAS (Computer Algebra System), but do not want to go through the obscure way Matlab deals with this neither pay an additional expansive Maple licence: just use Sympy. If you want to experiment with some of the newest models for Machine Learning or Neural Networks, just use ScikitLearn and Keras + Tensorflow. All those packages can be installed with one simple line of code using Pip, the package installer of Python.

## Python is a much better programming language than Matlab

This is a fact: as a high level, interpreted programming language, Python is beautiful, efficient and expressive. While I used to complain about the lack of brackets for defining loops and conditional statements before I started using Python, I now think this is a brilliant feature of the language. In addition, following the PEP8 (which can be a bit painful in the beginning) will help you develop clean, (more) easily maintainable code compared with the ugly Matlab syntax and programming style. Some people use to mention that the absence of semicolon (;) at the end of each line of Python is nice, and I definitely agree with them, but I think there are much more important differences. For example, the fact that the Python syntax for classes and dictionaries is clear and easy (I had never used a class or dictionary in Matlab...) is definitely a huge improvement over Matlab. Being able to define several functions in one file, by opposition to Matlab, is also a great point (note: I have heard that this is now possible with Matlab also, from version 2017 and up). When I read some of my old bunch of Matlab scripts and functions, I do not understand how I could develop so bad, unmaintainable code - or rather, I understand well: with Matlab language features as dangerous as the 'one function per file' rule, I was doomed to not use functions as much as I should and program by copy / paste instead, therefore violating the DRY (Don't Repeat Yourself) principle.

## Python uses (...) for functions calls, and [...] for array slicing

Another point a bit in the spirit of the previous one: your code will become much clearer and easier to read (which means, shorter development time and less bugs) when you move to Python and its convention that clearly makes function calls and array slicing different looking. And, by the way, Python indexes start at 0 instead of 1 for Matlab, which was a bit of a problem to me originally, but after a bit of practise makes more sense than the Matlab convention. This simple indexing convention is making my life easier nowadays and drastically reduces the number of out of range errors I get.

## Python makes it easy to use tests to ensure software quality

Using tests forces you to write code of good quality, and will make it much easier to debug / extend your code in the future. While Matlab has some tools for writing tests, I never heard about anybody using those. I guess one reason may be that it is a bit unpractical to use tests in Matlab (back to the 'one function per file' policy?). By contrast, Python has some tools (well documented, with many tutorials online about how to use them) that make it very easy to use tests during your code development process, for example the unittest or pytest modules (see this page for a short introduction to the use of pytest).

## Using other people's code and releasing your own code is much easier in Python

It is important to be able to re-use code not only at the scale of one project, but also at the scale of the whole users community. Developing and testing packages of good quality is a time consuming process, and it is very likely that you will get a much better result in terms of both development time and number of bugs by re-using a package developed by a community rather than writing your own quick fix solution. Here also Python has a clear advantage over Matlab because of pip, its package installer. Pip will let you install, with just one line of command, any package that has been registered in the Python Package Index. Most packages you will want to re-use are available from pip, but if it were not the case you could still get the source code (for example from Github) and add it to your Python path. The fact that Python is open source will let you install a specific Python version without much difficulty through a virtual environment, if this is necessary to use the package. By contrast, I do not know about some Matlab equivalent to Pip. I have downloaded a few scripts and functions from the Matlab File Exchange, but I always found the process clumsy, obscure and time consuming compared with the simplicity of pip. In addition, since Matlab is closed-source and expensive, you will easily run into versions related problems.

## Python and its packages are free and open source - bye bye, expansive Matlab licenses and closed-source code

I did not want to mention this point earlier on, as I wanted to make it clear that Python simply is a better technical solution than Matlab, but the difference between Open Source and Closed Source is also a huge plus for Python. No more license problems, difficulties working from home etc. In addition, open source code means that you can always check to the smallest details what is happening in your code. Actually, the company behind Matlab tries to make a point of being closed-source (software developed by professionals, of industry-grade quality standards as they call it on their webpage); this is largely bullshit. By the way, Matlab comes with a 'without warranty' user license, of course; while I understand why they do so, it is a bit of a paradox when one makes reliability and guarantee of good functioning a sales argument. Some colleague at work do have experienced bugs in the past - now fixed, but still this proves Matlab arguments are wrong - with the Matlab FFT on some corner cases... This is not to say there have never been, and will never be any bugs with Python, but the situation is no better with Matlab. Just the contrary, it is well established that security through obscurity is a very bad idea, and I am pretty convinced it is the same for exactness of algorithms and programming languages. And, in addition, I am pretty sure that 99.9 percent of the bugs experienced by Matlab and Python users are due to the user's own code, not the underlying language used. Thus the importance, again, of using a sound programming language, which syntax and organisation reduces the risk of bugs in your own code.

## Python as a programming language is much more popular than Matlab

Python is much more used that Matlab. Matlab is an old-fashioned scripting language originally designed for numerical matrix calculations, extended into a sort of general programming language. Python is a high level language, that can be used together with the right packages for doing pretty anything - numerical calculations for Math of Physics, Machine learning, implementation of a webserver, etc. As a consequence, Python has a much wider user base. This (together with the fact that Python is open source, so very well known by its community) means better, faster community support and a much, much wider range of tutorials, online courses etc. In addition, you will find most of the Python programming help you need on Stack Overflow, which is much better in my experience than the Matlab Exchange page thanks to its ranking by upvote / downvote system.

## Python: one programming tool to rule them all

Due to its popularity, Python is used as the language for the API of many projects. One recent example: I needed to do some CAD (Computer Aided Design, i.e. desing of 3D parts, to be 3D printed in my case) for a side project. I started using FreeCAD (by the way, as a consequence you will find quite a few tutotials and introductions to FreeCAD on this page of my Teaching section), which is great and has a nice GUI. But one of the geometries I wanted to print needs to be generated automatically by a computer program rather than by hand. Not a problem: the FreeCAD GUI is just a way to call through buttons the FreeCAD Python API, that can be imported as a module. As a consequence, I can easily design complex 3D printed parts through some Python programs. This is becoming true of an increasing number of Open Source project: C++, Fortran or whatsoever under the hood for doing the heavy work, Python API and import as a Python module for easy scripting and re-use.