Before getting into programming, I worked as a Mechanical Engineer in the steel and rail industry. Whilst a lot of engineering inevitably involves boring bits like finance and project management, we do spend a fair amount of time doing mathematical calculations, whether it be for design or operations.
At University I did a large amount of study and calculations by hand. This is actually essential in order to really get to grips with the underlying formulae and concepts – plus we could only bring a calculator into exams – no laptops obviously! I remember calculating Fourier transforms by hand being particularly arduous. When you get into the real world of work, in many cases, once you’ve got the right formula for your problem you need to run the calculation multiple times which means setting up a computer to do it for you. Which begs the question: which software package are you going to use? It seems like there are three popular generic choices: Matlab, MathCAD and Excel. I’m going to suggest five top reasons why Python is better.
1. Python is free and open source
At the time of writing Matlab costs £1800 for the basic commercial license (exlcuding many add-ons and updates), and MathCAD £675 per year for the commercial license. Most companies probably buy Microsoft Office anyway (business license £7.90/user per month), or you could use a free alternative like Libre Office. But Python is totally free. That could be a significant economic saving especially for number of users. Of course that assumes Python is as capable as the others – which I think it is.
2. Python can automate everything
When I worked in the steel industry one of my jobs was to produce the report for the 9am morning meeting. This was basically involved taking production KPIs from some spreadsheets or other documents and putting them into an Excel pro-forma. It only maybe took 15 minutes or so, but if you add up 15 minutes every day it ends up being a fair amount of time. This is the kind of thing python can do automatically – simply point it to the input files and it can pull data and produce a some kind of report automatically. If you can constantly doing repetitive tasks in Excel, then that is a candidate for automation. I think that this is probably where the major benefits could be in using Python over Excel, especially in operations departments. (Although Matlab can also execute its own scripts, it would be overkill to buy it just for this kind of data manipulation).
3. Python can go beyond simple calculations to machine / deep learning
Python has advanced machine learning capabilities for engineers looking to be smart in their data analysis. I’m not talking trivial things like taking an average or a maximum value in a data set but using machine learning to analyse multi-variable data to find an optimum solution or spot patterns in the data. This could be object classification (sorting recyclable materials from rubbish), image recognition (analyzing a physical product for defects from a picture or 3D scan), component failure analysis or a host of other problems which would require a lot of manual labour otherwise. This kind of data science is already commonplace in the IT industry and will become more and more valuable to engineers in the coming years. Python has the scikit-learn module which makes this kind of programming accessible to engineers.
4. Python code can be turned into standalone desktop or web apps for other users
There are many cases where the final deliverable is (or could be) a standalone piece of software or web application that allows end users to process some data without relying on the engineer to do the calculations each time. If you’ve already created your automated python script, it is not that difficult to create a standalone app with a GUI or even a web app, especially if you deploy to a PaaS such as Heroku or Openshift, so you don’t even need to know how to manage a server yourself. Let’s take my example in part (2), once we’ve got a python script to automatically pull data in from various sources for reporting purposes, if we turned this into a web app, it could make these reports available to multiple people at there own request, rather than relying on an emailed Excel sheet. Historical data could also be saved for further analysis etc.
5. Python can be easily integrated into a physical system (especially with a Raspberry Pi)
This final reason perhaps owes more to the Raspberry Pi that to Python itself, however, clearly the two are symbiotic (Remember ‘Pi’ is supposed to represent Python, the Raspberry Pi foundation’s favored education programming language). With Python and the Raspberry Pi, its very easy to integrate your data analysis or calculation with physical data collection or output. Let me give you an example. After my finishing my masters degree I stayed on at university to do a months additional research into mountain bike suspension systems. I won’t go into all the details, but from the data analysis point of view, we had two accelerators and a magnetic wheel speed sensor to log data from. In order to do this, we plugged the sensors via a very long set of wires into a fairly expensive mircocontroller (I’m guessing a few hundred quid), with some extra electronics on a circuit board. This was then connected to a laptop via a serial port. This was all bundled up and put in a backpack. On the software side, we had Matlab and Simulink running, which required us to describe the whole system diagrammatically in Simulink, which took a fair amount of work. After the live testing (with mountain biker equipped with huge backpack full of electronics, which you could only last as long as the laptop battery went for!), we had our data which we processed in Matlab and produced some graphs. Admittedly this was almost 10 years ago, but nowadays this could be done a lot more quickly and simply with Python and a Raspberry Pi. I’d probably start of with using an arduino to capture the data before sending it to the Pi, but I’m fairly sure I could have a system doing the same thing, in a small box attached to the bike, even with a live visual display of what was happening in less than half the time it took originally. That is just one example – there are millions of other physical computing possibilities with Python too.
Are there any drawbacks?
Although I’m a big fan of python it would be wrong to say it is the right choice in every case. I think there are a couple of cases where it probably isn’t the right piece of software to do you calculations. These are:
1. You are doing calculations in a highly specialised field such as finite element analysis (use ANSYS) or Computational Fluid Dynamics (use Fluent). There may be very specialized cases where Matlab also has a function specialised for your use case, although you could undoubtedly write this in Python too.
2. One-off design work with linear algebra calculations in mathematical notation and explanatory diagrams. Although you can display mathematical notation in Matplotlib (python data visualisation module), python code itself still needs to be written in ascii / utf text format so if your calculations need this kind of presentation, MathCAD is probably a better way to go. If I was doing a one-off structural design even hand written calcs with accompanying diagrams might actually be easier than any software package – although you lose the automated calculation and checking.
The only other (slight) drawback is that there is a learning curve with Python, if you are coming from Matlab or have never programmed before, since you’ll need to get it installed, find an IDE and learn about installing packages and the python syntax. However, I think it is worth the up front time investment in almost every case, as the benefits later on are significant.
For the benefit of fairness, here’s a link to Mathworks sales blurb of why they think Matlab is better than Python. But I honestly don’t think there arguments are any good, given my reasons above. And if you want more ammunition, Phillip Fedlman also gives some more specific reasons with regards to langauge syntax why Python is better than Matlab.