Defining a function for the mean calculation:
def mean(x):
    """Return the mean of a list of numbers."""
    s = 0.
    for i in x:
        s += i
    return s/len(x)
L1 = [.5,10,17,3]
mean(L1)
Should we report our result with return or print?
def mean2(x):
    """Print the mean of a list of numbers."""
    s = 0.
    for i in x:
        s += i
    print s/len(x)
x = mean(L1)
y = mean2(L1)
mean uses return, so the result is stored in x.
x
mean2 uses print with no return statement, so it returns the default return value, which is None.
y
z = None
z
Normal test for equality: z and y both point to None, so their values are equal.
z == y
Test for identity: z and y point to the same location in memory (because the None value is unique and lives in a single location)
z is y
Advanced exercise: The "%p" format string returns an object's address in memory. Can you use this to figure out how python handles memory management for int and float values. Is there a difference?
def mean3(x):
    #Return the mean of a list of numbers.
    s = 0.
    for i in x:
        s += i
    return s/len(x)
Because mean is documented with a docstring, its documentation is visible to IPython via mean? or tab-completion on mean(.
mean3 uses a comment, which is invisible to the built in help.                                                                                                                                 
Functions for creating integer sequences:
x = range(10000)
Using IPython's %time magic to time our mean function
%time mean(x)
For more accurate measurements, use %timeit, which averages over multiple runs (c.f. %timeit? for details)
%timeit mean(x)
%time?
Demonstrating that IPython magics are converted to python function calls when logging:
%logstart -o day2.log
%less day2.log
Using introspection to find the source of the sqrt function in the pylab environment:
sqrt.__class__
Ah, it's the version from numpy (not part of standard Python. Standard Python includes a slightly different sqrt implementation in the math module)
sqrt.__class__.__module__
? is an IPython-specific way to look up the documentation and source for an object:
sqrt?
Advanced: There are several ways to import modules from directories other than the current directory. The most common is to modify the PYTHONPATH environment variable. You can inspect and modify the current import path via sys.path.
import stats
stats?
dir lists the contents of an object:
dir(stats)
stats.mean?
stats.mean(L1)
from stats import mean
mean(L1)
Here, we edited the definition of mean in stats.py and experimented with how to make the change visible to Python:
mean(L1)
stats.mean(L1)
import stats
stats.mean(L1)
reload(stats)
stats.mean(L1)
mean(L1)
from stats import mean
mean(L1)
Opening a file in read-only mode:
fp = open("stats.py")
Read the first 10 bytes:
Python scripts and modules are simple text files, so Python translates the bytes to text using its default text encoding
fp.read(10)
Read the next 10 bytes (note that read has a side effect: the file pointer moves through the file as it is read, so sequential calls to read produce different results)
fp.read(10)
Read the rest of the file:
fp.read()
Rewind by reopening the file
fp = open("stats.py")
Because stats.py is a text file, we can use readline in place of read to read a whole line of text (including the end-of-line character)
fp.readline()
fp.readline()
fp.readline()
For file objects, next has the same behavior as readline.
fp.next()
Objects with a next method are iterators, which can be used by a for loop
To be more precise: for loops operate on iterables, which are objects with an __iter__ method, which is responsible for generating or pointing to the appropriate iterator object
for i in open("stats.py"):
    print i
The example above is double spaced, because print adds its own newline (\n) character. We can use the rstrip string method to remove the redundant newline (and any other trailing whitespace) ftem each line of the file:
for i in open("stats.py"):
    print i.rstrip()
To write data to disk, we can open a file in write mode:
out = open("example.txt","w")
out.write("This is some text")
out.write("this is some other text")
out.close()
for line in open("example.txt"):
    print line.rstrip()
If we want to write output to separate lines, we need to add explicit newlines:
out = open("example.txt","w")
out.write("This is some text\n")
out.write("this is some other text\n")
out.close()
for line in open("example.txt"):
    print line.rstrip()
We have been using relative paths, which look for files relative to the current directory. We can specify absolute paths instead, by starting with a forward slash (/).
Windows uses backslash () as its default path separator, but Python will do the correct conversion of forward slash on Windows, so you don't need to worry about this
for line in open("/home/mvoorhie/Projects/Courses/PracticalBioinformatics/python/May2013/example.txt"):
    print line.rstrip()
readlines returns all lines of a text file as a list of strings
data = open("supp2data.cdt").readlines()
len(data)
data[0]
print data[0]
data[1]
data[-1]
The string split method splits a line on whitespace (or on an arbitrary string, if supplied)
data[1].split()
data[1].split("\t")[:10]
data[1].split("\t")[2:10]
Coercing strings to floats:
float(data[1].split("\t")[2])
Handling missing values with a try..except block:
r = []
for i in data[1].split("\t")[2:]:
    try:
        r.append(float(i))
    except ValueError:
        print i
r = []
for i in data[1].split("\t")[2:]:
    try:
        r.append(float(i))
    except ValueError:
        r.append(None)
print r
r = []
for i in data[1].split("\t")[2:]:
    try:
        r.append(float(i))
    except ValueError:
        r.append(0.)
print r
Oops! Better log my session!
%logstart -o BMS270b.2013.02.log