PYTHON LECTURE OUTLINE BME 205, Fall 2010 Contact: auzilov@ucsc.edu (0) Disclaimers and Background We will probably run out of time; can continue in a workshop outside of class. Why Python? - Easy for beginners, but many advanced features. - Consistent syntax (more or less). - Good for prototyping (fast to write). - Good for debugging (e.g. stack traces). - Powerful standard library: - unit tests (module "unitest") - profiler (module "profile") - regular expressions (module "re") - iterators (module "itertools") - tests of docstring-embedded examples (module "doctest") - and many more! - Widely adopted, including by bioinformatics community. - Thriving development community for language itself and associated tools But remember: there is no "one true/best language"; you will have to learn others. Several Python implementations: CPython, Jython, IronPython, etc. (1) Resources - docs.python.org - "index" is most useful - "Library Reference" is next in usefulness - These are the most relevant pages for BME205 Assignment #1: Tutorial docs: http://docs.python.org/tutorial/interpreter.html http://docs.python.org/tutorial/introduction.html http://docs.python.org/tutorial/controlflow.html http://docs.python.org/tutorial/inputoutput.html Understanding strings (and more generally, sequences): http://docs.python.org/library/stdtypes.html#string-methods http://docs.python.org/library/stdtypes.html#string-formatting-operations http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange Useful standard library modules: http://docs.python.org/library/string.html http://docs.python.org/library/re.html http://docs.python.org/library/sys.html - Python in a Nutshell (useful for advanced users only) emacs/aquamacs packages John St. John likes: http://www.emacswiki.org/emacs/AutoComplete - easy to install - will look in current buffer to append to current dictionary on-the-fly - comes with Python dictionary, but can extend with other dictionaries, e.g. YaSnippet (http://code.google.com/p/yasnippet/) - but nontrivial to integrate YaSnipped with AutoComplete, although possible (2) Starting it up - Python versions: do NOT use 3.x yet! - Python installation on SOE machines (refer to Kevin for this one) - Command-line interpreter - using it as a calculator; basic caveats about type conversion - getting help: - whitespace only matters for indentation - history (readline support) - Ctrl+D to exit (3) Python types and interpreter features - type overview - int, float, list, string, tuple, dict, None - basics: int and float - an aside about the Python data model - everything is a reference; the object to which we refer has the type - what is an object? what is a method? why not just use functions? - the concept of "binding": associating a name to a reference - re-binding: powerful but dangerous! - caveat about re-binding built-in functions like "type" and "chr" and "next": see http://docs.python.org/library/functions.html and http://docs.python.org/library/stdtypes.html - or use interpreter to tell if a built-in function or type - caveats about "modifying" a reference - you don't modify references other than re-binding; you modify the object TO WHICH the reference(s) point - help() function, takes strings or references: help ('help') help (help) # same, as long as name is bound to a reference help ('modules') x = 205 help (x) s = 'some arbitrary string' help (s.rstrip) # help on object methods - some standard types (with special attention paid to the binding/reference model) - see http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy - always keep in mind: is object of this type mutable or immutable? iterable or not iterable? - None type - sequences: mutable and immutable - lists (mutable): - creating - .append() - .sort() versus sorted() - in place (no return value) versus new copy returned - sorting 'listOrig' and 'listNew' (test understanding of references) - min() and max() - an aside: method versus function - looking at contents; modifying contents - lists store REFERENCES (which can get rebound) - slices - I will not cover this --- see additional notes instead (BME_205_Python_lecture_code.txt) - list comprehensions - only in Python 2.5 or newer! - strings (immutable): single versus double quotes - all slice operations on lists can also be done on strings - some string methods: {l,r}strip, join, replace - tuples (immutable) - useful for: - string formatting (will cover later) - as keys to dictionaries - dictionaries - I will not cover this --- see additional notes instead (BME_205_Python_lecture_code.txt) - brief note about other types: dictionaries, sets, modules, callables, classes, files, etc. (4) Printing and string formatting - print keywords - string formatting with % operator and tuples (5) Writing your first Python program (BME_205_Python_lecture_firstProg) - shebang - chmod Two ways to run a Python program: - use shebang to locate interpreter: ./progName - ignore shebang, specify explicitly which Python interpreter to use (also ignores chmod +x setting): python progName /usr/bin/python progName - comments and docstrings - imports - sys module - sys.argv - sys.exit() - printing to STDOUT, STDERR - loops over input - conditionals and whitespace/indentation - assert (6) Writing a more advanced Python program (BME_205_Python_lecture_secondProg) (7) Questions and feedback - another workshop outside of class time?