{ "metadata": { "name": "", "signature": "sha256:af51a48a3269ea6bfb377862b7a17824ba47158e9395331e2682a855e2e61784" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Biology 723, Fall 2014: Class 1" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Help and Documentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A key skill for becoming an efficient programmer is learning to efficiently navigate documentation resources. The Python standard library is very well documented, and can be quickly accessed from the IPython notebook help menu or online at the python.org website. Similar links to some of the more commonly used scientific and numeric libraries are also found in the Ipython help menu.\n", "\n", "In addition, there are several ways to access abbreviated versions of the documentation from the interpetter itself." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# help(min)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# ?min # this will pop-up a documentation window in the ipython notebook" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Data Types" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Numeric data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the simplest ways to use the Python interpretter is as a fancy calculator. We'll illustrate this below and use this as an opportunity to introduce the core numeric data types that Python supports." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# this is a comment, the interpretter ignores it\n", "# you can use comments to add short notes or explanation\n", "\n", "# add two integers (whole numbers)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# add an integer and a real number (floating point number)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# multiplication" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# division" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# be careful with integer division! is the output what you expect?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# The % (modulo) operator yields the remainder after division" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# exponentiation" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# exponentiation with fractional powers, **0.5 = square root, **(1/3.) = cube root" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# numerical operators differ in their precedence\n", "# contrast the output of this line with the line below" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# good habit to use parentheses to disambiguate potentially confusing calculations" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# complex numbers" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# adding complex numbers" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# complex multiplication" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `type` function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a built-in Python function called `type` that we can use to query a variable for it's data type" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# type(2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#type(2.0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ " # when adding variables of two numeric types, the outcome\n", " # is always the more general type" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Variable assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The results of a calculation can be given a name, and then reused in a different context. This is called variable assignment." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# calculate area of circle using variables\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions encapsulate a series of related operations or calculations. Writing functions is fundamental to programming so we'll introduce them right away. The general form of a function definition in Python is:\n", "\n", "```\n", "def func_name(arg1, arg2, ...):\n", " body of function\n", " return result\n", "\n", "```\n", "\n", "Note that Python is white space sensitive, rather than using delimiters like braces. White space sensitivity often surprises people who are already familiar with other languages like C or Java. This feature of the language arguably lends iself to readability of code.\n", "\n", "As show above the body of a function definition must be indented. A Python aware editor / environment (like IPython) will help you get the indenting correct.\n", "\n", "Below, we present our first \"real\" function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# write an area_of_circle function" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We call our function as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# area_of_circle(4)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# help(area_of_circle)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you define a function in the interpreter, it's immediately available for use. By contrast, functions and variables defined in modules or libraries you install usually need to be \"imported\"" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from math import factorial # import the factorial function from the math module\n", "help(factorial)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print factorial(10) # factorial(x) = x * (x-1) * (x-2) .... * 2 * 1 " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this class, I've configured IPython to import a bunch of commonly used functions from Numpy and Matplotlib, which provide convenient functions for numerical computing and generating scientific figures." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "In class assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function, `area_of_rect`, that calculates the area of a rectangle. Your function shoudl take two arguments, `length` and `width`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# write your area_of_rect function here\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Booleans" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python has a data type to represent True and False values (Boolean variables) and supports standard Boolean operators like \"and\", \"or\", and \"not\"" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# defining boolean variables" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# not" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# and" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# or" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# compound Boolean statements" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Comparison operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python supports comparison operators on numeric data types. The results of applying these comparison operators are Booleans." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# less than" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# greater than" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# less than or equal to" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# tests equality" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# equality for floating point numbers, the results of this might surprise you" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# the problem is that sqrt(5) can not be represented exactly with floating point numbers\n", " # this is not a limitation of Python but generally true for all programming languages" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# here's one way to test approximate equality when you suspect\n", "# a floating point calculation might be imprecise\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "None type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The None type is used in Python to represent the absence of a value (or a null value). Many functions will return a None value if a calculation is valid but there's nothing to return. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = None" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(x)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x is None" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# this function takes no arguments\n", "# and implicitly returns None\n", "\n", "def say_something():\n", " print \"Something!\"\n", " \n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = say_something()\n", "print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next core data type we'll look at is strings." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define a string variable" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# print a string" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String's can be specified using either single or double quotes." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# string concatenation" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# get the length of the string" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a variety of string related functions. Here are a few for illustration; check out the Python Standard Library for more." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# methods on strings \n", "# title case" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# count the number of times the substring appear in s3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# split" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Indexing and Slicing Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access individual characters in a string by indexing. Python strings (and other sequences) are zero-indexed, meaning the first character is zero, and the last element of a string of length $n$ is $n-1$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# print in version 2.7+ is a fxn\n", "# in version <= 2.7 print is a keyword" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# first char of string" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# whoops, generates an error! Remember zero indexing!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# for a string of length n, the index of the last element is n-1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing with negative numbers indexes from the end, starting with -1" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing is like indexing but allows you to retrieve more than one item at a time. Indexing goes from the first element (inclusive) to the last element (non-inclusive)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# first six elements" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# next four elements" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# last five element" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use extended slicing, `s[start:end:step]` as follows" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# get every 2nd element" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# get every element walking backwards in the string" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "s4 = \"GGGCGUGGCGCGUA\" # first few bases of the first nucleotide to be sequenced\n", " # alanine tRNA (Holley 1965)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"T\" in s4 # test for inclusion" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"GUG\" in s4 # to for a substring" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "s4.lower() # make lower case" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Data Structures" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples are ordered collections of arbitrary Python objects." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define a tuple" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# tuple indexing" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# deletion doesn't work, because tuples are immutable" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define a list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# list indexing" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# returns the reversed list but doesn't change x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# reverses the list in place, i.e. actually effects data" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists, unlike strings and tuples, are mutable (i.e. can be changed)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# demonstrate list mutability" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# delete elements from list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# append elements to list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert elements in list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Sets" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define two set variables x and y" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# intersection of x and y" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# union of x and y" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# get elements of x that are not in y" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# elements of sets must be non-mutable so a list can't be inserted in a set" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# but a tuple works because it's non-mutable" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define a dictionary" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# dictionary lookup on keys" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# add key,value pair to dictionary" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# delete item from the dictionary" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Control Flow Statements" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "for, range" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A \"for\" statement iterates over the elements in a seqeunce" ] }, { "cell_type": "code", "collapsed": false, "input": [ " " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `range` function creates a list of integers. Use `help` to read the range documentation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(range)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "list comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List comprehensions are a more compact and efficient way to iterate over the elements of a list, usually applying some function to the list elements. A list comprehension returns another list." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline # enable plotting in the notebook\n", "from pylab import * # import all the functions define in the pylab module" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# plot a simple relationship\n", "# the plot function comes from the Matplotlib drawing library" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "if, if-else" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "break, continue" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }