Title: An Introduction to Python
1An Introduction to Python Part II
- Dr. Nancy Warter-Perez
- April 12, 2007
2Overview
- Solution to Programming Workshop 1
- If tests
- Loops
- for
- while
- Example amino acid search program
- Programming Workshop 2
3Solution to Programming Workshop 1
- Write a Python program to compute the
hydrophobicity of an amino acid - Program to compute the hydrophobicity of an
amino acid - (solution only includes first 3 amino acids)
- Written by Prof. Warter-Perez
- Date created April 15, 2004
- Last modified
- hydro "A"1.8,"C"2.5,"D"-3.5
- aa raw_input ("Please enter amino acid ")
- print "The hydrophobicity of s is f." (aa,
hydroaa)
4Make solution case insensitive
Program to compute the hydrophobicity of an
amino acid Written by Prof. Warter-Perez
Date created April 15, 2004 Last modified
April 20, 2004 - made script case insensitive for
amino acids hydro "A"1.8,"C"2.5,"D"-3.5
aa raw_input ("Please enter amino acid ") aa
aa.upper() print "The hydrophobicity of s is
f." (aa, hydroaa)
5Python Basics Relational and Logical Operators
- Relational operators
- equal
- ! not equal
- gt greater than
- gt greater than or
- equal
- lt less than
- lt less than or equal
- Logical operators
- and and
- or or
- not not
6if Statement
- Example
- a1 'A a2 'C'
- match 0
- if (a1 a2)
- match1
7if-elif-else Statement
- if expression
- action 1
- elif expression action 2else
- action 3
- Example
- a1 'A a2 'C'
- match 0 gap 0
- if (a1 a2)
- match1
- elif (a1 gt a2)
-
- else
- gap1
8String operations
mystring Hello World! mystring Hello World! mystring Hello World!
Expression Value Purpose
len(mystring) 12 number of characters in mystring
helloworld helloworld Concatenate strings
s worldhello hello world Format strings (like sprintf)
world hello world world 0 or False 1 or True Test for equality
a lt b b lt a 1 or True 0 or False Alphabetical ordering
9Lists
mylista,b,3.58,d,4,0 mylista,b,3.58,d,4,0 mylista,b,3.58,d,4,0
mylist0 mylist2 a 3.58 Indexing
mylist-1 mylist-2 0 4 Negative indexing (counts from end)
mylist14 b,3.58,d Slicing (like strings)
b in mylist e not in mylist 1 or True 1 or True
mylist.append(8) a,b,3.58,d,4,0,8 Add to end of list
10Dictionaries
mydictr1,g2,y3.5,8.58,9nine mydictr1,g2,y3.5,8.58,9nine mydictr1,g2,y3.5,8.58,9nine
mydict.keys() 'y', 8.5, 'r', 'g', 9 List of the keys
mydict.values() 3.5, 8, 1, 2, 'nine' List of the values
mydicty 3.5 Value lookup
mydict.has_key(r) True or 1 Check for keys
mydict.update(a75) 8.5 8, 'a' 75, 'r' 1, 'g' 2, 'y' 3.5, 9 'nine' Add pairs to dictionary
11for Statement
- for var in list
- action
- Sets var to each item in list and performs action
- range() function generates lists of numbers
range (5) -gt 0,1,2,3,4
- Example
- mylisthello,hi,hey,!
- for i in mylist
- print i
Iteration 1 prints hello Iteration 2 prints
hi Iteration 3 prints hey Iteration 4 prints !
12while Statement
- Example
- x 0
- while x ! 3
- x x 1
/ 2 Infinite loop!
Iteration 1 x011 Iteration 2
x112 Iteration 3 x213 Iteration 4 dont
exec
13Example Amino Acid Search
- Write a program to count the number of
occurrences of an amino acid in a sequence. - The program should prompt the user for
- A sequence of amino acids (seq)
- The search amino acid (aa)
- The program should display the number of times
the search amino acid (aa) occurred in the
sequence (seq)
14Example Amino Acid Search (2)
this program will calculate the number of
occurrences of an amino acid in a sequence by
Bryce Ready done0 while (not done)
sequenceraw_input("Please enter a sequence")
aaraw_input("Please enter the amino acid to
look for")
15Example Amino Acid Search (3)
compute the number of occurrences using for
loop cnt0 for i in sequence
if i aa cnt1 if cnt 1
print "s occurs in that sequence once"
aa else print "s occurs in that
sequence d times" (aa, cnt)
answerraw_input("try again? yn") if answer
"n" or answer "N" done 1
16Creating a Python Program
- Enter your program in the editor
- Notice that the editor has a color coding
- Comments
- Key words
- Etc
- Also notice that it automatically indents
- Dont override!! this is how python tells when
block statements end! - If doesnt indent to proper location indicates
bug
17Running your Program
- To build your program
- Under File-gtRun
- Select No Debugging in the drop-down window
- Fix any errors, then run again
18File I/O
- To open a file
- myfile open('pathname', ltmodegt)
- modes
- 'r' read
- 'w' write
- Ex infile open("D\\Docs\\test.txt", 'r')
- Ex outfile open("out.txt", 'w') in same
directory
19Common input file operations
Operation Interpretation
input open ('file', 'r') open input file
S input.read() read entire file into string S
S input.read(N) Read N bytes (Ngt 1)
S input.readline() Read next line
L input.readlines() Read entire file into list of line strings
20Common output file operations
Operation Interpretation
output open('file', 'w') create output file
output.write(S) Write string S into file
output.writelines(L) Write all line strings in list L into file
output.close() Manual close (good habit)
21Programming Workshop 2
- Write a sliding window program to compute the GC
in a sequence of nucleotides. - The program should prompt the user for
- The DNA sequence
- The window size (assume the window increment is
1) - Test your program using the data for Workshop 3.