Python for Engineers: Part 3
[dropcap style=””]I[/dropcap]t’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:
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. [highlighted_text]A module is a chunk of reusable functionality.[/highlighted_text] 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
Now we have a square root up our sleeve we can finally put in our braced and unbraced lengths calculations:
l_clear = 7000
k_top = 0.1
k_bot = 0.1
braced = 0.5*l_clear * math.sqrt((1 + k_top/(0.45+k_top)) * (1 + k_bot/(0.45+k_top)))
unbraced = l_clear * max(math.sqrt(1 + 10*k_top*k_bot/(k_top+k_bot)), (1 + k_top/(1+k_top)) * (1 + k_bot/(1+k_bot)))
There’s a few things here. Firstly, note that when using the
sqrt function from the
math module, we use it’s full name (
. 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.
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. [highlighted_text]These are the statements we use to control the flow of the program[/highlighted_text]- 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:
if customer_name == "James Bond":
martini = "shaken, not stirred."
martini = "on fire."
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
"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:
l_clear = float(input("Clear height: "))
k_top = float(input("k (top): "))
k_bot = float(input("k (base): "))
braced = input("Braced? (yes/no): ")
if braced == "yes":
l_eff = 0.5*l_clear * math.sqrt((1 + k_top/(0.45+k_top)) * (1 + k_bot/(0.45+k_top)))
l_eff = l_clear * max(math.sqrt(1 + 10*k_top*k_bot/(k_top+k_bot)), (1 + k_top/(1+k_top)) * (1 + k_bot/(1+k_bot)))
print("Effective Length = ")
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: