Python For Engineers: Part 1
I don’t want to hazard a guess how many times I’ve complained about the terrible misuse of Excel that I often witness at the hands of over-zealous engineers. However, last time I did this someone kindly pointed out that, except for one glancing blow, I’ve never really done much to remedy this first world problem (in fact, if anything I’ve helped contribute to it!).
A prime example of Excel abuse is the fantastic TCC column spreadsheet. So in an attempt to put my money where my mouth is, I’m going to kick off a regular series of posts where I’ll take you, dear reader, through the process of converting this magnificent monstrosity into a Python application.
That’s right, we’re going to do some programming.
n.b. this guide is going to be written like you’re a windoze user without admin rights, however every tool I suggest has an OS X and Linux version.
Step One: Getting Python
I don’t want to dwell too much on the what or why of Python, as, over this series, I’ll be hopefully do a better job of demonstrating this than a paragraph of computer science terminology could ever do. However, you might as well know upfront that it’s a programming language that is favoured for science and engineering applications by academia; and that it’s named after Monty Python.
At the risk of introducing some upfront confusion, but hopefully saving you some pain in the long run, I’m going to recommend that you install Python by using the distribution “Anaconda“. Python is wonderfully diverse and comes in many varied forms and with a whole variety of optional packages. In order to focus on the programming I’m just going to come straight out and tell you to download Anaconda for Python 3.5 for now:
Make sure when you click through the installer that you opt to Add Anaconda to my PATH environment variable and Register Anaconda as my default Python 3.5 unless you know any better.
Programming as we go
For the last part of this introduction I want to get you comfortable with the way python works, so we can just dive straight into writing some more serious code in the next post.
First open up a command prompt and type python and you’ll be dropped into a ‘python prompt’. From this prompt anything you type will be evaluated when you press the enter button. Type
1 + 1 and press enter. Python will respond with
2. Not massively impressive, I know, but you’ve just written and executed some python! Feel free to try a few more calculations, but when you’re ready type
quit() to escape.
Of course, this way of using python has a fairly obvious drawback, the user of your “program” will have to type it all out themselves (which was not unknown in the olden-days). Python allows you to write out all the commands that make up your program in a file, which can then be executed- saving you a having to type everything out every time you want to use it!
I’m going to talk a little bit about where to write code next, but for now just open Notepad and type the following:
print(1 + 1)
I’ll come back to the
print() in the next post (although short story long it means that the result of 1 + 1 will be shown). For now save the file as sum.py. Now browse to this new file in the command prompt and type
python sum.py. You’ll be rewarded with the number
2. Throughout this series will be writing our code in files and then using the
python command to run them.
Programs to Write Code
Programmers have a habit of being weirdly opinionated about the editors you should use to write code. However I think I can say without fear of contradiction that none of them would be sadistic enough to suggest that you should use Notepad. Like with Anaconda, I’m just going to tell you to use Visual Studio Code, unless you think you know better. It’s free, light weight, and known how to colour your Python code so it’s easy to read.
There are a lot of more powerful and perhaps more Python centric editors out there (PyCharm being my professional choice), but the majority of them will come with a steeper learning curve, and, for now, I don’t want to teach you how to use a Python editor, but how to do some actual coding.
Now with Python installed and the knowledge of how to literally write some Python we’re armed for part two, coming soon, where we’ll make a start on our TCC spreadsheet replacement.