PYTHON LECTURE OUTLINE (0) Disclaimers - there is no "one true/best language"; you will have to learn others - we will probably run out of time; can continue in a workshop outside of class (1) Resources - docs.python.org - "index" is most useful - "Library Reference" is next in usefulness - stuff I put up on Assignment 1 page for BME 205: 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) - stuff to which I refer throughout this lecture (2) Starting it up - Python versions - Make sure to specify "python2.6" on SOE machines - Command-line interpreter - using it as a calculator; basic caveats about type conversion - whitespace only matters for indentation and EOL - history (readline support) - Ctrl+D to exit (3) Some Python types - type overview (draw table for type conversion syntax versus shorthand) - 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" - 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 build-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 - some standard types (with special attention paid to the binding/reference model) - see http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy - 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 - understanding how Python iterates over ranges - explains why out-of-bounds is allowed - all Python ranges (i,j,k) ALWAYS: - init to i - steps of i+k, i+2k, i+3k... - terminate BEFORE j (j-1 or j+1, depending on sign of k) - left-hand slices versus right-hand slices - 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 (actually a "mapping" type) - what can be a key? any hashable type - all immutable types can be a key: string, tuple, number - don't confuse with lists - brief note about other types: 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 - shebang - chmod - comments - imports - sys module - sys.argv - sys.exit() - printing to STDERR - whitespace - conditionals - assert - loops (file I/O) - simple comparator functions - iterating through lists of tuples (6) Additional stuff - ONLY OF THERE IS TIME LEFT - more on iterators - a special note about xrange() and loops - function parameters: positional, named, extra, defaults - caveat about modifying default params (7) Questions and feedback - another workshop outside of class time?