Introduction to Python BCHB 524 Lecture 3 BCHB

Introduction to Python BCHB 524 Lecture 3 BCHB 524 - Edwards

Outline l l Review Functions & Methods Defining new functions Control flow: if statement BCHB 524 - Edwards 2

Outline l Review l l l Hello World (Printing, Execution) Simple Numbers (Variables, Integers) Simple Numbers II (Floats) DNA Sequence (Strings, characters from) DNA Sequence II (String arithmetic, methods) BCHB 524 - Edwards 3

Hello World # Output Hello World to the terminal print "Hello World!" print "Hello Georgetown!" print 'Hello Everyone' l Printing, order of execution, comments, blank-lines, syntax errors, various errors BCHB 524 - Edwards 4

Simple Numbers # Program input cars = 100 people_per_car = 4 drivers = 30 passengers = 90 # Compute the dependent values cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * people_per_car average_people_per_car = ( drivers + passengers ) / cars_driven people_in_last_car = ( drivers + passengers - 1 ) % people_per_car + 1 # Output the results print "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. " BCHB 524 - Edwards 5

Simple Numbers (Review) l l l l 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 integer BCHB 524 - Edwards 6

Simple Numbers II # Program input cars = 100. 0 people_per_car = 4. 0 drivers = 30. 0 passengers = 80. 0 # Compute the dependent values cars_not_driven = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * people_per_car average_people_per_car = ( drivers + passengers ) / cars_driven people_in_last_car = ( drivers + passengers - 1 ) % people_per_car + 1 # Output the results print "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. " BCHB 524 - Edwards 7

Simple Numbers II (Review) l l l l 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. BCHB 524 - Edwards 8

DNA Sequence # DNA is cool! dna_sequence = 'gcatgacgttattacgactctgtgtggcgtctgctggg' # Compute dependent values first_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 results print "First nucleotide", first_nucleotide print "Last nucleotide", last_nucleotide print "First four nucleotides", first_four_nucs print "Last ten nucleotides", last_ten_nucs print "Sequence length", sequence_length BCHB 524 - Edwards 9

DNA Sequence (Review) l l l 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 ] l l l Chunks of the sequence, using [s: e] l l 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 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 BCHB 524 - Edwards 10

DNA Sequence II # DNA is cool! dna_sequence = 'gcatgacgttattacgactctgtgtggcgtctgctggg' oligo 1 = 'ATTCG' oligo 2 = 'TCGAT' # Compute dependent values, using arithmetic and string methods ligated_oligos = oligo 1 + oligo 2 tandem_repeat = oligo 1*6 polya = 'A'*20 in_uppercase_symbols = dna_sequence. upper() Number. Of. A = dna_sequence. count('a') Position. Of. T = dna_sequence. find('t') rna_sequence = dna_sequence. replace('t', 'u') # Output results print "Ligated oligos", ligated_oligos print "Tandem repeat", tandem_repeat print "Polynucleotide run", polya print "Uppercase", in_uppercase_symbols print "Number of Adenine", Number. Of. A print "Position of first Thymine", Position. Of. T print "As RNA", rna_sequence BCHB 524 - Edwards 11

DNA Sequence II (Review) l l l l 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. BCHB 524 - Edwards 12

Conversion Functions # There are many useful functions built into Python a. String = 'abcdefghijkl' an. Integer = 10 a. Float = 3. 456 integer. String = '17' float. String = '1. 234' # Conversion print "an. Integer: before", an. Integer, "after", float(an. Integer) print "a. Float: before", a. Float, "after", int(a. Float) print "integer. String: before", integer. String, "after", int(integer. String) print "float. String: before", float. String, "after", float(float. String) # Errors print "float. String: before", float. String, "after", int(float. String) print "a. String: before", a. String, "after", int(a. String) print "a. String: before", a. String, "after", float(a. String) print "float. String + 1: ", float. String + 1 print "integer. String + 1: ", integer. String + 1 # Figure out the internal representation print type(an. Integer), repr(an. Integer), an. Integer print type(a. Float), repr(a. Float), a. Float print type(integer. String), repr(integer. String), integer. String print type(float. String), repr(float. String), float. String BCHB 524 - Edwards 13

More Functions # There are many useful functions built into Python a. String = 'abcdefghijkl' an. Integer = 10 a. Float = 3. 456 negative. Integer = -5 negative. Float = -7. 9999 # Math print "Absolute value: ", abs(negative. Integer) print "Absolute value: ", abs(negative. Float) print "Min", min(an. Integer, a. Float, negative. Integer, negative. Float) print "Max", max(an. Integer, a. Float, negative. Integer, negative. Float) # String length print len(a. String) # Complicated expressions! print "Also known as 1. 0: ", abs(-3)*(1/float('3. 0')) print "Also known as 5: ", int(float('1. 23456'))*5 BCHB 524 - Edwards 14

String Methods # String methods are very useful! seq = 'gcatgacgttattacgactctgtgtggcgtctgctggg' # A few important string methods print "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') BCHB 524 - Edwards 15

Defining New Functions # Name and describe a small task (no execution!) def helloworld(): print "Hello world" # Functions may be parameterized by arguments def hello(to): print "Hello", to # Functions can return values def bytwo(x): y = 2*x return y # Functions can be parameterized by more than one argument def rectangle_area(length, height): return length*height # Continued. . . BCHB 524 - Edwards 16

Defining New Functions # Continuation. . . # Function execution must occur after its definition helloworld() hello("Georgetown") hello("everyone") helloworld() print bytwo(2), bytwo('abcdef'), bytwo(1. 23456) x = 3 y = 4 z = 5 print bytwo(x), bytwo(y), bytwo(z) print rectangle_area(x, y) BCHB 524 - Edwards 17

Defining New Functions l Saves typing Reduces errors Single change, global effect Conceptual name aids readability l Functions can use other functions! l l l BCHB 524 - Edwards 18

Control Flow: if statement # The input DNA sequence seq = 'atggcatgacgttattacgactctgtgtggcgtctgctggg' # Remove the initial Met codon if it is there if seq. startswith('atg'): print "Sequence without initial Met: ", seq[3: ] else: print "Sequence (no initial Met): ", seq l l Execution path depends on string in seq. Make sure you change seq to different values. BCHB 524 - Edwards 19

Exercises 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 sequence to be translated (no 5' UTR). Write a Python program to print answers to the following questions: l l l 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. BCHB 524 - Edwards 20
- Slides: 20