|
1
|
|
|
2
|
- Overview of program/script development (BP Ch 1)
- Python Basics (BP Ch1)
- Python Types and Operators
- Numbers and Arithmetic operators (BP Ch1, App B)
- Strings (BP Ch3)
- Lists and Dictionaries (BP Ch2 and Ch4)
- Input & Output (BP Ch1)
- Programming Workshop #1
|
|
3
|
|
|
4
|
- A portable, interpretive, object-oriented programming language
- Elegant syntax
- Powerful high-level built-in data types
- Numbers, strings, lists, dictionaries
- Full set of string operations
|
|
5
|
- Previously used C++
- Scripting languages useful for bioinformatics
- Perl was “bioinformatics standard”
- Python is more “robust” for larger software projects
|
|
6
|
- DNA from the Beginning
- http://www.dnaftb.org/dnaftb/
- Python Tutorial
- http://www.python.org/doc/current/tut/tut.html
|
|
7
|
- Python interpreter - will run on windows, you need to download it in two
parts:
- 1. The actual interpreter and core of python http://www.python.org/download/releases/
(we’re using python-2.3.3 in class).
- 2. An integrated development environment for python called pythonwin,
by Mark Hammond http://sourceforge.net/project/showfiles.php?group_id=78018
|
|
8
|
- Python comments
- Header comments
- #Description of program
- #Written by:
- #Date created:
- #Last Modified:
|
|
9
|
- Python variables are not “declared”.
- To assign a variable, just type:
identifier=literal
- Identifiers
- Have the following restrictions:
- Must start with a letter or underscore (_)
- Case sensitive
- Must consist of only letters, numbers or underscore
- Must not be a reserved word
- Have the following conventions:
- All uppercase letters are used for constants
- Variable names are meaningful – thus, often multi-word (but not too
long)
- Convention 1:
alignment_sequence
(align_seq)
- Convention 2:
AlignmentSequence
(AlignSeq)
- Python specific conventions (Avoid _X, __X__, __X, _)
|
|
10
|
- Numbers
- Normal Integers –represent whole numbers
- Long Integers – unlimited size
- Ex: 9999999999999999999999L
- Floating-point – represent numbers with decimal places
- Ex: 1.2, 3.14159,3.14e-10
- Octal and hexadecimal numbers
- Complex numbers
|
|
11
|
- + add
- - subract
- * multiply
- / divide
- % modulus/remainder
|
|
12
|
- << shift left
- >> shift right
- ** raise to power
|
|
13
|
- Relational operators
- == equal
- !=, <> not equal
- > greater than
- >= greater than or
- equal
- < less than
- <= less than or equal
- Logical operators
|
|
14
|
- Assume x = 1, y = 4, z = 14
|
|
15
|
- Assume x = 1, y = 4, z = 14
|
|
16
|
- Enclosed in single or double quotes
- Ex: ‘Hello!’ , “Hello!”, “3.5”,
“a”, ‘a’
- Sequence of characters:
mystring=“hello world!”
- mystring[0] -> “h” mystring[1] -> “e”
- mystring[2] -> “l” mystring[-1] -> “!”
|
|
17
|
|
|
18
|
- slicing:
mystring = “spoon!”
- mystring[2:] -> “oon!”
mystring[:3] -> “spo” #note last element is never
included!
mystring[1:3]-> “po”
- Many useful built-in functions
- mystring.upper() -> “SPOON!”
- mystring.replace(‘o’, ‘O’) -> “spOOn!”
|
|
19
|
- “%” operator:
sort of “fill in the blanks” operation:
mystring=“%s has %d marbles” % (“John”,35)
mystring -> “John has 35 marbles”
- %s replace with string
- %d,%i replace with integer
- %f replace with float
|
|
20
|
|
|
21
|
- Tuples – sequence of values
like lists, but cannot be changed after it is created
mytuple=(1,”a”,”bc”,3,87.2)
mytuple[2] -> “bc”
- mytuple[1]=“3”
- Used when you want to pass several variables around at once
|
|
22
|
- Dictionaries – map ‘keys’ to ‘values’
- like lists, but indices can be of any type
- Also, keys are in no particular order
- Eg:
mydict={‘b’:3, ’a’:4, 75:2.85}
mydict[‘b’] -> 3
mydict[75] -> 2.85
mydict[‘a’] -> 4
|
|
23
|
|
|
24
|
- Slicing not allowed
- Referencing invalid key is an error:
- >>> mydict={8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9:
'nine'}
- >>> mydict["red"]
- Traceback (most recent call last):
File "<interactive input>", line 1, in ?
- KeyError: 'red‘
- Use mydict.get(“red”) instead, it returns None if key is not found
|
|
25
|
- Function raw_input() designed to read a line of input from the user
- 1 optional argument: string to prompt user
- If int or float desired, simply convert string:
- int(mystring)->convert to int (if possible)
- float(mystring)->convert to float (if possible)
|
|
26
|
- Function print
- Prints each argument, followed by space
- After all arguments, prints newline
- Put comma after last arg to prevent newline
- “add” strings to avoid spaces
- print “a”,”b”,”c”
- a b c
- print “a”,”b”,”c”,
- a b c
- print “a”+”b”+”c”
- abc
|
|
27
|
- >>> print "hello","world";print "hello","again"
- hello world
- hello again
- >>> print "hello","world",;print "hello","again"
- hello world hello again
- >>> print "hello %s world" % "cold and
cruel"
- hello cold and cruel world
- >>> print "hello","cold"+ " " + "and","cruel","world"
- hello cold and cruel world
|
|
28
|
- Enter your program in the editor
- Notice that the editor has a color coding
- Also notice that it automatically indents
- Don’t override!! – this is how python tells when block statements end!
- If doesn’t indent to proper location – indicates bug
|
|
29
|
- To build your program
- Under File->Run…
- Select No Debugging in the drop-down window
- Fix any errors, then run again
|
|
30
|
- Write a Python program to compute the hydrophobicity of an amino acid
|