Image Image Image Image Image Image Image Image Image Image

Being Brunel |

Scroll to top

Top

2 Comments

Python For Engineers: Part 2

Python For Engineers: Part 2

It turns out there’s a surprisingly large amount 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 3!

As promised, today we’re going to put in some functionality; we’re going to calculate the gross area of the column. Arguably not that exciting, but it’ll arm us with a surprisingly large number of the building blocks we’ll need to tackle some of the harder questions.

Getting Talkative

Let’s make the wild assumption that we’ve got a 500mm square column. To calculate the area we just multiply (which is represented by the * in computer notation for no sensible reason at all) 500 by 500.

So let’s create the file rccol.py, with the following completely uninspiring code in:

And when we run it (python rccol.py) we’ll calculate the area of said column. You’ll have to take my word on that because you get absolutely zero output. This is because any programming language that constantly shouted the result of any calculation you did would be incredibly irritating.

As I hinted last week, the solution is to use the print() function. A function in programming is the same as a function in mathematics, it’s something that you give some parameters (called arguments) to, and it returns something back. In the case of print(), it takes the argument (given inside the brackets) and outputs it to the command line.

Therefore, to output our calculated area we need to wrap the calculation in a print() function:

Now when you run it you are helpfully told that 500 x 500 is 250000- what an amazing use of the gigaFLOPS your computer can offer.

As an aside, you might be wondering why you didn’t need to use the print() function when we were just typing in python directly into the interpreter last week. It might help if I tell you that the proper name for that is a REPL (or a Read, Evaluate, Print Loop); it reads your input, evaluates the answer and prints it out. As such the print() function is already baked in!

Variables

I have two major problems with our code, it’s not very descriptive (what is this 500?) and it also only works for columns that are 500 x 500. We can solve both of these problems by using variables:

Variables are a simple but powerful concept; they are the algebra of programming. In the code above we introduce the variable width and we say that it will equal 500 (this is called an assignment). Once we’ve assigned a value to a variable we can then use the variable instead of repeating that value everywhere. So instead of having area = 500 * 500, we can use area = width * depth.

Not only does this make it much more clearer what’s going on, but we can sanely make changes- knowing that I only have to change width = 500 to width = 600 in one place and all calculations using the width of the column will be updated to use 600 instead of 500.

User Input

I’m sure some of you are feeling that I’m a little prematurely satisfied with our code. I mean, who’s heard of a program where you have to go in and edit random lines of code every time you want to change your inputs?

Like print() we have the input() function, which waits for a user to type something and then returns that value to the program, so that:

Makes the variable typed equal whatever the user typed before pressing the enter key.

Now it’s tempting to say, “OK Tom- I’ve got this- let’s just use input() to get the width and depth from the user.” Well, give it a go:

However you’ll get this cryptic message back:

Traceback (most recent call last):
File "rccol.py", line 3, in <module>
area = width * depth
TypeError: can't multiply sequence by non-int of type 'str'

And this takes me to the last thing I want to explain, and that’s the wonderful world of typing.

Data Types

The problem here is that when you type in something, you’re typing in text. That means that although you may have typed a number when prompted for width and depth, you’ve actually typed in the text 500.

Now you and I both know that 500 is a number, because we can read. But computers are dumb. Python has attempted to multiply two pieces of text, and this error poses the philosophical question, how does one multiple one word by another? To Python this is just as confusing as if you’d been asked to multiply apples with oranges.

The technical word for how a program interprets data is called the data’s Type. Python has a whole variety of exciting types, however I’m going to introduce you to the three most pertinent of them, with a translation from the computer science term:

  • int – (short for integer) a whole number
  • float – (short for floating point) any number
  • string – (as in a set of characters ‘strung‘ together) text; words, numbers and punctuation

As input() returns a string (text), we have to explicitly tell Python that we want to treat it as a number (in this case, a float). To do that we’ll use the float() function, which will read the argument and attempt to convert it into a floating point number:

Now we can run the code without errors!

User Interfaces

Just to tie this all together we’re going to do some tidying up:

Here we can see that to allow Python to tell the difference between text (as a string) and text that is code to be executed, you have to quote it; "In Python, this is a piece of text". This snippet of code also uses input("prompt"); where we give input() a prompt argument so that the user knows what they’re expected to type in!

Now when we run the code for our 500mm square column we get:

RC Column Designer v0.2
Column Width: 500
Column Depth: 500
Column Area =
250000.0


Well that’s variables, the use of functions (will come to defining them soon), and the basics of types. And with this information alone we could rewrite the spreadsheet, but I want you to come away with an understanding of some of the abstractions programmers use to write cleaner code faster- so we won’t stop here.; not by a long shot.

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

Submit a Comment

Leave a Reply

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

Trackbacks

  1. […] the use of Excel, I’ll grudgingly admit that it’s a useful application. And until you’ve all learned Python– it remains one of the most popular pieces of engineering software today. So you might as […]

  2. […] 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 […]