introduction to python

22
9/4/2013 BCHB524 - 2013 - Edwards Introduction to Python BCHB524 2013 Lecture 3

Upload: emma-dorsey

Post on 02-Jan-2016

24 views

Category:

Documents


1 download

DESCRIPTION

Introduction to Python. BCHB524 2013 Lecture 3. Outline. Review Homework #1 Solutions Functions & Methods Defining new functions Control flow: if statement. Outline. Review Hello World (Printing, Execution) Simple Numbers (Variables, Integers) Simple Numbers II (Floats) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Python

9/4/2013 BCHB524 - 2013 - Edwards

Introduction to Python

BCHB5242013

Lecture 3

Page 2: Introduction to Python

9/4/2013 BCHB524 - 2013 - Edwards

Outline

Review Homework #1 Solutions Functions & Methods Defining new functions Control flow: if statement

2

Page 3: Introduction to Python

9/4/2013 BCHB524 - 2013 - Edwards

Outline

Review Hello World (Printing, Execution) Simple Numbers (Variables, Integers) Simple Numbers II (Floats) DNA Sequence (Strings, characters from) DNA Sequence II (String arithmetic, methods)

3

Page 4: Introduction to Python

Hello World

Printing, order of execution, comments,blank-lines, syntax errors, various errors

9/4/2013 BCHB524 - 2013 - Edwards

# Output Hello World to the terminal

print "Hello World!"

print "Hello Georgetown!"

print 'Hello Everyone'

4

Page 5: Introduction to Python

Simple Numbers

9/4/2013 BCHB524 - 2013 - Edwards

# Program inputcars = 100people_per_car = 4drivers = 30passengers = 90

# Compute the dependent valuescars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * people_per_caraverage_people_per_car = ( drivers + passengers ) / cars_drivenpeople_in_last_car = ( drivers + passengers - 1 ) % people_per_car + 1

# Output the resultsprint "There are", cars, "cars available." print "There are only", drivers, "drivers available."print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_people_per_car, "in each car."print "There are", people_in_last_car, "people in the last car."

5

Page 6: Introduction to Python

Simple Numbers (Review)

Variables (names) to store values Variables to store the result of expressions The variable name itself does not matter, but

should be descriptive Variables must have a value before you use

them Arithmetic operators +, -, *, /, % are available Arithmetic can use variables and values Result of integer division is an integer9/4/2013 BCHB524 - 2013 - Edwards 6

Page 7: Introduction to Python

Simple Numbers II

9/4/2013 BCHB524 - 2013 - Edwards

# Program inputcars = 100.0people_per_car = 4.0drivers = 30.0passengers = 80.0

# Compute the dependent valuescars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * people_per_caraverage_people_per_car = ( drivers + passengers ) / cars_drivenpeople_in_last_car = ( drivers + passengers - 1 ) % people_per_car + 1

# Output the resultsprint "There are", cars, "cars available." print "There are only", drivers, "drivers available."print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_people_per_car, "in each car."print "There are", people_in_last_car, "people in the last car."

7

Page 8: Introduction to Python

Simple Numbers II (Review)

Numbers can be fractional (float) or integer (int).

Floats are entered using a decimal place. Variables can store floats or ints Arithmetic operators +, -, *, /, % are available Arithmetic can use variables or values Result of float division is a float Result of arithmetic with mixed floats and ints

is a float.9/4/2013 BCHB524 - 2013 - Edwards 8

Page 9: Introduction to Python

DNA Sequence

9/4/2013 BCHB524 - 2013 - Edwards

# DNA is cool!dna_sequence = 'gcatgacgttattacgactctgtgtggcgtctgctggg'

# Compute dependent valuesfirst_nucleotide = dna_sequence[0]last_nucleotide = dna_sequence[-1]first_four_nucs = dna_sequence[0:4]last_ten_nucs = dna_sequence[-10:]sequence_length = len(dna_sequence)

# Output resultsprint "First nucleotide",first_nucleotideprint "Last nucleotide",last_nucleotideprint "First four nucleotides",first_four_nucsprint "Last ten nucleotides",last_ten_nucsprint "Sequence length",sequence_length

9

Page 10: Introduction to Python

DNA Sequence (Review) Strings are sequences of symbols (characters) Variables can store strings! (and ints and floats) Access characters from a string stored in a variable using an

index (integer) between [ and ] Positive index from beginning of string 0… Negative index from end of string -1… The index may be stored in a variable The index may be the result of arithmetic

Chunks of the sequence, using [s:e] Chunk starts at index s, ends before index e. If s is missing, start at beginning of string If e is missing, end at end of string.

Function len(…) returns the length of the string

9/4/2013 BCHB524 - 2013 - Edwards 10

Page 11: Introduction to Python

DNA Sequence II

9/4/2013 BCHB524 - 2013 - Edwards

# DNA is cool!dna_sequence = 'gcatgacgttattacgactctgtgtggcgtctgctggg'oligo1 = 'ATTCG'oligo2 = 'TCGAT'

# Compute dependent values, using arithmetic and string methodsligated_oligos = oligo1 + oligo2tandem_repeat = oligo1*6polya = 'A'*20in_uppercase_symbols = dna_sequence.upper()NumberOfA = dna_sequence.count('a')PositionOfT = dna_sequence.find('t')rna_sequence = dna_sequence.replace('t','u')

