Munch Lab

First session

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 Canopy program in the Start menu. On Mac OS X you should be able to find them in the Applications directory. On Linux I am not sure.

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 Canopy has both,
but we will try the interactive shell first. If you start Canopy you will get a window for
typing in python. For historic reasons this window is referred to as a shell or

Once Canopy opens it spends quite a bit of time setting up your Python environment. When it finally opens choose click on the big “Editor” icon and then click “Create new file” – and you are ready to start.

The window is split in two (if it is not choose “Python” in the “View” menu). The top part is the editor where you write your code and the bottom part shows the results of running your code. It is also a command prompt for entering single lines of Python code. “In [1]” is a command prompt, which is computer speak for “please type in an instruction 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. At the command prompt, you can type in arithmetic expressions such as:

In [1]: 2 + 2

In [1]: 2 * 2

In [1]: 2 / 2

In [1]: 2 - 2

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

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:

In [1]: x = 2

You can now get the value stored in the variable just by typing it:

In [1]: x

or use it in an expression:

In [1]: x*x

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.

In [1]: pi = 3.14

In [1]: r = 2 

In [1]: circleArea = pi * r**2 In [1] print circleArea

One thought on “First session”

Leave a Reply

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

You are commenting using your 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: