Image Image Image Image Image Image Image Image Image Image

Being Brunel |

Scroll to top

Top

4 Comments

Python For Engineers: Part 1

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:

Download Anaconda for Python 3.5

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.

Download Visual Studio Code

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.

I already know how to write Python, so this is for all you inquisitive engineers out there- I will answer any questions, either as a comment, by e-mail, or on twitter.

Submit a Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks

  1. […] of engineers excited about the prospect of learning Python! If you haven’t read part one, I suggest you do that now– as I can’t start every post explaining how to install and run Python […]

  2. […] already talking about Python in another series, but the fact remains that for whatever reason the scientific community love a bit of Python. […]

  3. […] my Python guide, this will focus more on the process of getting something together. There’s quite a lot to […]

  4. […] Who’s excited? In the last couple of posts I’ve helped you literally get started by setting up a Python environment and then set you down the road of variables and in/output. And we could stop there; it’s […]