Download - Cal State LA - Instructional Web Server

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
An Introduction to Python – Part II
Dr. Nancy Warter-Perez
June 15, 2005
Overview

References





Solution to Programming Workshop #1
If tests (PP Ch1, LP Ch 9)
Loops (PP Ch1, LP Ch 10)




Python Programming for the Absolute Beginner (PP)
Learning Python (LP)
for
while
Example amino acid search program
Programming Workshop #2
6/15/05
Introduction to Python – Part II
2
Solution 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, hydro[aa])
6/15/05
Introduction to Python – Part II
3
Make 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, hydro[aa])
6/15/05
Introduction to Python – Part II
4
Python Basics – Relational and
Logical Operators

Relational operators
==
!=
>
>=
<
<=
6/15/05
equal
not equal
greater than
greater than or
equal
less than
less than or equal

Logical operators
and
or
not
Introduction to Python – Part II
and
or
not
5
if Statement

if expression:
action
6/15/05
Example:
a1 = 'A‘; a2 = 'C';
match = 0;
if (a1 == a2) :
match+=1;
Introduction to Python – Part II
6
if-elif-else Statement

if expression:
action 1
elif expression:
action 2
else :
action 3
Example:
a1 = 'A‘; a2 = 'C';
match = 0; gap = 0;
if (a1 == a2) :
match+=1;
elif (a1 > a2):
else:
gap+=1;
6/15/05
Introduction to Python – Part II
7
for Statement
for var in list:


action
Sets var to each item in
list and performs action
range() function
generates lists of
numbers:
range (5) -> [0,1,2,3,4]
Example
mylist=[“hello”,”hi”,”hey”,”!”];
for i in mylist:
print i
Iteration 1 prints: hello
Iteration 2 prints: hi
Iteration 3 prints: hey
Iteration 4 prints: !
6/15/05
Introduction to Python – Part II
8
while Statement
while expression:
action
Example
x = 0;
while x != 3:
x = x + 1/ 2
Infinite loop!
Iteration
Iteration
Iteration
Iteration
6/15/05
Introduction to Python – Part II
1:
2:
3:
4:
x=0+1=1
x=1+1=2
x=2+1=3
don’t exec
9
Example: 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



6/15/05
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)
Introduction to Python – Part II
10
Example: Amino Acid Search (2)
#this program will calculate the number of occurrences of an amino
acid in a
#sequence
#by Bryce Ready
done=0
while (not done):
sequence=raw_input("Please enter a sequence:");
aa=raw_input("Please enter the amino acid to look for:");
6/15/05
Introduction to Python – Part II
11
Example: Amino Acid Search (3)
#compute the number of occurrences using for loop
cnt=0
for i in sequence:
if i == aa:
cnt+=1
if cnt == 1:
print "%s occurs in that sequence once" % aa
else:
print "%s occurs in that sequence %d times" % (aa, cnt)
answer=raw_input("try again? [yn]")
if answer == "n" or answer == "N":
done = 1
6/15/05
Introduction to Python – Part II
12
Programming Workshop #2

Write a sliding window program to compute
the %GC in a sequence of nucleotides.

The program should prompt the user for


6/15/05
The DNA sequence
The window size (assume the window increment is 1)
Introduction to Python – Part II
13