python - Python Carl Kingsford - 648b Basic Structure...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Python Carl Kingsford - 648b
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Basic Structure Python is an interpreted language (like Perl). Programs are in files with the .py extension. Programs should start with a “#!” line: #!/usr/bin/env python Programs are executed from top to bottom. Advanced: it’s strongly dynamically typed (values have a fixed type, but variables can change type on the fly.) Most unusual syntax: indenting and newlines are important. Unlike Perl, there are no { } characters to indicate the start and end of a block. That is done through indenting.
Background image of page 2
Interactive Mode The command “python” will start an interactive python session: $ python Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> You can enter any python commands here. The most important one is help(x), which will show you detailed help on function (or type or class) x. Use Ctrl-D or quit() to exit.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example #!/usr/bin/env python import sys import seq def remove_gap(s): return s.replace('-','') S1 = seq.read_fasta(sys.argv[1]) S2 = seq.read_fasta(sys.argv[2]) print sys.argv[1] print sys.argv[2] SD1 = dict((s.name, s) for s in S1) SD2 = dict((s.name, s) for s in S2) assert len(SD1) == len(SD2) for s in SD1.itervalues(): if s.seq != SD2[s.name].seq: print 'DISAGREE:', s.name print s.seq print SD2[s.name].seq if s.seq == SD2[s.name].seq: print 'AGREE:', s.name Import some libraries (sys is a standard one; seq is one I wrote) Define a function Call the function “read_fasta” in the seq library. Print some info to the screen Create some dictionary data structures (called hashes in Perl) that map sequence names to DNA sequences. For every sequence in the dictionary SD1, check that the corresponding sequence in SD2 matches
Background image of page 4
Example 2 def random_order(n): “Create random mapping between [n] and [n]” import random R = range(n) random.shuffle(R) return dict(enumerate(R)) A function that takes 1 parameter “Docstring” that documents what the function does. Load the “random” library. R = [0, 1, 2, 3, . .., n-1] The list R is randomly shuffled to be something like [7, 8, 10, n-1, . .., 4] Turns shuffled list into a list of pairs: [(0, 7), (1, 8), (2, 10), . ..] Turns list of pairs [(i,j)] into a mapping from i j
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data Structures Main Idea: Sequences
Background image of page 6
Built-in Basic Data Types str = string (delimit with ‘xyz’ or “xyz”) >>> str(10) '10' int = arbitrary-sized integer (see also long ) >>> 7**73 49221735352184872959961855190338177606846542622561400857 262407L float = floating point number >>> 1/2 0 >>> 1.0/2 0.5 bool = True or False >>> bool(10) True >>> bool(0) False
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Collection Data Types list = mutable list >>> ['a','b',10,10,7] ['a', 'b', 10, 10, 7] tuple = frozen list (can’t change) >>> ('a','b',10, 10,7) ('a', 'b', 10, 10, 7) dict = dictionary, aka hash >>> {'a':7, 'b':10, 13:2} {'a': 7, 'b': 10, 13: 2} set = mutable set of elements >>> set(['a','b','b',10]) set(['a', 10, 'b']) frozenset = frozen set of elements >>> frozenset(['a','b','b',10]) frozenset(['a', 10, 'b'])
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 423 taught by Professor Staff during the Fall '07 term at Maryland.

Page1 / 33

python - Python Carl Kingsford - 648b Basic Structure...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online