Last time we... [1] Learned how to set up our computer for scripting with python et al. [2] Thought about breaking down a scripting problem into its constituent steps. [3] Solved a simple data logistics problem using natural language/pseudocode. course website: skynet.ohsu.edu/bmi507 notepad++
2 Python: introduction & data types Lecture
Why Python? [1] Why not? [2] Object-orientation [3] Just write your mind: intuitive syntax, no variable declaration [4] What about perl, ruby, and lua? [5] Massive user base. Massive. www.xkcd.com
Why Python? Some extra modules of note wxpython xlrd rpy2 nltk biopython libsvm, svmlight re glob random Graphical user interface Read/write to MS Excel Python/R middle man Natural Language Toolkit Bioinformatics miscellany Support Vector Machine Classification Regular expressions Pathname pattern matching Random number-related functionality www.xkcd.com
Python: basic conventions Write what you re thinking: assigning variables >>> var1 = 35 >>> var2 = 'This is a string.' >>> var3 = [var1, var2, ('apple', 'hippocampus')] printing information >>> print var1 ** 2 #1225 >>> var2.upper() #'THIS IS A STRING.' >>> var3.reverse(); print var3 #[('apple', 'hippocampus'), 'This is a string.', 35] importing & using modules >>> import os >>> os.getcwd() #'/Users/ambertk'
Python: basic conventions for script anatomy ***Note two things: [1] How I accessed what I needed from the sys module [2] The specific sys-module functions used.
Python: data types
Numbers. Integers Floating point number Long numbers Complex numbers int(3) = 3 float(3) = 3.0 long(3) = 3L complex(3) = 3 + 0j Caution: Type specification is more frequently done without coercion: >>> a = 2 >>> b = 5 So avoid errors by knowing which you re using, >>> a/b 0 >>> 2.0/5.0 0.40000000000000002 and their differences
Numbers. Shortcuts: x += 5 #Set x to the result of x + 5 x -= 5 #Set x to the result of x - 5 x *= 5 #Set x to the result of x * 5 x = 5 #Set x to the result of x/5
Numbers. From the math module math. copysign(x, y) sum(iterable) factorial(x) ceil(x) floor(x) isnan(x) isinf(x) exp(x) log(x, base) sqrt(x) cos(x) acos(x) hypt(x, y) radians(x) degress(x) pi e
Truth. Each of the following evaluate to false: None False 0, 0L, 0.0, 0j, (), [] {} Python is promiscuous with its beliefs. Everything else is True. Everything. >>> bool('') False >>> bool(' ') True >>> bool(['']) True
Truth. >>> var2 = True >>> var3 = False >>> var2 or var3 True >>> var2 and var3 False >>> var1 = 5 >>> 5 == var1 True >>> 5 >= 6 False
Truth. Fairly self-explanatory: objects of none-type are nothing. Well...they re something, but the thing they are is nothing, which is to say that nothing is something, and, sometimes, something is nothing, which is something. Let s move on. >>> var1 = None >>> print var1 None
Letters. Strings are one of a group of sequence-like objects in python, along with lists, tuples, and some others. Strings are also one of a few mutable objects, in python--that is to say, once they are created, they can t be changed in the same way you might change other types. You can obtain portions of your strings using the syntax depicted in this table. min and max aren t always meaningful for strings, but they ll still give you an answer. s[-1] last item in s Python has particularly well-developed string methods. Many of which we ll discuss at length later on in the course.
Letters. Strings are defined with single or double quotes, or using the str(x) function. If you need to include information in your string that you don t yet know (perhaps the result of a calculation), you can use %-substitution: >>> var1 = 54 >>> 'The result of our calculation is %s.'%(var1) 'The result of our calculation is 54.' where %s is a string.
Letters. This works for other types as well: >>> import math >>> 'The result of our calculation is %0.4f.'%(math.pi) 'The result of our calculation is 3.1416.' >>> 'The result of our calculation is %i.'%(math.pi) 'The result of our calculation is 3.'
Letters. Basic string methods Read more about strings at http://docs.python.org/library/stdtypes.html
Letters. --- raw strings and windows file paths --- formatting: \n\t\b Other things you should know about strings Read more about strings at http://docs.python.org/library/stdtypes.html
Letters. Advanced string methods & more translate encode/decode Regular expression matching Read more about strings at http://docs.python.org/library/stdtypes.html
Lists. >>> My_Opinion = [ Lists, are, fantastic ] Unlike other languages you might have learned, a list can contain objects of different types (but see array module): >>> [ Lists + are + fantastic, 5, None, 3.0, 3.0/5.0] ['Listsarefantastic', 5, None, 3.0, 0.59999999999999998] Lists are an ordered container for your information. They are heterogeneous sequences, and, like strings, can be accessed using the previously described slice operations: >>> My_Opinion[0] #'Lists' >>> My_Opinion[-1] #'fantastic'
Lists. List Methods
Lists. >>> range(0, 5) [0, 1, 2, 3, 4] >>> hello.split( e ) ['h', 'llo'] Important List-generating Functions >>> {'a':1, 'b':2, 'c':3}.keys() ['a', 'c', 'b'] >>> random.sample(range(0, 5000), 5) [3169, 3702, 2917, 695, 4213]
Lists. List Comprehensions (where lists get fun) >>> [i ** 2 for i in range(0, 5)] [0, 1, 4, 9, 16] >>> [(i, j) for i in range(0, 2) for j in ['a', 'b', 'c']] [ (0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c'), ]
Dictionaries. >>>{1: Dictionaries, 2: contain, 3: unordered,4: information. } Unlike lists, which are ordered, dictionaries are like a paper bag full of your data. Entries are accessible by a key, which needs to be something immutable (string, number, tuple). The syntax is >>> dx = { key1 : entry1, key2 : entry2 } >>> dx[ key1 ] entry1 One can extract the keys or values in a dictionary (in no particular order) by >>> dx.keys() [ key1, key2 ] >>> dx.values() [ entry2, entry1 ] The zip function provides an alternative way for creating dictionaries: >>> zip(dict(( key1, key2 ), ( entry1, entry2 )))
Dictionaries. Other Dictionary Methods & Functions Using Dictionaries len(dx) key (not) in dx copy() dx.clear() dx.items() dx.get(key, default) dx.pop(key, default) dx.setdefault(key, default) number of keys in dx evaluates to (False) True if key exists in dx returns a copy of dx clears dx; same as dx = {} returns key/value pairs in dx returns the value associated with key, otherwise returns default returns value associated with key, and removes it from dx if key is in dx, returns associated value, else sets key:default in dx, and returns default
Tuples. Tuples are immutable containers of information. They re like lists, but they can t be altered (in the same way as strings). Since they re immutable, they can be used as dictinoary keys. >>> var1 = ('a', 1) >>> type(var1) <type 'tuple'>
Combining things. Evaluation proceeds from left to right, and inside to outside. What would the following produce? >>> var1 = 'hello.' >>>'o'.join((var1.title().swapcase().split('e')))[0:-1] + 'w' 4 1 2 3 5 6
Combining things. Evaluation proceeds from left to right, and inside to outside. What would the following produce? >>> var1 = 'hello.' >>>'o'.join((var1.title().swapcase().split('e')))[0:-1] + 'w' 4 1 2 3 5 6 Although this is possible, blind use of this type of code writing is somewhat unpython. Make sure to write code that you and others are able to quickly understand, or you will be driven from town.