# Output resultsprint "Ligated oligos",ligated_oligosprint "Tandem repeat",tandem_repeat print "Polynucleotide run",polya print "Uppercase",in_uppercase_symbols print "Number of Adenine",NumberOfA print "Position of first Thymine",PositionOfT print "As RNA",rna_sequence

11

Page 12: Introduction to Python

DNA Sequence II (Review) Strings can be added (concatenation) Strings can be multiplied by an integer

(concatenated copies) Upper and lower-case characters are not the

same s.find(t) → (integer) position of the string t in

string s, if t is in s. Otherwise, -1. s.count(t) → (integer) count of string t in string s. s.upper() → upper-case version of string s. s.replace(u,v) → string s with string u replaced

by string v. 9/4/2013 BCHB524 - 2013 - Edwards 12

Page 13: Introduction to Python

Homework Solutions

9/4/2013 BCHB524 - 2013 - Edwards 13

Page 14: Introduction to Python

Conversion Functions

9/4/2013 BCHB524 - 2013 - Edwards

# There are many useful functions built into Python

aString = 'abcdefghijkl'anInteger = 10aFloat = 3.456integerString = '17'floatString = '1.234'

# Conversionprint "anInteger: before", anInteger, "after", float(anInteger)print "aFloat: before", aFloat, "after", int(aFloat)print "integerString: before", integerString, "after", int(integerString)print "floatString: before", floatString, "after", float(floatString)

# Errorsprint "floatString: before", floatString, "after", int(floatString)print "aString: before", aString, "after", int(aString)print "aString: before", aString, "after", float(aString)print "floatString + 1:", floatString + 1print "integerString + 1:", integerString + 1

# Figure out the internal representationprint type(anInteger), repr(anInteger), anIntegerprint type(aFloat), repr(aFloat), aFloatprint type(integerString), repr(integerString), integerStringprint type(floatString), repr(floatString), floatString

14

Page 15: Introduction to Python

More Functions

9/4/2013 BCHB524 - 2013 - Edwards

# There are many useful functions built into PythonaString = 'abcdefghijkl'

anInteger = 10aFloat = 3.456negativeInteger = -5negativeFloat = -7.9999

# Mathprint "Absolute value:", abs(negativeInteger)print "Absolute value:", abs(negativeFloat)print "Min", min(anInteger,aFloat,negativeInteger,negativeFloat)print "Max", max(anInteger,aFloat,negativeInteger,negativeFloat)

# String lengthprint len(aString)

# Complicated expressions!print "Also known as 1.0:",abs(-3)*(1/float('3.0'))print "Also known as 5: ",int(float('1.23456'))*5

15

Page 16: Introduction to Python

String Methods

9/4/2013 BCHB524 - 2013 - Edwards

# String methods are very useful!seq = 'gcatgacgttattacgactctgtgtggcgtctgctggg'

# A few important string methodsprint "The number of 'a' symbols:",seq.count('a')print "The sequence in uppercase:",seq.upper()print "Does it end with tggg:",seq.endswith('tggg')print "Does it start with atg:",seq.startswith('atg')print "What position is tggg in:",seq.find('tggg')print "After conversion to uppercase?",seq.upper().find('TGGG')

16

Page 17: Introduction to Python

Defining New Functions

9/4/2013 BCHB524 - 2013 - Edwards

# Name and describe a small task (no execution!)def helloworld(): print "Hello world"

# Functions may be parameterized by argumentsdef hello(to): print "Hello",to

# Functions can return valuesdef bytwo(x): y = 2*x return y

# Functions can be parameterized by more than one argumentdef rectangle_area(length,height): return length*height

# Continued...

17

Page 18: Introduction to Python

Defining New Functions

9/4/2013 BCHB524 - 2013 - Edwards

# Continuation...

# Function execution must occur after its definitionhelloworld()helloworld()hello("Georgetown")hello("everyone")helloworld()

print bytwo(2), bytwo('abcdef'), bytwo(1.23456)x = 3y = 4z = 5print bytwo(x), bytwo(y), bytwo(z)print rectangle_area(x,y)

18

Page 19: Introduction to Python

Defining New Functions

Saves typing Reduces errors Single change, global effect Conceptual name aids readability

Functions can use other functions!

9/4/2013 BCHB524 - 2013 - Edwards 19

Page 20: Introduction to Python

Control Flow: if statement

Execution path depends on string in seq. Make sure you change seq to different

values.9/4/2013 BCHB524 - 2013 - Edwards

# The input DNA sequenceseq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg'

# Remove the initial Met codon if it is thereif seq.startswith('atg'): print "Sequence without initial Met:",seq[3:]else: print "Sequence (no initial Met):",seq

20

Page 21: Introduction to Python

9/9/2013 BCHB524 - 2013 - Edwards

Homework 2

Due Monday, September 9th.

Complete Rosalind problems 4 and 5.

Submit using Blackboard

Use only the techniques introduced so far.

Make sure you can run the programs

demonstrated in lecture.

21

Page 22: Introduction to Python

Exercise 1 Download or copy-and-paste the DNA sequence of

the Anthrax SASP gene from the anthrax_sasp.nuc file in the course data-directory. Treat the provided sequence as the entire gene.

Write a Python program to print answers the following questions: Does the SASP gene start with a Met codon? Does the SASP gene have a frame 1 Met codon? How many nucleotides in the SASP gene? How many amino-acids in the SASP protein? What is the GC content (% G or C nucleotides) of the

SASP gene? Test your program with other gene sequences.9/4/2013 BCHB524 - 2013 - Edwards 22