Now, depending on where you installed Python when you ran the installation program, you can access it in different ways.

On Windows you should be able to find the Python tools in the Start menu. On Mac OS X you should be able to find them in the Applications directory. On Linux it is probably easiest to start them from the shell.

In this class we will interact with Python in two different ways: In the interactive mode or by executing Python code written in a Python text editor. The program IDLE has both, but we will try the interactive shell first. If you start IDLE you will get a window for typing in python. For historic reasons this window is referred to as a shell or terminal. It will look something like this:

The last line here, ">>>" is a command prompt, which is computer speak for "please type in your instructions to Python here and press enter".

Evaluating simple expressions

You can use Python as a very simple calculator, where you type in expressions and Python gives you the value they evaluate to.

To try this out, start an "interactive Python shell". If you are on one of our UNIX machines you do this by typing "python" in a shell and pressing enter, or if you have installed Enthought's Python - as we recommend - you do it by starting IDLE. The two versions work in slightly different ways, but for the purposes here they are essentially the same.

Simple arithmetic expressions

At the command prompt, you can type in arithmetic expressions such as:

>>> 2+2
4

>>> 2*2
4

>>> 2/2
1

>>> 2-2
0

Play with this a bit to get a feeling for it, but don't spend too much time on it — there isn't really that much to it and it does get dull rather quickly.

Try to figure out what the operator ** does, as in 2**2 or 2**3.

If you feel particularly brave, try doing something crazy. Divide by zero or something.

Integers and floats

What happens if you evaluate the expression 1/2?

The reason this happens is that Python distinguishes between integers (whole numbers) and floats (decimal numbers) and since both 1 and 2 is an integer it does integer division. Integer division is just normal division where the result is rounded down to an integer.

To avoid this, you must make explicit that you want to work with floating point numbers. You can do this in two ways: either write 1.0/2.0 (where the .0 tells Python that the numbers are floats) or write float(1)/float(2) (which explicitly tells Python to translate the integers 1 and two into the corresponding floats).

You don't actually need to make both numbers explicitly floats; one will suffice as Python will use float division if just one of the two numbers are floats.

Play a bit with this as well. See what happens when you mix floats and integers.

The distinction between integers and floats won't matter much for how we usually use Python, except for the division case where it is important to remember.

Using variables

You can store the value of an expression in a variable just by writing the name of the variable, then "=" and then the expression, like this:

>>> x = 2
You can now get the value stored in the variable just by typing it:
>>> x

or use it in an expression:

>>> x*x
4

Circles, spheres, and cylinders

We will try this out by calculating the area of a sphere. Values common for the expression for all three are pi and the radius, r.

It makes sense to use variables for those, e.g.

>>> pi = 3.14

>>> r = 2

>>> circleArea = pi * r**2

>>> print circleArea
12.56

Index

Contact info

Office address:
Bioinformatics Research Centre (BiRC)
Aarhus University
C.F. Møllers Allé 8
DK-8000 Aarhus C
Denmark
Office phone:
+45 871 55558
Mobile phone:
3013 8342
Email:
kaspermunch@birc.au.dk