# Python for Engineers: Part 3

It’s about time we did some more Python. 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 enough functionality to write pretty much anything- but it’ll be a long and painful process that none of us will enjoy. So instead let’s continue tooling up.

## Touching on Modules

For today’s lesson we’re going to be coping with the effective length of our column, defined as such:

• $L_{braced} = 0.5 L_{clear} \sqrt{ \frac{1 + k_t }{ 0.45 + k_t } \frac{1 + k_b}{ 0.45 + k_b } }$
• $L_{unbraced} = L_{clear} \sqrt{ \frac{1 + 10 k_t k_b }{ k_t + k_b } } \lor L_{clear} \sqrt{ ( 1 + \frac{k_t}{1 + k+t} ) ( 1 + \frac{k_b}{1+k_b} )}$

Before we do anything exciting, we’re going to write out these equations in Python. This presents us with a bit of a challenge, because while Python will merrily evaluate brackets and the four standard operators (+-/*), it doesn’t inherently know how to square root.

While we could roll our own square root algorithm using primitive mathematics, I personally feel life is too short. So instead we’re going to call upon the math module. I’m not going to dwell too much on how modules work, at the moment, but instead just introduce you to the concept. A module is a chunk of reusable functionality. In the case of the math module, this functionality is (unsurprisingly) mainly mathematical.

To use a module in Python you need to import it. This tells Python that you want it to go a fetch all the functionality of a named module and bring it in to your program. To let us perform square roots we’re going to be using the sqrt function from the math module. To make code easier to read, Python programmers tend to respect the convention of putting all import statements at the start of a file- so at the top of our rccol.py we want to add import math.

Now we have a square root up our sleeve we can finally put in our braced and unbraced lengths calculations:

There’s a few things here. Firstly, note that when using the sqrt function from the math module, we use it’s full name (math.sqrt). The . notation is a way of saying “from” or “that belongs to” in Python. So math.sqrt means, the function sqrt from the math module we imported. The second is my cheeky use of the max() function- this works exactly as it does in Excel, it picks the maximum of two numbers. Note that max() is a built-in function- that is; it’s something you don’t have to import from a module.

## Being Conditional

Our column is either going to be braced or unbraced. It’s not going to be both. Therefore we don’t really need to calculate the effective length for both conditions. And thus, it’s time to break out the control flow. These are the statements we use to control the flow of the program– getting it to loop and repeat things a certain amount of time, jump out and run through code defined elsewhere in a function, or branch depending on specific conditions.

In this situation, we need an if statement; if the column is braced then return one value, if it is unbraced then return another. In Python an if statement looks like this:

In the above example, our Python Publican has specific instructions: if the customer is called James Bond, then serve a Martini to the classic standard. Or else, if it isn’t James Bond, just set fire to the Martini.

This example also introduces you to some of the finer points of Python. Firstly, did you notice that when I compared customer_name to "James Bond" I used ==? This is because the meaning of a single equals (=) is already taken; it means assign a value to a variable. That means that we have to use something else of equality, and in Python (and the majority of programming languages, in fact) that is ==.

Secondly, did you spot the indentation? What is done for each condition is indented one level. Normally this is just good programming practice, because it makes it easier for you to see what bits of code are dependent on another. However Python is sensitive to white space- for example, it doesn’t know you’ve finished your else until you stop intending your lines. Now is also a good time to point out a Python quirk- indent with either spaces or tabs; doing both in one file will cause errors (spaces are the reigning champion).

OK- so now we have the fantastic if statement, it’s time to use it:

Now when you run the program you get asked to input a few key properties of the column, and asked if it’s braced or not. You then get the effective length appropriate to the support condition of your column. And perhaps more subtly, it means that our program can just use the variable l_eff (effective length), without having to constantly keep testing braced to see if it should be using the effective length of a un/braced column.

Knowing how to load in and use modules is going to open up a lot of pre-written functionality to us, and save us quite a bit of time. In later posts it’s also going to allow us to start spreading our program out across multiple files by writing our own modules. Similarly, writing anything that isn’t completely trivial without being able to set variables to different values depending on specific conditions is pretty awful. However there’s still a good few tools left to add to our inventory before we can really tackle this column designer, so stay tuned…

I’m going to talk about GitHub a bit later on, but if you want to have a look at the “finished” code from this tutorial, checkout:

https://github.com/thomasmichaelwallace/rccol