Munch Lab

Python code in files – AKA programming

So far, we have communicated with Python by typing our code into the interactive Python interpreter — the Python shell.

For quick calculations with little code and where we just want a quick result and not care about the details of the calculations behind, that is a sensible way to use Python. For code with more than a few lines, or for code you expect to have to run more than once on different input, it is not practical. If you make a mistake anywhere, or if you want to repeat the calculations on new data, you have to start from scratch every time.

Rather than typing the code directly into the interpreter, you can write a file with all the code and run it from there. Such a file has to be a plain text file, so you cannot use any old word processor or slide presentation tool, you have to use a program that uses plain text files. Any such program will do, but there are some programs that knows Python and will assist you in editing Python code, and those of course are preferable to those that only considers the code as any other text.

The Python text editor

The Enthought Python distribution you have installed comes with a text editor called IDLE. This editor is made for writing Python code, just like Word is made for writing prose. When you double click the icon for IDLE — or start it in any other way — you will get the window with the interactive shell that we worked on in the previous exercise. To create a file you can write code in select “New Window” from the “File” menu. Here you can type Python code. It will not be directly executed but by pressing F5, or by selecting “Run Module” in the “Run” menu it will execute in the interactive shell. Try this out. Write some expressions like last week in the file editor and then try to execute them.

Executing the script will just send it to the interactive shell in the other window. There is one difference from doing it this way compared to typing in the commands directly as we did before. The results of commands are not shown in the shell by default. For long files with lots of code, this is the desired behaviour!

If you want to see the result of a selected calculation made in your file, like the value of variable myResult, you need to make Python print it by putting “print” before the expression:

print myResult

rather than just

myResult

Try to get a feeling for how you interact with Python through the editor. You can try out this snippet of code:

n = 42

if n > 10:
    print "Number is larger than ten"
if n <= 10:
    print "Number is not larger than ten" 

and this:

if n > 10:
    print "Number is larger than ten"
else:
    print "Number is not larger than ten"

and this:

p = 13
x = 1

if x == 1:
    p = p - 10
    x = x - 1

print p

if x == 0:
    p = p + 3

print p

Try and make smalle modifications and see what happens.

If you have time to spare try out more of the formulas for calculating the area and volume of circles, spheres and cylinders. You might find this formula sheet helpful. Be bold and inventive. Try out some if statements too like this example:

pi = 3.14

r = 2

calculateArea = True

if calculateArea:
    print "calculating area"
    result = pi * r**2
else:
    print "calculating volume"
    result = 4/3.0 * pi * r**3

print result

What happens if you set calculateArea = False?

3 thoughts on “Python code in files – AKA programming”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: